libgdx学习记录13——矩形CD进度条绘制
利用ShapeRenderer可进行矩形进度条的绘制,多变形的填充等操作。
这是根据角度获取矩形坐标的函数。
public Vector2 GetPoint( float x, float y, float w, float h, float angle ){
Vector2 point = new Vector2();
while( angle >= 360 ){
angle -= 360;
}
while( angle < 0 ){
angle += 360;
} System.out.println( GetAtan( h/w ) ); if( angle>=0 && angle<GetAtan( h/w ) || angle>=360-GetAtan( h/w ) && angle<360 ){
point.x = x+w;
point.y = y+w*Change( angle );
}
else if( angle>=GetAtan( h/w ) && angle<180-GetAtan( h/w ) ){
point.x = x+h*Change( 90-angle );
point.y = y+h;
}
else if( angle>=180-GetAtan( h/w ) && angle<180+GetAtan( h/w ) ){
point.x = x-w;
point.y = y-w*Change( angle );
}
else if( angle>=180+GetAtan( h/w ) && angle<360-GetAtan( h/w ) ){
point.x = x-h*Change( 90-angle );
point.y = y-h;
} return point;
}
画部分矩形
public void FillPartRect( float x, float y, float w, float h, float start, float angle, ShapeRenderer rend, Color color )
{
rend.begin( ShapeType.Line );
rend.setColor( color );
rend.rect( x-w, y-h, 2*w, 2*h );
rend.end(); rend.begin( ShapeType.Filled ); for( int i=0; i<angle-1; i++ ){
Vector2 point1 = GetPoint( x, y, w, h, start+i );
Vector2 point2 = GetPoint( x, y, w, h, start+i+1 );
rend.triangle( x, y, point1.x, point1.y, point2.x, point2.y );
}
rend.end();
}
全部代码如下:
package com.fxb.newtest; import sun.security.provider.certpath.Vertex; import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2; public class Lib012_CdDraw extends ApplicationAdapter{ ShapeRenderer rend; float[] vertexs; Pixmap pixmap;
float angle = 0; @Override
public void create() {
// TODO Auto-generated method stub
rend = new ShapeRenderer(); vertexs = new float[]{ 100, 100, 250, 100, 200, 200, 100, 250 }; } public void FillPolygon( float[] vertexs, ShapeRenderer rend, Color color ){
if( vertexs.length%2 != 0 ){
return;
}
rend.begin( ShapeType.Filled );
rend.setColor( color );
for( int i=2; i<=vertexs.length-4; i+=2 ){
rend.triangle( vertexs[0], vertexs[1], vertexs[i], vertexs[i+1], vertexs[i+2], vertexs[i+3] );
}
rend.end();
} public float Change( float angle0 ){
return (float)Math.tan( angle0*(float)Math.PI/180 );
} public void FillRect( float x, float y, float w, float h, float start, float angle, ShapeRenderer rend, Color color ){ rend.begin( ShapeType.Line );
rend.setColor( color );
rend.rect( x-w, y-h, 2*w, 2*h );
rend.end(); rend.begin( ShapeType.Filled );
rend.setColor( color );
//angle = angle*(float)Math.PI/180; if( angle <= 45 ){
rend.triangle( x, y, x+w, y, x+w, y+w*(float)Math.tan( angle*(float)Math.PI/180 ) );
}
else if( angle <= 135 ){
rend.triangle( x, y, x+w, y, x+w, y+w );
rend.triangle( x, y, x+w, y+h, x+h*(float)Math.tan( (90-angle)*(float)Math.PI/180 ), y+h );
}
else if( angle <= 135+90 ){
rend.triangle( x, y, x+w, y, x+w, y+w );
rend.triangle( x, y, x+w, y+h, x-w, y+h );
rend.triangle( x, y, x-w, y+h, x-w, y-w*Change( angle ) );
}
else if( angle <= 135+180 ){
rend.triangle( x, y, x+w, y, x+w, y+w );
rend.triangle( x, y, x+w, y+h, x-w, y+h );
rend.triangle( x, y, x-w, y+h, x-w, y-h );
rend.triangle( x, y, x-w, y-h, x-h*Change( 270-angle ), y-h );
}
else if( angle <= 360 ){
rend.triangle( x, y, x+w, y, x+w, y+w );
rend.triangle( x, y, x+w, y+h, x-w, y+h );
rend.triangle( x, y, x-w, y+h, x-w, y-h );
rend.triangle( x, y, x-w, y-h, x+w, y-h );
rend.triangle( x, y, x+w, y-h, x+w, y+w*Change(angle) );
} rend.end();
} public float GetAtan( float angle ){
return (float)( 180*Math.atan( angle )/Math.PI );
} public Vector2 GetPoint( float x, float y, float w, float h, float angle ){
Vector2 point = new Vector2();
while( angle >= 360 ){
angle -= 360;
}
while( angle < 0 ){
angle += 360;
} System.out.println( GetAtan( h/w ) ); if( angle>=0 && angle<GetAtan( h/w ) || angle>=360-GetAtan( h/w ) && angle<360 ){
point.x = x+w;
point.y = y+w*Change( angle );
}
else if( angle>=GetAtan( h/w ) && angle<180-GetAtan( h/w ) ){
point.x = x+h*Change( 90-angle );
point.y = y+h;
}
else if( angle>=180-GetAtan( h/w ) && angle<180+GetAtan( h/w ) ){
point.x = x-w;
point.y = y-w*Change( angle );
}
else if( angle>=180+GetAtan( h/w ) && angle<360-GetAtan( h/w ) ){
point.x = x-h*Change( 90-angle );
point.y = y-h;
} return point;
} public void FillRect1( float x, float y, float w, float h, float start, float angle, ShapeRenderer rend, Color color ){ rend.begin( ShapeType.Line );
rend.setColor( color );
rend.rect( x-w, y-h, 2*w, 2*h );
rend.end(); rend.begin( ShapeType.Filled );
rend.setColor( color );
//angle = angle*(float)Math.PI/180; if( angle <= 45 ){
rend.triangle( x, y, x, y+h, x+h*Change( angle ), y+h );
}
else if( angle <= 135 ){
rend.triangle( x, y, x, y+h, x+w, y+w );
rend.triangle( x, y, x+w, y+h, x+w, y+w*Change( 90-angle ) );
}
else if( angle <= 135+90 ){
rend.triangle( x, y, x, y+h, x+w, y+w );
rend.triangle( x, y, x+w, y+h, x+w, y-h );
rend.triangle( x, y, x+w, y-h, x-h*Change( angle ), y-h );
}
else if( angle <= 135+180 ){
rend.triangle( x, y, x, y+h, x+w, y+w );
rend.triangle( x, y, x+w, y+h, x+w, y-h );
rend.triangle( x, y, x+w, y-h, x-w, y-h );
rend.triangle( x, y, x-w, y-h, x-w, y-w*Change( 270-angle ) );
}
else if( angle <= 360 ){
rend.triangle( x, y, x, y+h, x+w, y+w );
rend.triangle( x, y, x+w, y+h, x+w, y-h );
rend.triangle( x, y, x+w, y-h, x-w, y-h );
rend.triangle( x, y, x-w, y-h, x-w, y+h );
rend.triangle( x, y, x-w, y+h, x+h*Change( angle ), y+h );
} rend.end();
} public void FillRect2( float x, float y, float w, float h, float start, float angle, ShapeRenderer rend, Color color ){ rend.begin( ShapeType.Line );
rend.setColor( color );
rend.rect( x-w, y-h, 2*w, 2*h );
rend.end(); rend.begin( ShapeType.Filled );
rend.setColor( color );
//angle = angle*(float)Math.PI/180;
rend.rect( x-w, y-h, 2*w, 2*h ); //rend.setColor( color.mul( 0.5f ) ); if( angle <= 45 ){
rend.triangle( x, y, x, y+h, x+h*Change( angle ), y+h );
}
else if( angle <= 135 ){
rend.triangle( x, y, x, y+h, x+w, y+w );
rend.triangle( x, y, x+w, y+h, x+w, y+w*Change( 90-angle ) );
}
else if( angle <= 135+90 ){
rend.triangle( x, y, x, y+h, x+w, y+w );
rend.triangle( x, y, x+w, y+h, x+w, y-h );
rend.triangle( x, y, x+w, y-h, x-h*Change( angle ), y-h );
}
else if( angle <= 135+180 ){
rend.triangle( x, y, x, y+h, x+w, y+w );
rend.triangle( x, y, x+w, y+h, x+w, y-h );
rend.triangle( x, y, x+w, y-h, x-w, y-h );
rend.triangle( x, y, x-w, y-h, x-w, y-w*Change( 270-angle ) );
}
else if( angle <= 360 ){
rend.triangle( x, y, x, y+h, x+w, y+w );
rend.triangle( x, y, x+w, y+h, x+w, y-h );
rend.triangle( x, y, x+w, y-h, x-w, y-h );
rend.triangle( x, y, x-w, y-h, x-w, y+h );
rend.triangle( x, y, x-w, y+h, x+h*Change( angle ), y+h );
} rend.end();
} public void FillPartRect( float x, float y, float w, float h, float start, float angle, ShapeRenderer rend, Color color )
{
rend.begin( ShapeType.Line );
rend.setColor( color );
rend.rect( x-w, y-h, 2*w, 2*h );
rend.end(); rend.begin( ShapeType.Filled ); for( int i=0; i<angle-1; i++ ){
Vector2 point1 = GetPoint( x, y, w, h, start+i );
Vector2 point2 = GetPoint( x, y, w, h, start+i+1 );
rend.triangle( x, y, point1.x, point1.y, point2.x, point2.y );
}
rend.end();
} public void FillCircle( ){ } @Override
public void render() {
// TODO Auto-generated method stub
Gdx.gl.glClearColor( 0, 1, 1, 1 );
Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT ); Gdx.gl.glEnable( GL10.GL_BLEND );
Gdx.gl.glBlendFunc( GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA ); /* rend.begin( ShapeType.Filled );
rend.setColor( Color.BLUE );
//rend.polyline( vertexs );
//rend.line( 100, 100, 200, 200 );
//rend.triangle( 100, 100, 200, 100, 100, 200 ); rend.arc( 100, 100, 100, 0, 60 );
rend.circle( 300, 300, 100 );
rend.end();*/ //FillPolygon( vertexs, rend, Color.BLUE ); angle += 0.5f;
/* if( angle > 360 ){
angle -= 360;
}*/ //angle =
//FillRect( 300, 300, 100, 100, 0, angle, rend, Color.BLUE ); //FillRect1( 300, 300, 100, 100, 0, angle, rend, new Color( 1f, 0.7f, 0.7f, 0.5f ));
//FillRect( 300, 300, 100, 100, 0, 90, rend, new Color( 1, 1, 1, 0 ) );
//FillRect2( 300, 300, 100, 100, 0, angle, rend, Color.BLUE); FillPartRect( 300, 300, 100, 100, 90, 360-angle, rend, Color.BLUE);
} @Override
public void dispose() {
// TODO Auto-generated method stub
rend.dispose();
super.dispose();
} }
运行结果:
libgdx学习记录13——矩形CD进度条绘制的更多相关文章
- jQuery ajax 标准写法及进度条绘制
jQuery ajax 标准写法及进度条绘制 $.ajax({ url: "http://www.microsoft.com", //请求的url地址 dataType: &quo ...
- ProgressBar学习笔记,自定义横向进度条的样式(包含ActionBar上面的进度条)
点显示进度条后→ android:max="100" 进度条的最大值 android:progress 进度条已经完成的进度值 android:progressDrawab ...
- 【Flutter学习】基本组件之进度条(LinearProgressIndicator, CircularProgressIndicator)
一,概述 基本有两种类型: 条形进度条(LinearProgressIndicator) new LinearProgressIndicator( backgroundColor: Colors.bl ...
- libgdx学习记录12——圆角矩形CircleRect
libgdx提供了ShapeRenderer这个工具,用它可以画点.画线.画圆.画矩形.画椭圆.画扇形,但是没有提供画圆角矩形的方法. 刚开始自己尝试分成8端,4端画直线,4端画扇形,发现多了半径几部 ...
- libgdx学习记录24——九宫格NinePatch
NinePatch用于图片纹理拉伸显示.当图片拉伸时,4个角不会拉伸,而只有中间的部分会拉伸,适合做圆角矩形类的Button. 简单示例: package com.fxb.newtest; impor ...
- libgdx学习记录16——资源加载器AssetManager
AssetManager用于对游戏中的资源进行加载.当游戏中资源(图片.背景音乐等)较大时,加载时会需要较长时间,可能会阻塞渲染线程,使用AssetManager可以解决此类问题. 主要优点: 1. ...
- libgdx学习记录2——文字显示BitmapFont
libgdx对中文支持不是太好,主要通过Hireo和ttf字库两种方式实现.本文简单介绍最基本的bitmapfont的用法. 代码如下: package com.fxb.newtest; import ...
- libgdx学习记录3——动画Animation
libgdx动画采用Animation实现,即通过帧动画实现. 代码如下: package com.fxb.newtest; import com.badlogic.gdx.ApplicationAd ...
- libgdx学习记录27——线段与线段相交检测
给定p1, p2, p3, p4四个点,p1,p2为一条线段,p3,p4为一条线段,检测其是否有交点. 可分为三种情况: 1. L2与x轴平行 2. L2与y轴平行 3. L2与坐标轴不平行. (L1 ...
随机推荐
- 《AngularJS权威教程》
第二章.数据绑定 2.2 简单的数据绑定 <!DOCTYPE html> <html ng-app> <head> <title>Simple app& ...
- JavaScript语法详解:if语句&for循环&函数
本文首发于博客园,并在GitHub上持续更新前端的系列文章.欢迎在GitHub上关注我,一起入门和进阶前端. 以下是正文. if语句 最基本的if语句 if语句的结构体:(格式) if (条件表达式) ...
- Oracle EBS INV 查询物料无值 ECO
查找物料的时候报错 没有输入值 解决方法: 针对FORM做trace 多查看几个生成的trace 搜索 MTL_SYSTEM_ITEMS_b 的信息 查看到最后面的语句(一般可直接查看) 看SQL 哪 ...
- 修改Sql Server 数据库文件默认存放目录
-- 更改数据文件存放目录 EXEC xp_instance_regwrite @rootkey='HKEY_LOCAL_MACHINE', @key='Software\Micr ...
- gitlab配置push -f 关闭
默认路径是/var/opt/gitlab/git-data/repositories/组/库 修改conf 文件 [core] repositoryformatversion = 0 filemode ...
- Vue.js Is Good, but Is It Better Than Angular or React?
Vue.js is a JavaScript library for building web interfaces. Combining with some other tools It also ...
- mysql5.7 误删管理员root账户
1.停止数据库,并在mysql配置文件my.cnf中添加skip-grant-tables参数到[mysqld]配置块中 2. 执行 systemctl start mysqld 3. 执行 mysq ...
- 代理工具--mitmproxy
#代理工具 mitmproxy 指令:mitmproxy -b ip -p port(代理ip设置为:ip,端口设置为:port) 拦截request: 输入字母“i”(代表Intercept fil ...
- python difflib.md
difflib 此模块提供了用于比较序列的类和函数.它可以用于例如比较文件,并且可以产生各种格式的差异信息,包括HTML和上下文以及统一差异. difflib 模块包含用于计算和处理序列间差异的工具. ...
- Rx编程的第一步是将native对象转换为monad对象
Rx编程的第一步是将native对象转换为monad对象 将基础类型转换为高阶类型,以便使用函数式编程的特性.