看了鸿洋_的Android 自定义View (三) 圆环交替 等待效果的自定义View,就有了一个能不能实现一个方形的等待效果的想法,于是就尝试的实现了一下;
参考鸿洋_博客地址http://blog.csdn.net/lmj623565791/article/details/24500107
具体效果图:
具体实现代码:
attrs定义属性
<declare-styleable name="CustomProgressBar">
<attr name="first_color" format="color"/>
<attr name="second_color" format="color"/>
<attr name="circle_width" format="dimension"/>
<attr name="speed" format="integer"/>
</declare-styleable>
CustomProgressBar代码:
public class CustomProgressBar extends View{
private static final String TAG = "CustomProgressBar";
private int mFirstColor;
private int mSecondColor;
private int mCircleWidth;
private int mSpeed;
private int mProgress;
private int mProgressLine1;
private int mProgressLine2;
private int mProgressLine3;
private int mProgressLine4;
private int mWidth;
private int mHeight;
/**
* 是否应该开始下一个
*/
private boolean isNext = false;
private Paint mPaint;
public CustomProgressBar(Context context){
this(context,null);
}
public CustomProgressBar(Context context, AttributeSet attrs){
this(context, attrs, 0);
}
public CustomProgressBar(Context context, AttributeSet attrs, int defStyle){
super(context,attrs,defStyle);
/**
* 获取自定义View的属性
*/
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs,R.styleable.CustomProgressBar,defStyle,0);
int number = typedArray.getIndexCount();
for (int i = 0; i < number; i++){
int attr = typedArray.getIndex(i);
switch (attr){
case R.styleable.CustomProgressBar_first_color:
mFirstColor = typedArray.getColor(attr, Color.GREEN);
break;
case R.styleable.CustomProgressBar_second_color:
mSecondColor = typedArray.getColor(attr,Color.RED);
break;
case R.styleable.CustomProgressBar_circle_width:
mCircleWidth = typedArray.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics()));
break;
case R.styleable.CustomProgressBar_speed:
mSpeed = typedArray.getInt(attr,20);
break;
}
}
typedArray.recycle();
mPaint = new Paint();
new Thread()
{
public void run()
{
while (true)
{
if (mProgressLine1 != mWidth){
mProgressLine1++;
}
if (mProgressLine1 == mWidth)
{
if (mProgressLine2 != mHeight){
mProgressLine2++;
}
if (mProgressLine2 == mHeight){
if (mProgressLine3!=0){
mProgressLine3--;
}
if (mProgressLine3 == 0){
if (mProgressLine4 != 0){
mProgressLine4--;
}
if(mProgressLine4==0){
mProgressLine1 = 0;
mProgressLine2 = 0;
mProgressLine3 = getMeasuredWidth();
mProgressLine4 = getMeasuredHeight();
if (!isNext)
isNext = true;
else
isNext = false;
}
}
}
}
postInvalidate();
try
{
Thread.sleep(mSpeed);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
};
}.start();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setStrokeWidth(mCircleWidth);
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.STROKE);
if (!isNext){
mPaint.setColor(mFirstColor);
canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mPaint);
mPaint.setColor(mSecondColor);
Path path = new Path();
path.moveTo(0,0);
path.lineTo(mProgressLine1, 0);
if(mProgressLine1 == mWidth){
path.moveTo(getMeasuredWidth(), 0);
path.lineTo(getMeasuredWidth(), mProgressLine2);
}
if (mProgressLine2 == mHeight){
path.moveTo(getMeasuredWidth(), getMeasuredHeight());
path.lineTo(mProgressLine3, getMeasuredHeight());
}
if (mProgressLine3 == 0){
path.moveTo(0, getMeasuredHeight());
path.lineTo(0, mProgressLine4);
}
path.close();
canvas.drawPath(path, mPaint);
}else{
mPaint.setColor(mSecondColor);
canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mPaint);
mPaint.setColor(mFirstColor);
Path path = new Path();
path.moveTo(0,0);
path.lineTo(mProgressLine1, 0);
if(mProgressLine1 == mWidth){
path.moveTo(getMeasuredWidth(), 0);
path.lineTo(getMeasuredWidth(), mProgressLine2);
}
if (mProgressLine2 == mHeight){
path.moveTo(getMeasuredWidth(), getMeasuredHeight());
path.lineTo(mProgressLine3, getMeasuredHeight());
}
if (mProgressLine3 == 0){
path.moveTo(0, getMeasuredHeight());
path.lineTo(0, mProgressLine4);
}
path.close();
canvas.drawPath(path, mPaint);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
mProgressLine3 = widthSize;
mProgressLine4 = heightSize;
mWidth = widthSize;
mHeight = heightSize;
}
}
自己实现的很简单,就是通过Path类的moveTo,lineTo实现的,如果有什么问题或者有什么更好的实现方式,还请大家多多指教。