Android学习之自定义圆环 颜色交替
原文出处:http://blog.csdn.net/lmj623565791/article/details/24500107
1、自定义View的属性
``
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="firstColor" format="integer"/>
<attr name="secondColor" format="integer"/>
<attr name="widthCircle" format="dimension"/>
<attr name="speed" format="integer"/>
<declare-styleable name="customCircleView">
<attr name="firstColor"></attr>
<attr name="secondColor" />
<attr name="widthCircle"></attr>
<attr name="speed" />
</declare-styleable>
</resources>
2、在View的构造方法中获得我们自定义的属性
public CircleProgressBar(Context context) {
this(context,null);
}
public CircleProgressBar(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public CircleProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//获取自定义属性的值
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.customCircleView,defStyleAttr,0);
int typeSize = typedArray.getIndexCount();
for(int i = 0; i<typeSize;i++){
int type = typedArray.getIndex(typeSize);
switch (type){
case R.styleable.customCircleView_firstColor:
firstColor = typedArray.getColor(type, Color.BLUE);
break;
case R.styleable.customCircleView_secondColor:
secondColor = typedArray.getColor(type,Color.GREEN);
break;
case R.styleable.customCircleView_widthCircle:
widthCircle = (int) typedArray.getDimension(type, TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_PX,20,getResources().getDisplayMetrics()));
break;
case R.styleable.customCircleView_speed:
speed = typedArray.getInt(type,20);
break;
}
}
[ 3、重写onMesure ]
4、重写onDraw
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//获取圆心的x坐标
int center = getWidth()/2;
//获取半径,
int radius = center-widthCircle;
//开始画圆环
paint.setStrokeWidth(widthCircle);//设置圆环的外围宽度
paint.setAntiAlias(true);//消除锯齿
paint.setStyle(Paint.Style.STROKE);//设置空心
RectF rectF = new RectF(center-radius,center-radius,center+radius,center+radius);//控制圆环left top,right,bottom位置
if(!isNext){
//第一种颜色旋转
paint.setColor(firstColor);//设置圆环颜色
canvas.drawCircle(center,center,radius,paint);//画出圆环
paint.setColor(secondColor);
//根据进度画圆弧
canvas.drawArc(rectF,-90,currProgress,false,paint);
}else {
paint.setColor(secondColor);
canvas.drawCircle(center,center,radius,paint);
paint.setColor(firstColor);
canvas.drawArc(rectF,-90,currProgress,false,paint);
}
}
/**
- 自定义圆环
- Created by JamesZhang on 2017/4/30.
*/
public class CircleProgressBar extends View {
private int firstColor;//第一种颜色
private int secondColor;//第二种颜色
private int widthCircle;//圆环宽度
private int speed;//速度
private Paint paint;//画笔
private int currProgress;//当前进度
private boolean isNext;//是否下一轮
public CircleProgressBar(Context context) {
this(context,null);
}
public CircleProgressBar(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public CircleProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//获取自定义属性的值
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.customCircleView,defStyleAttr,0);
int typeSize = typedArray.getIndexCount();
for(int i = 0; i<typeSize;i++){
int type = typedArray.getIndex(typeSize);
switch (type){
case R.styleable.customCircleView_firstColor:
firstColor = typedArray.getColor(type, Color.BLUE);
break;
case R.styleable.customCircleView_secondColor:
secondColor = typedArray.getColor(type,Color.GREEN);
break;
case R.styleable.customCircleView_widthCircle:
widthCircle = (int) typedArray.getDimension(type, TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_PX,20,getResources().getDisplayMetrics()));
break;
case R.styleable.customCircleView_speed:
speed = typedArray.getInt(type,20);
break;
}
}
typedArray.recycle();
paint = new Paint();
//绘图线程
new Thread(new Runnable() {
@Override
public void run() {
//
while (true){
//进度增加
currProgress++;
//如果当前进度到360,设置进度为0,更换颜色
if(currProgress==360){
currProgress = 0;
if(isNext){
isNext = false;
}else {
isNext = true;
}
postInvalidate();
try {
Thread.sleep(speed);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}).start();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//获取圆心的x坐标
int center = getWidth()/2;
//获取半径,
int radius = center-widthCircle;
//开始画圆环
paint.setStrokeWidth(widthCircle);//设置圆环的外围宽度
paint.setAntiAlias(true);//消除锯齿
paint.setStyle(Paint.Style.STROKE);//设置空心
RectF rectF = new RectF(center-radius,center-radius,center+radius,center+radius);//控制圆环left top,right,bottom位置
if(!isNext){
//第一种颜色旋转
paint.setColor(firstColor);//设置圆环颜色
canvas.drawCircle(center,center,radius,paint);//画出圆环
paint.setColor(secondColor);
//根据进度画圆弧
canvas.drawArc(rectF,-90,currProgress,false,paint);
}else {
paint.setColor(secondColor);
canvas.drawCircle(center,center,radius,paint);
paint.setColor(firstColor);
canvas.drawArc(rectF,-90,currProgress,false,paint);
}
}
public int getFirstColor() {
return firstColor;
}
public void setFirstColor(int firstColor) {
this.firstColor = firstColor;
}
public int getSecondColor() {
return secondColor;
}
public void setSecondColor(int secondColor) {
this.secondColor = secondColor;
}
public int getWidthCircle() {
return widthCircle;
}
public void setWidthCircle(int widthCircle) {
this.widthCircle = widthCircle;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public Paint getPaint() {
return paint;
}
public void setPaint(Paint paint) {
this.paint = paint;
}
public int getCurrProgress() {
return currProgress;
}
public void setCurrProgress(int currProgress) {
this.currProgress = currProgress;
}
public boolean isNext() {
return isNext;
}
public void setNext(boolean next) {
isNext = next;
}
}
网友评论