欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

自定义View设置转盘

程序员文章站 2022-07-13 15:31:00
...

XML中的代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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_1.customview.CustomView
       android:id="@+id/custom_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <bawei.com.day3_1.customview.CustomInsideView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

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();
    }
    public void initView(){
        paint=new Paint();
        paint.setColor(Color.RED);
        paint.setStrokeWidth(20);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextSize(60);

        rectF=new RectF();
        rectF.top=100;
        rectF.left=350;
        rectF.bottom=400;
        rectF.right=550;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(Color.BLACK);
        canvas.drawArc(rectF,60,60,true,paint);//设置指针
        paint.setColor(Color.RED);
        canvas.drawCircle(450,450,150,paint); //设置圆
        paint.setColor(Color.BLACK);
        canvas.drawText("Start",380,470,paint);//设置字体
    }
}

MainActivity中的代码

public class MainActivity extends AppCompatActivity {

    private CustomView customView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView(savedInstanceState);
    }

    private void initView(Bundle savedInstanceState) {
        customView = findViewById(R.id.custom_view);
        customView.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);
            }
        });
    }
}
相关标签: 自定义View