/**
- @author Jaesoon
- @version 创建时间:2016年7月6日 下午2:32:54
*/
import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.util.AttributeSet;
import android.view.View;
/**
-
@author Jaesoon
*/
public class PercentProgressView extends View {private float percent = 0f;
private int progressColor = 0xFFEF831E;
private Paint p;/**
-
@param context context
*/
public PercentProgressView(Context context) {
super(context);p = new Paint();
}
/**
-
@param context context
-
@param attrs attrs
*/
public PercentProgressView(Context context, AttributeSet attrs) {
super(context, attrs);p = new Paint();
}
/**
-
@param context context
-
@param attrs attrs
-
@param defStyle defStyle
*/
public PercentProgressView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.db_percent_progress);
int n = a.getIndexCount();for (int i = 0; i < n; i++) {
int attr = a.getIndex(i);
switch (attr) {
case 0:
percent = a.getFloat(attr, 25f);
break;
case 1:
progressColor = a.getColor(attr, Color.RED);
break;
}
}
a.recycle();
p = new Paint();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = getMeasuredWidth();
int height = getMeasuredHeight();
/* 创建画笔 *//* 充满 */ p.setStyle(Paint.Style.FILL); /* 设置画笔的锯齿效果 */ p.setAntiAlias(true); p.setStrokeWidth(height); p.setStrokeCap(Cap.ROUND); // 先画底图 p.setColor(0xFFD5D5D5); canvas.drawLine(height / 2, height / 2, width - height / 2, height / 2, p); if (width * percent / 100 - height / 2 > 0) { p.setColor(progressColor); canvas.drawLine(height / 2, height / 2, width * percent / 100 - height / 2, height / 2, p); }
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}public float getPercent() {
return percent;
}public void setPercent(float percent) {
if (percent > 100) {
percent = 100;
} else if (percent < 0) {
percent = 0;
}
this.percent = percent;
invalidate();
}/**
-
设置当前的百分比
*/
public void setCurrentPercent(float percent) {
Keyframe keyframe1 = Keyframe.ofFloat(0, 0);
Keyframe keyframe2 = Keyframe.ofFloat(1, percent);
PropertyValuesHolder holder = PropertyValuesHolder.ofKeyframe("percent", keyframe1, keyframe2);ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(this, holder);
animator.setDuration(2000);
animator.setInterpolator(new FastOutSlowInInterpolator());
animator.setStartDelay(100);
animator.start();
}
}
-
网友评论