本文实例为大家分享了Android自定义View实现抽奖转盘的具体代码,供大家参考,具体内容如下
public class LuckCircle extends SurfaceView implements SurfaceHolder.Callback,Runnable { private SurfaceHolder mHolder; private Canvas mCanvas; //用于绘制的线程 private Thread mThread; //线程开关的控制 private boolean isRunning; private String[] mStr = new String[]{"优惠券","十元话费","恭喜发财","恭喜发财","英雄皮肤","50M流量"}; //物品的图片 private int[] mImgs = new int[]{R.mipmap.ic_launcher, R.mipmap.ic_launcher,R.mipmap.ic_launcher, R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher}; private int mItemCount = 6; //盘快的颜色 private int[] mColor = new int[]{0xffffc300,0xFFD9B114,0xFFDC0B2E,0xFF5510A4,0xFF447C42,0xFFEC3636}; //与图片对应的bitmap数组 private Bitmap[] mImgBitmap; //整个盘块的范围 private RectF mRange = new RectF(); //整个盘块的直径 private int mRadius; //绘制盘块的画笔 private Paint mArcPaint; //绘制文本的画笔 private Paint mTextPaint; //滚动速度 private double mSpeed = 10; //绘制的角度 private volatile int mStartAngle = 0; //判断是否点击了停止按钮 private boolean isShouldEnd; //转盘的中心位置 private int mCenter; //padding取四个padding中的最小值 private int mPadding; //背景图 //private Bitmap mBgBitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher); private float mTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,20,getResources().getDisplayMetrics()); public LuckCircle(Context context) { this(context,null); } public LuckCircle(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 holder) { // 初始化绘制盘块的画笔 mArcPaint = new Paint(); mArcPaint.setAntiAlias(true); mArcPaint.setDither(true); // 初始化绘制盘块的画笔 mTextPaint = new Paint(); mTextPaint.setColor(0XFF0B25CF); mTextPaint.setTextSize(mTextSize); // 初始化盘块绘制的范围 mRange = new RectF(mPadding,mPadding,mPadding+mRadius,mPadding+mRadius); // 初始化图片 mImgBitmap = new Bitmap[mItemCount]; for (int i = 0; i
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。