学习篇---幸运转盘



import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Created by Administrator on 2016/5/23.
 */
public class LuckyPan extends SurfaceView implements SurfaceHolder.Callback,Runnable {
    private SurfaceHolder surfaceHolder;//与SurfaceView绑定的SurfaceHolder
    private Thread surfaceThread;//用于绘制的线程
    private Canvas surfaceCanvas;//与SurfaceView绑定的Canvas
    private boolean isRunning;//转盘的状态
    private Bitmap bgBitmap;//背景图
    private RectF bgRectf;//绘制背景用的RectF
    private RectF arcRectF;//绘制扇形用的RectF
    private RectF btRecF;//绘制奖品用的RectF
    private float startAngle;//开始的角度
    private float sweepAngle;//滑过的角度
    private float tmpAngle;//

    private int count;//奖品的数量
    private boolean isFirstMeasure = true;
    private float radius;//圆的半径
    private Paint arcPaint;//绘制弧形块
    private Paint textpaint;//绘制text
    private Path arcPath;//绘制弧线的path

    private float speed = 0;//转盘旋转的速度

    public boolean isbegin = false;//记录是否开始

    private float btCenterX;//奖品图片中心点的x坐标
    private float btCenterY;//奖品图片中心点的y坐标
    private int[] colors = new int[]{0xFFFFC300, 0xFFF17E01, 0xFFFFC300, 0xFFF17E01, 0xFFFFC300, 0xFFF17E01};//存放颜色

    private String[] texts = new String[]{"单反相机", "IPAD", "恭喜发财", "IPHONE", "妹子一只", "恭喜发财"};//存放文本

    private int pic[] = new int[]{R.drawable.danfan, R.drawable.ipad, R.drawable.f040, R.drawable.iphone, R.drawable.meizi, R.drawable.f040};
    private Bitmap bt[];

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

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

    public LuckyPan(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取SurfaceHolder
        surfaceHolder = getHolder();
        surfaceHolder.addCallback(this);
        bgBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg2);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        isRunning = true;
        surfaceThread = new Thread(this);
        surfaceThread.start();

        //画笔初始化
        arcPaint = new Paint();
        arcPaint.setAntiAlias(true);
        arcPaint.setDither(true);

        textpaint = new Paint();
        textpaint.setTextSize(45);
        textpaint.setColor(Color.WHITE);

        count = colors.length;
        bt = new Bitmap[count];
        for (int i = 0; i < count; i++) {
            bt[i] = BitmapFactory.decodeResource(getResources(), pic[i]);
        }
        sweepAngle = tmpAngle = 360f / count;
    }


    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        isRunning = false;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);

            int result = Math.min(getMeasuredHeight(), getMeasuredWidth());
           // Log.e("TAG", result + "");
            setMeasuredDimension(result, result);  //设置测量值

            radius = (result - getPaddingLeft() - getPaddingRight()) / 2f;
            bgRectf = new RectF(getPaddingLeft(), getPaddingTop(), radius * 2 + getPaddingRight(), radius * 2 + getPaddingTop());
            arcRectF = new RectF(getPaddingLeft() + 30, getPaddingTop() + 30, radius * 2 + getPaddingRight() - 30, radius * 2 + getPaddingTop() - 30);


    }

    @Override
    public void run() {
        while (isRunning) {
            draw();
        }

    }

    private void draw() {
        try {
            surfaceCanvas = surfaceHolder.lockCanvas();
            if (surfaceCanvas != null) {

                drawBg();
                drawArc(tmpAngle);
                drawArcText(tmpAngle);
                drawPic(tmpAngle);

                tmpAngle += speed;
                if (!isbegin) {
                    speed--;
                }
                if (speed < 0) {
                    speed = 0;
                }
            }
        } catch (Exception e) {

        } finally {
            if (surfaceCanvas != null)
                surfaceHolder.unlockCanvasAndPost(surfaceCanvas);
        }
    }

    /**
     * 画奖品
     */
    private void drawPic(float tmpAngle) {
        for (int i = 0; i < count; i++) {
            startAngle = tmpAngle;
            float btWidth = radius / 6f;
            btCenterX = (float) (radius + getPaddingLeft() + Math.cos((startAngle + sweepAngle / 2f) * Math.PI / 180) * radius / 2f);
            btCenterY = (float) (radius + getPaddingTop() + Math.sin((startAngle + sweepAngle / 2f) * Math.PI / 180) * radius / 2f);
            btRecF = new RectF(btCenterX - btWidth / 2f, btCenterY - btWidth / 2, btCenterX + btWidth / 2f, btCenterY + btWidth / 2);
            surfaceCanvas.drawBitmap(bt[i], null, btRecF, null);
            tmpAngle += sweepAngle;
        }

    }

    /**
     * 画文本
     */
    private void drawArcText(float tmpAngle) {

        for (int i = 0; i < count; i++) {
            startAngle = tmpAngle;
            float textWidth = textpaint.measureText(texts[i]);//测量字体的宽度
            arcPath = new Path();
            arcPath.addArc(arcRectF, startAngle, sweepAngle);
            surfaceCanvas.drawTextOnPath(texts[i], arcPath, (float) ((Math.PI * 2 * radius / count - textWidth) / 2f), radius / 6f, textpaint);
            tmpAngle += sweepAngle;
        }

    }

    /**
     * 画弧形块
     */
    private void drawArc(float tmpAngle) {


        for (int i = 0; i < count; i++) {
            startAngle = tmpAngle;
            arcPaint.setColor(colors[i]);
            surfaceCanvas.drawArc(arcRectF, startAngle, sweepAngle, true, arcPaint);
            tmpAngle += sweepAngle;
        }

    }

    /**
     * 绘制背景
     */
    private void drawBg() {
        surfaceCanvas.drawColor(0xffffffff);
        surfaceCanvas.drawBitmap(bgBitmap, null, bgRectf, null);
    }

    public void start() {
        speed = 30;
        isbegin = true;
    }

    public void stop() {

        isbegin = false;
    }
}

参考博客地址: 文章地址


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值