自定义view圆环的改变
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义view圆环的改变相关的知识,希望对你有一定的参考价值。
//次线程更新ui
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
invalidate();
if (du>=360){
timer.cancel();
}
}
};
Timer timer = null;
int outColor = Color.GRAY;
float du = 0;
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onCreateContextMenu(ContextMenu menu) {
super.onCreateContextMenu(menu);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//最上面的字
Paint p1 = new Paint();
p1.setColor(Color.BLACK);
p1.setTextSize(32);
canvas.drawText("改变外层圆环的颜色",0,32,p1);
//最外面的园
Paint p2 = new Paint();
p2.setColor(outColor);
canvas.drawCircle(getWidth()/2,getHeight()/2,100,p2);
//进度
Paint p5 = new Paint();
p5.setColor(Color.RED);
RectF rectF = new RectF(getWidth()/2-100,getHeight()/2-100,getWidth()/2+100,getHeight()/2+100);
canvas.drawArc(rectF,0,du,true,p5);
//中心的白色园
Paint p3 = new Paint();
p3.setColor(Color.WHITE);
canvas.drawCircle(getWidth()/2,getHeight()/2,50,p3);
//中间的文字
Paint p4 = new Paint();
p4.setColor(Color.BLACK);
canvas.drawText("78%",getWidth()/2,getHeight()/2,p4);
//开始
Paint p6 = new Paint();
p6.setColor(Color.BLACK);
p6.setTextSize(32);
canvas.drawText("开始",0,getHeight()-100,p6);
//重置
Paint p7 = new Paint();
p7.setColor(Color.BLACK);
p7.setTextSize(32);
canvas.drawText("重置",getWidth()/2,getHeight()-100,p6);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
if (event.getY() <= 32) {//更改上面的文字为绿色
outColor = Color.GREEN;
invalidate();
}
if (event.getX() < getWidth()/2 && event.getY()<getHeight()-100 && event.getY()>getHeight()-132){
//Log.d("mylog","开始");
//点击开始后启动timer不断的给du进行++
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
du += 12;
Log.d("mylog","度数:"+du);
handler.sendEmptyMessage(1);
}
},1000,1000);
}
if (event.getX() >= getWidth()/2 && event.getY()<getHeight()-100 && event.getY()>getHeight()-132){
//Log.d("mylog","重置");
//重置,把所有的变量回归初始值,更新界面,关闭timer
du = 0;
outColor = Color.GRAY;
invalidate();
timer.cancel();
}
}
return super.onTouchEvent(event);
}
}
以上是关于自定义view圆环的改变的主要内容,如果未能解决你的问题,请参考以下文章
Android进阶——自定义View之自己绘制彩虹圆环调色板