In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to achieve the effect of petals falling in Android development. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Implementation steps
1. Define a variable to initialize a variable
Private SurfaceHolder mHolder; private boolean mFlag = true;// switch mark private ArrayList mFlowers;// floret coordinate set private Random mRandom;// is responsible for random number generation of private Bitmap mBitmap;// floret pattern public FlowerView (Context context) {super (context); init ();} public FlowerView (Context context, AttributeSet attrs) {super (context, attrs); init () } public FlowerView (Context context, AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr); init ();} private void init () {mHolder = getHolder (); mHolder.addCallback (this); / / set background transparent this.setZOrderOnTop (true); mHolder.setFormat (PixelFormat.TRANSLUCENT); mFlowers = new ArrayList (); mRandom = new Random () MBitmap = BitmapFactory.decodeResource (getResources (), R.drawable.ic_hua);}
two。 Realize the method of adding flower coordinate points
/ * add flowers * / private void addFlower () {PointF point = new PointF (); point.x=mRandom.nextInt (getWidth ()); / / randomly generate X-axis coordinates point.y=-mBitmap.getHeight () according to the width of the control; / / set the vertical coordinate to the negative value of the floret image (to produce the effect of entering from outside the screen) mFlowers.add (point) / / add coordinate points to the collection}
3. Implement SurfaceHolder.Callback and Runnable interface
Public class FlowerView extends SurfaceView implements SurfaceHolder.Callback,Runnable
4. Implementation of drawing Logic in run method
@ Override public void run () {while (mFlag) {try {Thread.sleep (80); / / controls the falling speed of florets Canvas canvas = mHolder.lockCanvas (); PointF pointF = null / / screen removal operation (otherwise some useless images will be left) if (canvasated images null) {canvas.drawColor (Color.TRANSPARENT, PorterDuff.Mode.CLEAR);} else {continue;} for (PointF point: mFlowers) {pointF = point Canvas.drawBitmap (mBitmap,pointF.x,pointF.y,null); int I = mRandom.nextInt (getHeight () / 50) + getHeight () / 50 addFlower / modify the ordinate of the raindrop line to make it look like it is raining pointF.y=pointF.y+i;} mHolder.unlockCanvasAndPost (canvas); addFlower () / / when the ordinate of the drawing point is greater than the height of the control, remove the point if (mFlowers.size () > 0 precision pointFoundpoint point F.y > = getHeight ()) {mFlowers.remove (pointF);}} catch (Exception e) {}
5. Start the drawing thread in the callback method of SurfaceHolder.Callback
@ Override public void surfaceCreated (SurfaceHolder holder) {mFlag = true;//surface turn on the thread switch new Thread (this). Start (); / / turn on thread drawing} @ Override public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {mFlowers.clear () / / clear the previous drawing point} @ Override public void surfaceDestroyed (SurfaceHolder holder) {mFlag = false;// when the control changes. Turn off the drawing thread when the surface is destroyed} the complete code shows the public class FlowerView extends SurfaceView implements SurfaceHolder.Callback,Runnable {private SurfaceHolder mHolder; private boolean mFlag = true;// switch marking the private ArrayList mFlowers;// floret coordinate collection private Random mRandom / / public FlowerView (Context context) {super (context); init ();} public FlowerView (Context context, AttributeSet attrs) {super (context, attrs); init ();} public FlowerView (Context context, AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr); init () } private void init () {mHolder = getHolder (); mHolder.addCallback (this); / / set background transparency this.setZOrderOnTop (true); mHolder.setFormat (PixelFormat.TRANSLUCENT); mFlowers = new ArrayList (); mRandom = new Random (); mBitmap = BitmapFactory.decodeResource (getResources (), R.drawable.ic_hua) } @ Override public void surfaceCreated (SurfaceHolder holder) {mFlag = true; new Thread (this). Start ();} @ Override public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {mFlowers.clear ();} @ Override public void surfaceDestroyed (SurfaceHolder holder) {mFlag = false } @ Override public void run () {while (mFlag) {try {Thread.sleep (80); Canvas canvas = mHolder.lockCanvas (); PointF pointF = null; / / if (canvasation operations null) {canvas.drawColor (Color.TRANSPARENT, PorterDuff.Mode.CLEAR) } else {continue;} for (PointF point: mFlowers) {pointF = point; canvas.drawBitmap (mBitmap,pointF.x,pointF.y,null); int I = mRandom.nextInt (getHeight () / 50) + getHeight () / 50 / / modify the ordinate of the raindrop line to make it look like it is raining pointF.y=pointF.y+i;} mHolder.unlockCanvasAndPost (canvas); addFlower (); if (mFlowers.size () > zero rain pointpointpoint point F.y > = getHeight ()) {mFlowers.remove (pointF) }} catch (Exception e) {} / * * add flowers * / private void addFlower () {PointF point = new PointF (); point.x=mRandom.nextInt (getWidth ()); point.y=-mBitmap.getHeight (); mFlowers.add (point);}} Thank you for reading! This is the end of this article on "how to achieve the effect of falling petals in Android development". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.