幸运转盘

在这里插入图片描述

//布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<com.example.zhuanpan.Cccc
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
//创建一个类继承View自定义控件
package com.example.zhuanpan;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;

/**
 * date:2018/11/2
 * author:11(lll)
 * function:幸运转盘
 */
class Cccc extends View implements View.OnClickListener{

    private  int screeWidth;
    private int screeheight;
    private Paint paint;
    private RectF rectF;
    private Paint paint1;
    private boolean isStart = false;
    private RotateAnimation rotateAnimation;

    public Cccc(Context context) {
        this(context,null);
    }

    public Cccc(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,-1);
    }

    public Cccc(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取屏幕相关新信息
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        screeWidth=displayMetrics.widthPixels;
        screeheight = displayMetrics.heightPixels;
        initPaint();
        initAnim();
    }

    private void initAnim() {
        rotateAnimation = new RotateAnimation(0f,360f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        rotateAnimation.setRepeatCount(-1);
        rotateAnimation.setFillAfter(true);
    }

    private void initPaint() {
        rectF = new RectF(100,100,300,300);
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStrokeWidth(2);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setAntiAlias(true);

        paint1 = new Paint();
        paint1.setColor(Color.BLACK);
        paint1.setStrokeWidth(1);
        paint1.setStyle(Paint.Style.STROKE);
        paint1.setAntiAlias(true);
//        PorterDuffXfermode porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(400,400);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        canvas.translate(screeWidth/2,screeheight/2);
        int[] Colors= new int[]{
                Color.RED,Color.GREEN,Color.BLACK,Color.GREEN,Color.YELLOW,Color.GRAY
        };
        String[] name = new String[]{
                "白   色","黑   色","红   色","绿   色","蓝   色","黄   色"
        };
        canvas.drawCircle(200,200,100,paint);

        float start = 60;
        for (int i = 0; i <6 ; i++) {
            paint.setColor(Colors[i]);
            canvas.drawArc(rectF,start*i,60,true,paint);
        }
        for (int i = 0; i < 6; i++) {
            paint1.setColor(Color.WHITE);
            Path path = new Path();
            path.addArc(rectF,(i-1)*60+60,60);
            canvas.drawTextOnPath(name[i],path,35,45,paint1);
        }
        paint1.setColor(Color.BLUE);
        paint1.setStyle(Paint.Style.FILL);
        canvas.drawCircle(200,200,30,paint1);
        paint1.setColor(Color.WHITE);
        canvas.drawText("start",188,205,paint1);
        setOnClickListener(this);
    }


    private void stopAnim() {
        clearAnimation();
    }

    @Override
    public void onClick(View v) {
        if (!isStart) {
            isStart=true;
            rotateAnimation.setDuration(800);
            rotateAnimation.setInterpolator(new LinearInterpolator());

            rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                    mHandler.sendEmptyMessageDelayed(1,3000);

                }

                @Override
                public void onAnimationEnd(Animation animation) {

                    isStart=false;

                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            startAnimation(rotateAnimation);
        }else{
//            isStart=false;
//            setRoundDom();
        }
    }

    private void setRoundDom(){

        double random = Math.random();
        RotateAnimation rotateAnimation2 = new  RotateAnimation(-100, (float) (180*random),
                200, 200);
        rotateAnimation2.setFillAfter(true);
        for (int i = 0; i < 1000; i++) {
            rotateAnimation2.setDuration(200+i);
        }

        startAnimation(rotateAnimation2);
        isStart=false;
    }
    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    setRoundDom();
                    break;
            }
        }
    };
}

猜你喜欢

转载自blog.csdn.net/NorthHar/article/details/83718597