public class testViewextends View {
private Paintpaint;
private int view_style;
private int view_color;
private int view_height;
private int view_width;
private int progerss =0;
private int mAnimateValue =0;
private ValueAnimatoranimator;
private RectFmRectF;
private int[]clor = {Color.RED, Color.BLUE, Color.YELLOW, Color.BLACK};
private int[]angel = {200, 40, 20, 10};
public testView(Context context) {
this(context, null);
}
public testView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public testView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAttrs(attrs);
initPaint();
start();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//检查高宽
view_width =resolveSize(view_width, widthMeasureSpec);
view_height =resolveSize(view_height, heightMeasureSpec);
setMeasuredDimension(view_width, view_height);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//圆形角度顺时针0 、 90 、270 、180 、360 startAngle 目标角度 useCenter 中心开始画 true 一般用来画扇形
Log.e("canvas", "canvas");
int i =0;
float start =270f;
for (int paintColor :clor) {
paint.setColor(paintColor);
float end =angel[i] *1.0f /270 *360;
canvas.drawArc(mRectF, start, end, true, paint);
start += end;
i++;
}
}
public void initPaint() {
paint =new Paint();
paint.setAntiAlias(true);
//画笔颜色
paint.setColor(view_color);
// 设置空心
// paint.setStyle(Paint.Style.STROKE);
mRectF =new RectF(0, 0, view_width, view_height);
}
public void initAttrs(AttributeSet attrs) {
TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.testView);
view_color = array.getColor(R.styleable.testView_view_color, Color.BLUE);
view_style = array.getColor(R.styleable.testView_view_style, 0);
view_width = array.getDimensionPixelSize(R.styleable.testView_view_height, getContext().getResources().getDimensionPixelOffset(R.dimen.x50));
view_height = array.getDimensionPixelSize(R.styleable.testView_view_height, getContext().getResources().getDimensionPixelOffset(R.dimen.x50));
array.recycle();
}
public void setProgerss(int pro) {
progerss = pro;
invalidate();
}
//设置宽
public void setWidth(int size) {
view_width = size;
//重新绘制高度 onMeasure、onLayout、onDraw(view位置改变会调用)
requestLayout();
}
private ValueAnimator.AnimatorUpdateListenermUpdateListener =new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
//监听动画根据设置的值来决定
mAnimateValue = (int) animation.getAnimatedValue();
//非UI postInvalidate();
//刷新重绘试图UI
// invalidate();
}
};
public void start() {
if (animator ==null) {
animator = ValueAnimator.ofInt(0, 360);
animator.addUpdateListener(mUpdateListener);
animator.setRepeatMode(ValueAnimator.RESTART);
animator.setDuration(6000);
animator.start();
}else if (!animator.isStarted()) {
animator.start();
}
}
public void stop() {
if (animator !=null) {
animator.removeUpdateListener(mUpdateListener);
animator.removeAllUpdateListeners();
animator.cancel();
animator =null;
}
}
}
网友评论