利用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进度条绘制的更多相关文章

  1. jQuery ajax 标准写法及进度条绘制

    jQuery ajax 标准写法及进度条绘制 $.ajax({ url: "http://www.microsoft.com", //请求的url地址 dataType: &quo ...

  2. ProgressBar学习笔记,自定义横向进度条的样式(包含ActionBar上面的进度条)

     点显示进度条后→   android:max="100" 进度条的最大值 android:progress  进度条已经完成的进度值 android:progressDrawab ...

  3. 【Flutter学习】基本组件之进度条(LinearProgressIndicator, CircularProgressIndicator)

    一,概述 基本有两种类型: 条形进度条(LinearProgressIndicator) new LinearProgressIndicator( backgroundColor: Colors.bl ...

  4. libgdx学习记录12——圆角矩形CircleRect

    libgdx提供了ShapeRenderer这个工具,用它可以画点.画线.画圆.画矩形.画椭圆.画扇形,但是没有提供画圆角矩形的方法. 刚开始自己尝试分成8端,4端画直线,4端画扇形,发现多了半径几部 ...

  5. libgdx学习记录24——九宫格NinePatch

    NinePatch用于图片纹理拉伸显示.当图片拉伸时,4个角不会拉伸,而只有中间的部分会拉伸,适合做圆角矩形类的Button. 简单示例: package com.fxb.newtest; impor ...

  6. libgdx学习记录16——资源加载器AssetManager

    AssetManager用于对游戏中的资源进行加载.当游戏中资源(图片.背景音乐等)较大时,加载时会需要较长时间,可能会阻塞渲染线程,使用AssetManager可以解决此类问题. 主要优点: 1. ...

  7. libgdx学习记录2——文字显示BitmapFont

    libgdx对中文支持不是太好,主要通过Hireo和ttf字库两种方式实现.本文简单介绍最基本的bitmapfont的用法. 代码如下: package com.fxb.newtest; import ...

  8. libgdx学习记录3——动画Animation

    libgdx动画采用Animation实现,即通过帧动画实现. 代码如下: package com.fxb.newtest; import com.badlogic.gdx.ApplicationAd ...

  9. libgdx学习记录27——线段与线段相交检测

    给定p1, p2, p3, p4四个点,p1,p2为一条线段,p3,p4为一条线段,检测其是否有交点. 可分为三种情况: 1. L2与x轴平行 2. L2与y轴平行 3. L2与坐标轴不平行. (L1 ...

随机推荐

  1. CSS 小结笔记之变形、过渡与动画

    1.过渡 transition  过渡属性用法: transition :ransition-property  transition-duration  transition-timing-func ...

  2. Python字符串和编码

    在最早的时候只有127个字符被编码到计算机里,也就是大小写英文字母.数字和一些符号,这个编码被成为ASCII编码. 但是要处理中文显然一个字节是不够的,至少需要两个字节,而且还不能和ASCII编码冲突 ...

  3. 关于innodb mtr模块

    mtr (mini-transaction)微事务 mtr作用 mtr模块主要保证物理操作的一致性和原子性 1 一致性:通过读写锁来保证 2 原子性:涉及到的物理更新,都记入redo日志 mtr何时使 ...

  4. c#中(&&,||)与(&,|)的区别和应用

    对于(&&,||),运算的对象是逻辑值,也就是True/False &&相当与中文的并且,||相当于中文的或者 .(叫做逻辑运算符又叫短路运算符) 运算结果只有下列四种 ...

  5. C#中获取数组中相加和最接近或等于(<=)给定值的算法

    , ,,,,,,,,, }; List<List<int>> mylist = new List<List<int>>(); int length = ...

  6. 初识java内存区域

    目录: 1.运行时数据区域 2.对象的创建 3.对象的内存布局 4.对象的访问定位 一.运行时数据区域 基本的java虚拟机运行时数据区如下图: 下面我们就来逐个认识这几个运行时的数据区域 1.程序计 ...

  7. 访问url地址 但tomcat会发两次请求??

    statDate===2017-06-27================2017年7月11日 16:06:43执行成功,共删除0条.2017年7月11日 16:06:43执行成功,共插入48835条 ...

  8. [Spark Core] 在 Spark 集群上运行程序

    0. 说明 将 IDEA 下的项目导出为 Jar 包,部署到 Spark 集群上运行. 1. 打包程序 1.0 前提 搭建好 Spark 集群,完成代码的编写. 1.1 修改代码 [添加内容,判断参数 ...

  9. Redis缓存雪崩、缓存穿透、热点Key解决方案和分析

    缓存穿透 缓存系统,按照KEY去查询VALUE,当KEY对应的VALUE一定不存在的时候并对KEY并发请求量很大的时候,就会对后端造成很大的压力. (查询一个必然不存在的数据.比如文章表,查询一个不存 ...

  10. vue+axios自己踩过的坑

    axios的介绍就不用了吧,api有具体的介绍axios或者是axios中文: 主要讲的就是我自己在第一次使用axios中遇到的问题,及二次封装 先来说说二次封装,之前自己也是网上找了很多同学的封装, ...