本文實(shí)例為大家分享了Android自定義View實(shí)現(xiàn)抽獎(jiǎng)轉(zhuǎn)盤的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)新互聯(lián)建站網(wǎng)站建設(shè)由有經(jīng)驗(yàn)的網(wǎng)站設(shè)計(jì)師、開發(fā)人員和項(xiàng)目經(jīng)理組成的專業(yè)建站團(tuán)隊(duì),負(fù)責(zé)網(wǎng)站視覺(jué)設(shè)計(jì)、用戶體驗(yàn)優(yōu)化、交互設(shè)計(jì)和前端開發(fā)等方面的工作,以確保網(wǎng)站外觀精美、網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)易于使用并且具有良好的響應(yīng)性。
public class LuckCircle extends SurfaceView implements SurfaceHolder.Callback,Runnable { private SurfaceHolder mHolder; private Canvas mCanvas; //用于繪制的線程 private Thread mThread; //線程開關(guān)的控制 private boolean isRunning; private String[] mStr = new String[]{"優(yōu)惠券","十元話費(fèi)","恭喜發(fā)財(cái)","恭喜發(fā)財(cái)","英雄皮膚","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}; //與圖片對(duì)應(yīng)的bitmap數(shù)組 private Bitmap[] mImgBitmap; //整個(gè)盤塊的范圍 private RectF mRange = new RectF(); //整個(gè)盤塊的直徑 private int mRadius; //繪制盤塊的畫筆 private Paint mArcPaint; //繪制文本的畫筆 private Paint mTextPaint; //滾動(dòng)速度 private double mSpeed = 10; //繪制的角度 private volatile int mStartAngle = 0; //判斷是否點(diǎn)擊了停止按鈕 private boolean isShouldEnd; //轉(zhuǎn)盤的中心位置 private int mCenter; //padding取四個(gè)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); // 可獲得焦點(diǎn) setFocusable(true); setFocusableInTouchMode(true); // 設(shè)置常亮 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 < mImgBitmap.length; i++) { mImgBitmap[i] = BitmapFactory.decodeResource(getResources(),mImgs[i]); } isRunning = true; mThread = new Thread(this); mThread.start(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { isRunning = false; } @Override public void run() { while(isRunning){ long start = System.currentTimeMillis(); draw(); long end = System.currentTimeMillis(); if (end - start < 50) { try { Thread.sleep(50 - (end - start)); } catch (InterruptedException e) { e.printStackTrace(); } } } } private void draw(){ try { mCanvas = mHolder.lockCanvas(); if (mCanvas != null) { // 繪制背景 drawBackground(); // 繪制盤塊 float tmpAngle = mStartAngle; float sweepAngle = 360 /mItemCount; for (int i = 0; i < mItemCount; i++) { mArcPaint.setColor(mColor[i]); // 繪制盤塊 mCanvas.drawArc(mRange,tmpAngle,sweepAngle,true,mArcPaint); // 繪制文本 drawText(tmpAngle,sweepAngle,mStr[i]); // 繪制Icon drawIcon(tmpAngle,mImgBitmap[i]); tmpAngle += sweepAngle; } mStartAngle += mSpeed; // 如果點(diǎn)擊了停止按鈕 if (isShouldEnd) { mSpeed -= 1; } if (mSpeed <= 0) { mSpeed = 0; isShouldEnd = false; } } }catch (Exception e){ }finally { if (mCanvas != null) { // 釋放Canvas mHolder.unlockCanvasAndPost(mCanvas); } } } //點(diǎn)擊啟動(dòng)旋轉(zhuǎn) public void luckyStart(){ mSpeed = 50; isShouldEnd = false; } public void luckEnd(){ isShouldEnd = true; } public boolean isStart(){ return mSpeed != 0; } public boolean isShouldEnd(){ return isShouldEnd; } //繪制Icon private void drawIcon(float tmpAngle, Bitmap bitmap) { // 設(shè)置圖片的寬度為直徑的1/8; int imgWidth = mRadius / 8; 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/2, y + imgWidth/2); mCanvas.drawBitmap(bitmap,null,rect,null); } //繪制每個(gè)盤塊的文本 private void drawText(float tmpAngle, float sweepAngle, String s) { Path path = new Path(); path.addArc(mRange,tmpAngle,sweepAngle); // 利用水平偏移量讓文字居中 float measureText = mTextPaint.measureText(s); int hOffset = (int) (mRadius * Math.PI/mItemCount/2 - measureText/2); int vOffset = mRadius /2/6; mCanvas.drawTextOnPath(s,path,hOffset,vOffset,mTextPaint); } private void drawBackground() { mCanvas.drawColor(0xFF696565); Paint paint = new Paint(); paint.setColor(0xFFF94905); mCanvas.drawCircle(getWidth()/2,getHeight()/2,getWidth()/2,paint); } }
效果圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。