Recently, the project requires a lottery turntable function. It takes a long time to draw the background, text, and image of each item, it will not catch up with the progress of the project, so it is a coincidence that the award and the turntable pointer are regarded as an independent whole, and only the rotation and angle judgment can be done, which will save a lot of time, even if you want to update the lottery prizes in the future, you only need to replace the large image of the prize. All the awards and prompts you will receive will be controlled by the server:
I wrote it (Project extraction instances are relatively simple, so don't be surprised)
I am not talking about the specific code analysis. I only paste the key code,
Import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. graphics. color; import android. graphics. matrix; import android. graphics. paint; import android. widget. imageView; public class Circleview extends ImageView implements Runnable {private Bitmap mHourBitmap; private boolean bInitComplete = false; private boolean sto PRoter = true; float Angel = 0.0f; Matrix matx = new Matrix ();/*** various winning calculation parameters maxAngel = rotate to the winning angle */float maxAngel = 0.0f; /*** screen width */int screnWidth = 0;/*** initial drawing slide component * @ param context * @ param width screen width */public Circleview (Context context, int width) {super (context); this. screnWidth = width; init (); new Thread (this ). start ();} public void init () {mHourBitmap = BitmapFactory. decodeResource (getResources (), R. drawable. required); bInitComplete = true;} public void setRotate_degree (float degree) {Angel = degree;} @ Overrideprotected void onDraw (Canvas canvas Canvas) {// TODO Auto-generated method stubsuper. onDraw (canvas); matx. reset (); canvas. drawColor (Color. TRANSPARENT); if (! BInitComplete) {return;} Paint localPaint = new Paint (); // you can call this operation to cancel the configuration of localPaint. setAntiAlias (true); localPaint. setFilterBitmap (true);/*** initial intermediate pointer */matx. setTranslate (this. screnWidth/2-mHourBitmap.getWidth ()/2, DensityUtil. dip2px (getContext (), 300)/2-mHourBitmap.getHeight () + DensityUtil. dip2px (getContext (), 20);/*** sets the rotation around the vertex */matx. preRotate (Angel, mHourBitmap. getWidth ()/2, mHourBitmap. getHeight () * 4/ 5); canvas. drawBitmap (mHourBitmap, matx, localPaint);} public void run () {try {while (true) {if (! IsStopRoter () {if (maxAngel! = 0 & Angel> = maxAngel) {setStopRoter (true); maxAngel = 0.0f;} else {if (maxAngel-Angel <360) setRotate_degree (Angel + = 10 ); elsesetRotate_degree (Angel + = 15); this. postInvalidate (); Thread. sleep (50) ;}}} catch (InterruptedException e) {e. printStackTrace () ;}}/*** get the current angle and set the stop angle * @ param palce position * @ return */public void setStopPlace (int place) {getRoterByPlace (place );} /*** clockwise rotation * 1 = 330-30*2 = 30-90*3 = 90-150*4 = 150-210*5 = 210-270*6 = 270 -330 * @ param place * @ return */void getRoterByPlace (int place) {float roter = getRoteCenter (place); float currentRoter = getCurrentRoter (); // if the current angle is smaller than the position, it indicates how many angles need to be converted. float difRoter = currentRoter-roter; // fixed three-ring 360*3, and added the current angle difference maxAngel = Angel + 360*2 + 360-difRoter ;} /*** from the perspective of the position where the award is received-the average value of each award is obtained for the turntable's 360 degrees, click * @ param place * @ return */float getRoteCenter (int place) {float roter = 0.0f; switch (place) {case 1: roter = 0; break; case 2: roter = 60/2 + 30; break; case 3: roter = 60/2 + 90; break; case 4: roter = 60/2 + 150; break; case 5: roter = 60/2 + 210; break; case 6: roter = 60/2 + 270; break; default: break;} return roter ;} /*** get the actual rotation angle -- convert the angle value * @ return */float getCurrentRoter () {int current = (int) Angel/360; if (0 = current) return Angel; float roter = Angel-360 * current; return roter;} public boolean isStopRoter () {return stopRoter;} public void setStopRoter (boolean stopRoter) {this. stopRoter = stopRoter ;}}
Specific Demo address: http://download.csdn.net/detail/yuan232007/5850753
If you have a good implementation method, please send a message to me for me to learn. Thank you!