Android學習之自定義圓環(huán) 顏色交替
原文出處: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;
//開始畫圓環(huán)
paint.setStrokeWidth(widthCircle);//設置圓環(huán)的外圍寬度
paint.setAntiAlias(true);//消除鋸齒
paint.setStyle(Paint.Style.STROKE);//設置空心
RectF rectF = new RectF(center-radius,center-radius,center+radius,center+radius);//控制圓環(huán)left top恋昼,right徘郭,bottom位置
if(!isNext){
//第一種顏色旋轉(zhuǎn)
paint.setColor(firstColor);//設置圓環(huán)顏色
canvas.drawCircle(center,center,radius,paint);//畫出圓環(huán)
paint.setColor(secondColor);
//根據(jù)進度畫圓弧
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);
}
}
/**
- 自定義圓環(huán)
- Created by JamesZhang on 2017/4/30.
*/
public class CircleProgressBar extends View {
private int firstColor;//第一種顏色
private int secondColor;//第二種顏色
private int widthCircle;//圓環(huán)寬度
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;
//開始畫圓環(huán)
paint.setStrokeWidth(widthCircle);//設置圓環(huán)的外圍寬度
paint.setAntiAlias(true);//消除鋸齒
paint.setStyle(Paint.Style.STROKE);//設置空心
RectF rectF = new RectF(center-radius,center-radius,center+radius,center+radius);//控制圓環(huán)left top,right弦叶,bottom位置
if(!isNext){
//第一種顏色旋轉(zhuǎn)
paint.setColor(firstColor);//設置圓環(huán)顏色
canvas.drawCircle(center,center,radius,paint);//畫出圓環(huán)
paint.setColor(secondColor);
//根據(jù)進度畫圓弧
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;
}
}