[Android][抽奖转盘]⑤---控制停止位置

[Android][抽奖转盘]⑤---控制停止位置_第1张图片
QQ截图20170323212323.png

修改代码

  /**
     * 点击启动旋转
     * @param index 停下来的位置
     */
    public void luckyStart(int index)
    {
        //计算每一项的角度
        float angle=360/mItemCount;

        //计算每一项中奖范围(当前index )
        //1->150~210
        //0->210~270
        float from=270-(index+1)*angle;
        float end=from+angle;

        //设置停下来需要旋转的距离
        float targetFrom=4*360+from;
        //每次旋转的角度不同
        float targetEnd=4*360+end;

        /**
         * 
         *   v1->0 每次-1
         *   等差数列 v1+1转成int
         *   (v1+0)*(v1+1)/2=targetFrom;
         *      v1*v1+v1-2*targetFrom=0;
         *      v1=(-1+Math.sqrt(1+8*targetFrom))/2
         * 
*/ float v1= (float) ((-1+Math.sqrt(1+8*targetFrom))/2); float v2= (float) ((-1+Math.sqrt(1+8*targetEnd))/2); mSpeed=v1+Math.random()*(v2-v1); isShouldEnd=false; } public void luckyEnd(){ //开始角度置0 mStartAngle=0; isShouldEnd=true; }
 private volatile float mStartAngle=0;

完整代码

package myapplication2.xt.com.myapplication2;

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

/**
 * Created by TONG on 2017/3/22.
 */

public class LuckyPan extends SurfaceView implements SurfaceHolder.Callback,Runnable {

    private SurfaceHolder mHolder;
    private Canvas mCanvas;
    /**
     * 用于绘制的线程
     */
    private Thread t;
    /**
     * 线程的控制开关
     */
    private boolean isRunning;

    /**
     * 盘块的奖项
     */
    private String[] mStrs=new String[]{"单反相机","IPAD","恭喜发财","IPHONE","服装一套","恭喜发财"};
    /**
     * 盘块的图片
     */
    private int[] mImgs=new int[]{R.mipmap.danfan, R.mipmap.ipad, R.mipmap.f040, R.mipmap.iphone, R.mipmap.meizi, R.mipmap.f040};
    /**
     * 与图片对应的bitmap数组
     */
    private Bitmap[] mImgBitmap;

    private Bitmap mBigBitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.bg2);

    /**
     * 盘块的颜色
     */
    private int[] mColors =new int[]{0XFFFFC300, 0XFFF17E01, 0XFFFFC300, 0XFFF17E01, 0XFFFFC300, 0XFFF17E01
    };

    private int mItemCount=6;

    /**
     * 绘制盘块的画笔
     */
    private Paint mArcPaint;
    /**
     * 绘制文本的画笔
     */
    private Paint mTextPaint;

    /**
     * 盘块的背景图
     * RectF与Rect区别:1、精度不一样,Rect是使用int类型作为数值,RectF是使用float类型作为数值
     2、两个类型提供的方法也不是完全一致
     */
    private RectF mRange=new RectF();
    /**
     * 整个盘块的直径
     */
    private int mRadius;

    /**
     * 文字大小 20sp
     */
    private float mTextSize= TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,20,getResources().getDisplayMetrics());

    /**
     * 滚动的速度
     */
    private double mSpeed=0;
    /**
     *
     * 可能有两个线程操作
     * 保证线程间变量的可见性
     */
    private volatile float mStartAngle=0;
    /**
     * 是否点击了停止按钮
     */
    private boolean isShouldEnd;
    /**
     * 转盘的中心位置
     */
    private int mCenter;
    /**
     * 这里我们的padding直接以paddingLeft为准
     */
    private int mPadding;


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

    public LuckyPan(Context context, AttributeSet attrs) {
        super(context, attrs);
        mHolder=getHolder();
        mHolder.addCallback(this);
        //获得焦点
        setFocusable(true);
        setFocusableInTouchMode(true);
        //设置常亮
        setKeepScreenOn(true);
    }

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

        int width=Math.min(getMeasuredWidth(),getMeasuredHeight());

        mPadding=getPaddingLeft();
        //直径
        mRadius=width-mPadding*2;
        //中心点
        mCenter=width/2;
        setMeasuredDimension(width,width);
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {

        //初始化绘制盘块的画笔
        mArcPaint=new Paint();
        //防止边缘的锯齿
        mArcPaint.setAntiAlias(true);
        //设置递色
        mArcPaint.setDither(true);

        //初始化绘制文本的画笔
        mTextPaint=new Paint();
        mTextPaint.setColor(0xffffffff);
        mTextPaint.setTextSize(mTextSize);

        //初始化盘块绘制的范围
        mRange=new RectF(mPadding,mPadding,mPadding+mRadius,mPadding+mRadius);

        //初始化图片
        mImgBitmap=new Bitmap[mItemCount];

        for(int i=0;i150~210
        //0->210~270
        float from=270-(index+1)*angle;
        float end=from+angle;

        //设置停下来需要旋转的距离
        float targetFrom=4*360+from;
        //每次旋转的角度不同
        float targetEnd=4*360+end;

        /**
         * 
         *   v1->0 每次-1
         *   等差数列 v1+1转成int
         *   (v1+0)*(v1+1)/2=targetFrom;
         *      v1*v1+v1-2*targetFrom=0;
         *      v1=(-1+Math.sqrt(1+8*targetFrom))/2
         * 
*/ float v1= (float) ((-1+Math.sqrt(1+8*targetFrom))/2); float v2= (float) ((-1+Math.sqrt(1+8*targetEnd))/2); mSpeed=v1+Math.random()*(v2-v1); isShouldEnd=false; } public void luckyEnd(){ //开始角度置0 mStartAngle=0; isShouldEnd=true; } /** * 转盘是否在旋转 * @return */ public boolean isStart(){ return mSpeed!=0; } public boolean isShouldEnd(){ return isShouldEnd; } /** * 绘制Icon * @param tmpAngle * @param bitmap */ private void drawIcon(float tmpAngle, Bitmap bitmap) { //设置图片的宽度为直径1/8 int imgWidth=mRadius/8; //PI/180 ->一度 //angle弧度值 float angle = (float) ((tmpAngle+360/mItemCount/2)*Math.PI/180); int x= (int) (mCenter+mRadius/2/2*Math.cos(angle)); int y= (int) (mCenter+mRadius/2/2*Math.sin(angle)); //确定图片位置 Rect rect=new Rect(x-imgWidth/2,y-imgWidth/2,x+imgWidth,y+imgWidth/2); mCanvas.drawBitmap(bitmap,null,rect,null); } /** * 绘制每个盘块的文本(弧形) * @param tmpAngle * @param sweepAngle * @param mStr */ private void drawText(float tmpAngle, float sweepAngle, String mStr) { Path path=new Path(); path.addArc(mRange,tmpAngle,sweepAngle); float textWidth=mTextPaint.measureText(mStr); //水平偏移量 mRadius是直径 int hOffset= (int) (mRadius*Math.PI/mItemCount/2-textWidth/2); //垂直偏移量 int vOffset=mRadius/2/6; //第三个参数:水平偏移量 //第四个参数:垂直偏移量 mCanvas.drawTextOnPath(mStr,path,hOffset,vOffset,mTextPaint); } /** * 绘制背景 */ private void drawBg() { mCanvas.drawColor(0xFFFFFFFF); mCanvas.drawBitmap(mBigBitmap,null, new Rect(mPadding/2,mPadding/2,getMeasuredWidth()-mPadding/2, getMeasuredHeight()-mPadding/2),null); } }

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private LuckyPan mLuckPan;
    private ImageView mStartBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mLuckPan= (LuckyPan) findViewById(R.id.id_luckypan);
        mStartBtn= (ImageView) findViewById(R.id.id_start_btn);

        mStartBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!mLuckPan.isStart())
                {
                    mLuckPan.luckyStart(1);
                    mStartBtn.setImageResource(R.mipmap.stop);
                }else {
                    if(!mLuckPan.isShouldEnd())
                    {
                        mLuckPan.luckyEnd();
                        mStartBtn.setImageResource(R.mipmap.start);
                    }
                }
            }
        });
    }
}

[Android][抽奖转盘]⑤---控制停止位置_第2张图片
Paste_Image.png

你可能感兴趣的:([Android][抽奖转盘]⑤---控制停止位置)