相关的方法:
1、集成view
2、实现3个构造方法(新版本是4个)
3、重写onMeasure,onLayout,onDraw方法
构造方法:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 
 | public MyCustomView(Context context) {this(context, null);
 }
 
 
 
 public MyCustomView(Context context, AttributeSet attrs) {
 this(context, attrs, 0);
 
 }
 
 
 
 public MyCustomView(Context context, AttributeSet attrs, int defStyle) {
 super(context, attrs, defStyle);
 
 }
 
 
 
 
 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MKLoader);
 loaderView.setColor(typedArray.getColor(R.styleable.MKLoader_mk_color, Color.parseColor("#ffffff")));
 typedArray.recycle();
 
 | 
onMeasure
设置自己的宽高
| 12
 
 | 最后要设置,不然就是没有效果的   setMeasuredDimension(measuredWidth, measuredHeight);
 
 | 
onLayout
计算位置,这里view的大小有了,可以使用getWidth(), getHeight()
可以设置动画操作
onDraw
真正画图
| 12
 3
 4
 5
 
 | canvas.save();
 canvas.rotate(45 * i, center.x, center.y);
 circles[i].draw(canvas);
 canvas.restore();
 
 | 
invalidate
手动调用重绘
动画
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 
 | ValueAnimator fadeAnimator = ValueAnimator.ofInt(126, 255, 126);
 fadeAnimator.setRepeatCount(ValueAnimator.INFINITE);
 fadeAnimator.setDuration(1000);
 fadeAnimator.setStartDelay(index * 120);
 fadeAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
 @Override public void onAnimationUpdate(ValueAnimator animation) {
 circles[index].setAlpha((int)animation.getAnimatedValue());
 invalidateListener.reDraw();
 }
 });
 
 fadeAnimator.start();
 
 | 
代码
有注解的代码