Android 사용자 정의 View 추첨 룰렛 실현

본 논문 의 사례 는 안 드 로 이 드 사용자 정의 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 < 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;
 //          
 if (isShouldEnd) {
 mSpeed -= 1;
 }
 if (mSpeed <= 0) {
 mSpeed = 0;
 isShouldEnd = false;
 }
 }
 }catch (Exception e){
 
 }finally {
 if (mCanvas != null) {
 //   Canvas
 mHolder.unlockCanvasAndPost(mCanvas);
 }
 }
 }
 
 
 //      
 
 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) {
 //            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);
 
 }
 
 
 //         
 
 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);
 
 }
}
효과 그림:

이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기