public class MyCricleView extends View {
private int borderColor ; //圆圈的颜色
private int defaultColor ; //保存圆圈或切换圆圈的颜色
private float r = 100 ; //半径
private float paintX ; //圆心X
private float paintY ; //圆形Y
private Paint paintCricle ;
private float borderWidth ; //圆圈的宽度
private int index = 1 ;
private int speed = 1 ;
private boolean canRotate = true;
public MyCricleView (Context context) {
super (context);
initView(context, null );
}
public MyCricleView (Context context, @Nullable AttributeSet attrs) {
super (context, attrs);
initView(context, attrs);
}
private void initView (Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyCricleView );
borderColor = typedArray.getColor(R.styleable.MyCricleView_BorderColor , Color.YELLOW );
defaultColor = borderColor ;
borderWidth = typedArray.getDimension(R.styleable.MyCricleView_BorderWidth , 1 );
}
//改变圆圈的颜色
public void setBorderColor (int color) {
if (defaultColor == color) {
defaultColor = borderColor ;
} else {
defaultColor = color;
}
}
//加速
public void speed (int sp) {
speed = sp;
}
//减速
public void slow_down (int sp) {
speed = sp;
}
@Override
protected void onDraw (Canvas canvas) {
super .onDraw(canvas);
paintX = getWidth() / 2 ;
paintY = getHeight() / 2 ;
//设置圆圈的画笔
paintCricle = new Paint();
paintCricle .setColor(defaultColor );
paintCricle .setStyle(Paint.Style.STROKE );
paintCricle .setStrokeWidth(borderWidth );
paintCricle .setAntiAlias(true );
//画一个圆圈
canvas.drawCircle(paintX , paintY , r , paintCricle );
//保存状态
canvas.save();
//让画布旋转
canvas.rotate(speed * index , paintX , paintY );
Path path = new Path();
path.moveTo(paintX + r , paintY );
path.lineTo(paintX + r - 20 , paintY - 20 );
path.lineTo(paintX + r , paintY + 20 );
path.lineTo(paintX + r + 20 , paintY - 20 );
path.close();
Paint paint = new Paint();
paint.setColor(Color.BLACK );
paint.setStyle(Paint.Style.FILL );
paint.setAntiAlias(true );
canvas.drawPath(path, paint);
canvas.restore();
//因为旋转是按度数去旋转了,所以得让他自动++
++index ;
//如果是true的时候让他重绘
if (canRotate ) {
invalidate();
}
}
//暂停/继续
public void StopRestart () {
if (canRotate ) {
canRotate = !canRotate ;
} else {
canRotate = !canRotate ;
invalidate();
}
}
}
//在values文件夹下新建attrs自定义属性
< declare-styleable name= "MyCricleView" >
<attr name= "BorderColor" format= "color" ></attr>
<attr name= "BorderWidth" format= "dimension" ></attr>
</ declare-styleable >
//引用 public class MainActivity extends AppCompatActivity {
private MyCustomView viewById ;
private int speed = 1 ;
private MyCricleView cricleView ;
@Override
protected void onCreate (Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main );
cricleView = (MyCricleView) findViewById(R.id.cricleView );
}
//改变颜色的点击事件
public void changeColor (View view) {
cricleView .setBorderColor(Color.BLUE );
}
//加速
public void expedite (View view) {
if (speed >= 5 )
Toast.makeText (this, "我已经快如闪电" , Toast.LENGTH_SHORT ).show();
speed += 2 ;
cricleView .speed(speed );
}
//减速
public void slow_down (View view) {
speed -= 2 ;
if (speed <= 0 ) {
speed = 1 ;
cricleView .slow_down(speed );
}
}
//暂停,继续
public void stop_restart (View view) {
cricleView .StopRestart();
}