Android makes the flashing hearts

Source: Internet
Author: User

Let's go first:


Talk about the source of this thing, today suddenly think of Descartes heart-shaped diagram, want to see can draw a heart out, but see a good article, that article lists a lot of heart-shaped function equation, this can make me happy bad, so I chose a more beautiful "peach heart" to do today's effect.

Paste the following code:

Import Android.content.context;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.paint.style;import Android.graphics.path;import Android.util.attributeset;import Android.view.view;public class Loveview extends View {private Paint paint;private float rate = 5; Radius change rate private animthread at; Change the rate of the thread private path path; Path public Loveview (context context) {super (context); init ();} Public Loveview (context context, AttributeSet Attrs) {Super (context, attrs); init ();} private void Init () {at = new Animthread ();//initialization brush paint = new Paint ();p Aint.setantialias (True);p Aint.setcolor (color.red );p Aint.setstyle (Style.fill);p aint.setstrokewidth (2);//Create a Path = new path (); At.start (); @Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas);//Reset artboard Path.reset ();//Get half the width of the screen int px =  Getmeasuredwidth ()/2;int py = getmeasuredheight ()/2;//The starting point of the path Path.moveto (px, py-5 * rate);//Draw for with the heart function (double i = 0; I <= 2 * math.pi; i + = 0.001) {float x = (float) (Math.sin (i) * Math.sin (i) * Math.sin (i)); float y = (float) (* Math.Cos (i)-5 * Ma) Th.cos (2 * i)-2 * MATH.COS (3 * i)-math.cos (4 * i)); x *= rate;y *= rate;x = Px-x;y = Py-y;path.lineto (x, y);} Canvas.drawpath (path, paint);} Private class Animthread extends Thread {public void run () {when (true) {rate + = 0.05;if (Rate > 20) {//My phone is larger than 20 , in order not to exceed screen rate = 5;} try {thread.sleep;} catch (Interruptedexception e) {e.printstacktrace ();} Refresh Canvas postinvalidate ();}}};}

Note It is clear that some parameter rate is based on the actual situation of my phone, so the value I give is fixed. And if you just follow the function in that article, the shape of the heart is reversed, so there's a code change over there, so the diagram is positive.

In fact, sometimes feel that this thing has no technical content (originally embarrassed to send), haha, but feel fun, or can not help send out.

Android makes the flashing hearts

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.