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">
<bawei.com.day3_3.customview.CustomView
android:id="@+id/custom_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<bawei.com.day3_3.customview.CustomInsideView
android:id="@+id/inside_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="152dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_editor_absoluteX="147dp"
app:layout_editor_absoluteY="155dp" />
</android.support.constraint.ConstraintLayout>
CustomView中的代码
public class CustomView extends View {
private Paint mPaint;
private int CriCount=6;
private RectF rectF;
private int degree=0;
private int textDegree=15;
public CustomView(Context context) {
super(context);
initView();
}
public CustomView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initView();
}
private int[] colors=new int[]{Color.parseColor("#8EE5EE"), Color.parseColor("#FFD700"), Color.parseColor("#FFD39B"), Color.parseColor("#FF8247"), Color.parseColor("#FF34B3"), Color.parseColor("#F0E68C")};
private String[] str=new String[]{"一等奖","二等奖","三等奖","四等奖","特等奖","谢谢参与奖"};
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i=0;i<CriCount;i++){
mPaint.setColor(colors[i]);//设置扇形颜色
canvas.drawArc(rectF,degree,60,true,mPaint);//设置扇形
mPaint.setColor(Color.BLACK);//设置字体颜色
mPaint.setTextSize(30);//设置字体大小
Path path = new Path();
path.addArc(rectF,textDegree,60);
canvas.drawTextOnPath(str[i],path,60,60,mPaint);//设置字体
degree+=60;
textDegree+=60;
}
}
public void initView(){
mPaint=new Paint();
rectF=new RectF();
rectF.top=100;
rectF.left=100;
rectF.bottom=800;
rectF.right=800;
}
}
CustomInsideView中的代码
public class CustomInsideView extends View {
private Paint paint;
private RectF rectF;
public CustomInsideView(Context context) {
super(context);
initView();
}
public CustomInsideView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initView();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(300,300);
}
public void initView(){
paint=new Paint();
paint.setColor(Color.RED);
paint.setStrokeWidth(10);
paint.setStyle(Paint.Style.FILL);
paint.setTextSize(60);
rectF=new RectF();
rectF.top=0;
rectF.bottom=55;
rectF.left=120;
rectF.right=150;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setColor(Color.RED);
canvas.drawCircle(150,150,100,paint);
paint.setColor(Color.BLACK);
canvas.drawArc(rectF,60,60,true,paint);
paint.setColor(Color.BLACK);
canvas.drawText("开始",90,160,paint);
}
}
MainActivity中的代码
public class MainActivity extends AppCompatActivity {
private CustomInsideView customInsideView;
private CustomView customView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customInsideView = findViewById(R.id.inside_view);
customView = findViewById(R.id.custom_view);
customInsideView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
float degree= (float) (720+Math.random()*1000);
RotateAnimation animation=new RotateAnimation(0,-degree,450,450);
animation.setDuration(5000);
animation.setFillAfter(true);
customView.startAnimation(animation);
}
});
}
}