自定义View 圆盘

布局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);
            }
        });

    }
}

 效果图 点中间小圆 外面大圆旋转

效果图

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值