Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the Gourmet turntable by Android

Shulou Source: shulou.com Published: 2022-06-02 14:46:13 10月04日 Update

This article is to share with you about how Android realizes the food turntable. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Effect video

Initialize SurfaceViewprivate void init () {mSurfaceHolder = this.getHolder (); / / manage the lifecycle of SurfaceView mSurfaceHolder.addCallback (this); this.setFocusable (true); this.setFocusableInTouchMode (true); this.setKeepScreenOn (true);} Measurement

Get the center point of the control by getting the height and width, and then subtracting the length of the mPadding, and then store the measured width and height

@ Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure (widthMeasureSpec, heightMeasureSpec); int width = Math.min (getMeasuredWidth (), getMeasuredHeight ()); mPadding = getPaddingLeft (); mRadius = width-mPadding * 2; / / Center point mCenter = width / 2; setMeasuredDimension (width, width);} draw

Draw the disc background

Set the background picture

Private Bitmap mBgBitmap = BitmapFactory.decodeResource (getResources (), R.drawable.turntable_bgcolor)

Draw the prominent part of the background

Private void drawBgColor () {mCanvas.drawColor (0xFFFFFFFF); mCanvas.drawBitmap (mBgBitmap, null, new Rect (mPadding / 2, mPadding / 2, getMeasuredWidth ()-mPadding / 2, getMeasuredWidth ()-mPadding / 2), null);} draw blocks private void drawArea () {/ / starting angle float tempAngle = mStartAngle; / / the angle of each block drawn float sweepAngele = 360 / mItemCount For (int I = 0; I < mItemCount; colors +) {mArcPaint.setColor (itemColors [I]); / draw mCanvas.drawArc (mRange, tempAngle, sweepAngele, true, mArcPaint); / / draw text drawText (tempAngle, sweepAngele, itemCharString [I]); / / draw icon drawIcon (tempAngle, mBitmaps [I]) TempAngle + = sweepAngele;} mStartAngle + = mSpeed; / / if you need to stop, let the speed gradually decrease until 0 if (isShouldEnd) {mSpeed-= mDifferSpeed;} if (create an oval.xml file under mSpeed drawable

The code is as follows:

Animation settin

Configure the circle style we established in the XML file

Android:background= "@ drawable/oval"

Then the animation is set in Activity, where the five parameters of the createCircularReveal () method are: control, X at the center where the animation begins, Y at the center at the beginning of the animation, radius at the beginning of the animation, and radius at the end of the animation

Private void RevealAnim (View view) {Animator animator = ViewAnimationUtils.createCircularReveal (view, view.getWidth () / 2, view.getHeight () / 2, view.getWidth (), 0); animator.setInterpolator (new AccelerateDecelerateInterpolator ()); animator.setDuration (2000); animator.start ();} Custom turntable Code public class Turntable extends SurfaceView implements SurfaceHolder.Callback, Runnable {private SurfaceHolder mSurfaceHolder; private Canvas mCanvas / * child thread for SurfaceView drawing * / private Thread mThread; / * controls the child thread switch * / private boolean isRunning; / * / private String [] itemCharString; / * picture * / private int [] itemImages; private Bitmap [] mBitmaps / * background * / private Bitmap mBgBitmap = BitmapFactory.decodeResource (getResources (), R.drawable.turntable_bgcolor); / * Color Block * / private int [] itemColors; / * * default equivalent * / private int mItemCount = 8; / * range of the entire disk * / private RectF mRange = new RectF () / * / private int mRadius; / * paint the brush of the block * / private Paint mArcPaint; / * the brush of the drawing text * / private Paint mTextPaint; / * font size * / private float mTextSize = TypedValue.applyDimension (TypedValue.COMPLEX_UNIT_SP, 20, getResources (). GetDisplayMetrics ()) / * the speed of the disk block scrolling * / private double mSpeed = 0; / * the center position of the turntable * / private int mCenter; / * where our padding directly takes paddingLeft * / private int mPadding; / * volatile to ensure the visibility between threads * / private volatile float mStartAngle = 0 / * determine whether the stop button is clicked * / private boolean isShouldEnd = false; / * set the minimum time for a single drawing. If the drawing is completed within this time, let the child threads sleep to the end of the change time * this prevents the thread from drawing frequently and consumes performance first * / private long mOneTimeMinMillionSeconds = 50; private int mDifferSpeed = 1 / / the decreasing speed difference after the call is stopped should be greater than 0 public Turntable (Context context) {super (context); init ();} public Turntable (Context context, AttributeSet attrs) {super (context, attrs); init ();} public Turntable (Context context, AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr); init () } public void InitNumber (int number) {if (number)

Tags: Turntable picture equivalent code title style icon text system background angle position animation diameter thread effects file text status brush Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MariaDB vpn Apple Shulou Tech Info