Android自定义view--简单转盘

大概效果如下:
Android自定义view--简单转盘_第1张图片
在activity_main.xml中引入该自定义布局:




    

    

创建MyLuckView类,继承View,实现OnClickListener接口:

import android.annotation.SuppressLint;
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.support.annotation.NonNull;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.RotateAnimation;

import java.util.Random;

public class MyLuckView extends View implements View.OnClickListener {
    private String[] contents = new String[]{"李  同  学", "郝  同  学", "吕  同  学", "张  同  学"};
    public int[] colors = new int[]{Color.parseColor("#8EE5EE"), Color.parseColor("#FFD700"), Color.parseColor("#FFD39B"), Color.parseColor("#FF8247")};
    private Context mContext;
    private Paint mPaint;
    private int mWidth;
    private String mStr = "start";
    private int startdj;

    public MyLuckView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mPaint = new Paint();
        setOnClickListener(this);
    }

    //到从到外层写到里层 不然会发生覆盖效果
    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //外面大圆  画型 可不写
        getBigCircle(canvas);

        //因为要画扇形 里面有个RectF
        //因为那个园其实是占全屏的,所以我这个RectF的空间也是全屏
        RectF rectF = getPaintArc(canvas);
        //转盘中的字体
        getPaintText(canvas, rectF);

        getCenter(canvas);
    }

    private void getCenter(Canvas canvas) {
        mPaint.setColor(Color.GREEN);
        canvas.drawCircle(mWidth / 2, mWidth / 2, 40, mPaint);
        //这是中心的圆中的文字  如果是插图的话 文字可不写
//        mPaint.setColor(Color.BLACK);
//        mPaint.setTextSize(24);
//        //咱们在最中心的位置画一个start (150,150) 我们要得到我们写的字的高和宽
//        Rect rect = new Rect();
//        mPaint.getTextBounds(mStr, 0, mStr.length(), rect);
//        int width = rect.width();
//        int height = rect.height();
//        canvas.drawText(mStr, mWidth / 2 - width / 2, mWidth / 2 + height / 2, mPaint);
    }

    private void getPaintText(Canvas canvas, RectF rectF) {
        mPaint.setColor(Color.BLACK);
        mPaint.setTextSize(35);
        for (int i = 0; i < contents.length; i++) {
            Path path = new Path();
            path.addArc(rectF, i * 90, 90);
            canvas.drawTextOnPath(contents[i], path, 70, 70, mPaint);
        }
    }

    @NonNull
    private RectF getPaintArc(Canvas canvas) {
        RectF rectF = new RectF(0, 0, mWidth, mWidth);
        //实心圆
        mPaint.setStyle(Paint.Style.FILL);
        for (int i = 0; i < colors.length; i++) {
            mPaint.setColor(colors[i]);
            canvas.drawArc(rectF, i * 90, 90, true, mPaint);
        }
        return rectF;
    }

    private void getBigCircle(Canvas canvas) {
        mPaint.setColor(Color.YELLOW);
        //空心圆
        mPaint.setStyle(Paint.Style.STROKE);
        //设置边缘锯齿
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(3);
        Log.e("width", mWidth + "");
        canvas.drawCircle(mWidth / 2, mWidth / 2, mWidth / 2, mPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(400, 400);
        mWidth = getMeasuredWidth();
    }
  @Override
    public void onClick(View v) {
        int nextInt = new Random().nextInt(1000);
       int last= new Random().nextInt(500)+300;
        RotateAnimation rotateAnimation = new RotateAnimation(startdj, nextInt+last, mWidth / 2, mWidth / 2);
        rotateAnimation.setDuration(3000);
        //保留最后执行完的位置
        rotateAnimation.setFillAfter(true);
        startAnimation(rotateAnimation);
        //动画结束后保存结束的位置,在下次的时候不回来原来的位置
        startdj = ( nextInt+last )% 360;
   }
}

效果中有点小瑕疵
以上代码完成 学艺浅薄 如造成困扰很抱歉

你可能感兴趣的:(Android自定义view--简单转盘)