android 自定义View(3)圆形View 加速条
2016-07-09 23:49
323 查看
android 自定义View(3)圆形View 加速条
分析加速条 一个圆环两种颜色, 加速的速度 圆环的宽度
firstColor
secondColor
speed
circleWidth;
分析加速条 一个圆环两种颜色, 加速的速度 圆环的宽度
firstColor
secondColor
speed
circleWidth;
<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="firstColor" format="color"></attr> <attr name="secondColor" format="color"></attr> <attr name="circleWidth" format="dimension"></attr> <attr name="speed" format="integer"></attr> <declare-styleable name="CustomProgress"> <attr name="firstColor"></attr> <attr name="secondColor"></attr> <attr name="circleWidth"></attr> <attr name="speed"></attr> </declare-styleable> </resources>
package com.zhy.customview02; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.util.AttributeSet; import android.util.TypedValue; import android.view.View; public class CustomProgress extends View { /** * 第一圈颜色 */ private int firstColor; /** * 第二圈颜色 */ private int secondColor; /** * 圆的宽度 */ private int circleWidth; /** * 速度 */ private int speed; /** * 当前的进度 */ private int curProgress; /** * 画笔 */ private Paint mPaint; boolean isNext = false; public CustomProgress(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomProgress, defStyle, 0); initView(typedArray); } public CustomProgress(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CustomProgress(Context context) { super(context, null); } public void initView(TypedArray typedArray){ int n = typedArray.getIndexCount(); for (int i = 0; i < n; i++) { int attr = typedArray.getIndex(i); switch (attr) { case R.styleable.CustomProgress_firstColor: firstColor = typedArray.getColor(attr, Color.GREEN); break; case R.styleable.CustomProgress_secondColor: secondColor = typedArray.getColor(attr, Color.RED); break; case R.styleable.CustomProgress_speed: speed = typedArray.getInt(attr, 20); break; case R.styleable.CustomProgress_circleWidth: circleWidth = typedArray.getDimensionPixelSize(attr, (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics())); break; default: break; } } typedArray.recycle(); mPaint = new Paint(); new Thread(new Runnable(){ @Override public void run() { while(true){ curProgress += speed; if(curProgress == 360){ curProgress = 0; if(!isNext){ isNext = true; }else { isNext = false; } } postInvalidate(); try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }).start(); } @Override protected void onDraw(Canvas canvas) { int center =getWidth()/2; int radius = center - circleWidth/2; mPaint.setStrokeWidth(circleWidth); mPaint.setStyle(Paint.Style.STROKE); mPaint.setAntiAlias(true); RectF oval = new RectF(center-radius, center-radius, center+radius, center+radius); //canvas.drawArc(oval, 0, 360, false, mPaint); if(isNext){ mPaint.setColor(firstColor); canvas.drawCircle(center, center, radius, mPaint);//画圆 mPaint.setColor(secondColor); canvas.drawArc(oval, -90, curProgress, false, mPaint); }else{ mPaint.setColor(secondColor); canvas.drawCircle(center, center, radius, mPaint);//画圆 mPaint.setColor(firstColor); canvas.drawArc(oval, -90, curProgress, false, mPaint); } } }
相关文章推荐
- Android6.0给开发者带来哪些影响
- Android启动过程深入解析
- Launcher3--抽屉
- Android内核开发:图解Android系统的启动过程
- Android开发之Html类详解
- Fragment详解之五——Fragment间参数传递
- Android开发之TextView高级应用
- Fragment跳转时传递参数及结果回传的方法
- Android开发之SpannableString详解(表情文字)
- ListView加载性能优化之ViewHolder的作用与用法
- Android 如何获取应用通知权限
- Android备忘录1
- How to fix process system isn’t responding on Android?
- android中保存Bitmap图片到指定文件夹中的方法
- Android多媒体研究之mediaservice
- 自己动手编译最新Android源码及SDK
- 【Android基础】趣谈Intent
- 图解 Android 事件分发机制
- 关于android.content.res.Resources$NotFoundException: String resource ID #0x1错误的产生
- 集成第三方推送最佳实践