学习篇---幸运转盘

来源:互联网 发布:人工智能的产品有哪些 编辑:程序博客网 时间:2024/06/04 18:26



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 0
原创粉丝点击