布局activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.example.paint.view.CustomDiskView
android:id="@+id/disk"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<com.example.paint.view.CustomBeginView
android:id="@+id/begin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/disk"
android:layout_marginLeft="160dp"
android:layout_marginTop="145dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
外面的大圆盘
public class CustomDiskView extends View {
Paint paint;
int degress=0;
RectF rectF;
int textdegress=15;
public CustomDiskView(Context context) {
super(context);
}
public CustomDiskView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//外边大圆盘
String[] strings=new String[]{"一","二","三","四","五","六"};//定义文字
int[] colors=new int[]{Color.YELLOW,Color.DKGRAY,Color.CYAN,Color.GREEN,Color.LTGRAY,Color.BLUE};//定义颜色
for(int i=0;i<6;i++){
paint.setColor(colors[i]);//每个扇形的颜色都不相同
Path path=new Path();
paint.setTextSize(24);//设置文字的大小
canvas.drawArc(rectF,degress,60,true,paint);
path.addArc(rectF,textdegress,60);
paint.setColor(Color.BLACK);//写文字
canvas.drawTextOnPath(strings[i],path,60,60,paint);
degress+=60;
textdegress+=60;
}
}
private void init() {
paint=new Paint();
paint.setStrokeWidth(10);
paint.setStyle(Paint.Style.FILL);
rectF=new RectF();
rectF.left=100;
rectF.right=600;
rectF.top=100;
rectF.bottom=600;
}
}
里面小圆加箭头
public class CustomBeginView extends View {
Paint paint;
public CustomBeginView(Context context) {
super(context);
}
public CustomBeginView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//自定义宽和高
setMeasuredDimension(120,150);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//中心图形 和文字
paint.setColor(Color.BLACK);
canvas.drawArc(0,-120,120,120,60,60,true,paint);
paint.setColor(Color.RED);
canvas.drawCircle(60,90,60,paint);
paint.setColor(Color.BLACK);
canvas.drawText("start",10,100,paint);
}
private void init() {
//实例化
paint=new Paint();
paint.setColor(Color.RED);
paint.setStrokeWidth(10);
paint.setTextSize(50);
paint.setStyle(Paint.Style.FILL);
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
private CustomDiskView customDiskView;
private CustomBeginView customBeginView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customDiskView = findViewById(R.id.disk);
customBeginView = findViewById(R.id.begin);
customBeginView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//随机数旋转
float degress= (float) (720+Math.random()*1000);
//旋转动画
RotateAnimation rotateAnimation = new RotateAnimation(0,degress,350,350);
rotateAnimation.setDuration(5000);//动画时长
rotateAnimation.setFillAfter(true);//保持动画完之后的效果
customDiskView.setAnimation(rotateAnimation);
customDiskView.startAnimation(rotateAnimation);
}
});
}
}
效果图 点中间小圆 外面大圆旋转