自定义转盘

效果




     

   


//自定义控件



import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
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;

public class DiscView extends View implements View.OnClickListener {

    private Paint paint;

    private int mWidth,mHeight;

    private boolean isStart = false;

    private String str="start";
    public  int[]  mColor = new int[]{Color.BLACK,Color.BLUE,Color.GREEN,Color.RED,Color.BLUE,Color.RED};
    private String[] contents = new String[]{"美 女","女 神","热 舞","丰 满","性 感","知 性"};
    public DiscView(Context context) {
        this(context,null);
    }

    public DiscView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public DiscView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initView();
        //获取中心点
         DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        int widthPixels = displayMetrics.widthPixels;
        int heightPixels = displayMetrics.heightPixels;
        mWidth = widthPixels/2;
        mHeight=heightPixels/2;


        setOnClickListener(this);
    }

    private void initView() {
        //画笔
        paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(5);

    }



    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(mWidth,mHeight);
        //画一个扇形,指定范围  rect就是整个圆盘的大小  指定上下左右四个角的位置
        RectF rect = new RectF(-240,-240,240,240);
        int start = 60;
        for (int i=0;i<6;i++){
            paint.setColor(mColor[i]);
            //第一个参数 oval 参数的作用是:定义的圆弧的形状和大小的范围
            //第二个参数:float startAngle 这个参数的作用是设置圆弧是从哪个角度来顺时针绘画的
            //第三个参数:float sweepAngle 这个参数的作用是设置圆弧扫过的角度
            //第四个参数:boolean useCenter 这个参数的作用是设置我们的圆弧在绘画的时候,是否经过圆形值得注意的是,这个参数在我们的 mPaint.setStyle(Paint.Style.STROKE); 设置为描边属性的时候,是看不出效果的。
            //第五个参数:Paint paint 画笔
            canvas.drawArc(rect,start*i,60,true,paint);
        }

        //画圆
        paint.setColor(Color.BLUE);
        canvas.drawCircle(0,0,100,paint);

        //写字
        paint.setColor(Color.RED);
        //大小
        paint.setTextSize(50);

        Rect rect1 = new Rect();
        //中心内的字
        paint.getTextBounds("start",0,5,rect1);
        int width = rect1.width();
        int height = rect1.height();
        canvas.drawText("start",-width/2,height/2,paint);

        //字体设置
        RectF rectF1 = new RectF(-150, -150, 150, 150);
        for (int i = 0; i < 6; i++) {
            paint.setColor(Color.BLACK);
            Path path = new Path();
            path.addArc(rectF1,start*i+15,60);
            canvas.drawTextOnPath(contents[i],path,0,0,paint);
        }
    }


    @Override
    public void onClick(View view) {
       if(!isStart){
            isStart = true;
           double random = Math.random();
           RotateAnimation rotateAnimation = new RotateAnimation(0, (float) (720 * random), mWidth, mHeight);
           rotateAnimation.setDuration(8000);
           rotateAnimation.setFillAfter(true);
           rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
               @Override
               public void onAnimationStart(Animation animation) {

               }

               @Override
               public void onAnimationEnd(Animation animation) {
                   isStart = false;
               }

               @Override
               public void onAnimationRepeat(Animation animation) {

               }
           });
           startAnimation(rotateAnimation);
       }

    }
  
}


你可能感兴趣的:(自定义转盘)