How to make a simple Android animation with thread

Source: Internet
Author: User

Recently, as the boss asked to do a simple animation, the content can be simply summed up as an arrow in the center of the screen automatically rotate. I know that the Android API has the animation class, which already provides rotation functions. However, the boss asked not to use its API. So my production is as follows:

1. Learn about threads (thread) first

Multithreading means that there are multiple threads in an application, threads are run side by side, and the CPU is preempted together. We usually write the main function is a thread, if we inherit the thread class, then we have developed a new thread.

It is implemented in two ways:

One is to inherit the thread class and override the run () function inside.

Another method is to implement Runnable () and override the run () function inside.

As follows:

class extends thread{

Public void run () { //write code to run }

}

Start thread

Mythread.start ();

Another way:
Class Primerun Implements Runnable {

         public Span class= "KWD" >void Run () {
             //compute primes larger than min Prime
              . Span class= "pun". .
         }
      }
 //start thread
Primerun=newprimerun();
newThread(P). Start();

I'm using the method in the thread class: Mythread.sleep ();
This method allows the system to run at intervals of time, and I set the interval to 100ms. And every time I draw the pointer changes its position, so it looks like it's spinning on its own. The code is as follows:

Private FinalRunnable Movethread =NewRunnable () {@Override Public voidrun () {//TODO auto-generated Method Stub             while(true) {degree=degree+0.1; b= 300*Math.Cos (Math.toradians (degree)); A= 300*Math.sin (Math.toradians (degree)); Try{Thread.Sleep (100); }Catch(interruptedexception e) {e.printstacktrace ();            } postinvalidate ();//re-plotting after each change. }        }            };NewThread (Movethread). Start ();

Next write the line code: you need to create a myview to inherit the view, and override the OnDraw () function

 Public class extends view{ public MyView (context context) {    Super(context);  Public void OnDraw (canvas canvas) {

Super.ondraw (canvas);
float xcenter = (float) getwidth ()/2;
float ycenter = (float) getheight ()/2;

Vector (Xcenter,ycenter, (float) (xcenter+a), (float) (ycenter+b), P,canvas); As the value of each 100ms,a and B is constantly changing, the final effect is that an arrow rotates automatically around the middle of the screen with a circle that has a radius of 300.
}}}

The vector function is used to draw an arrow, and the function is defined as follows:

public void Vector (float x1,float y1,float x2,float y2,paint Paint,canvas Canvas) {
Double vdeg = math.todegrees (Math.atan2 (y2-y1, x2-x1));
Double deldeg = 10;
Canvas.drawline (x1, y1, x2, y2, paint);
Double arrowsize = 0.8*math.sqrt ((x2-x1) * (x2-x1) + (y2-y1) * (y2-y1));
Double Vrad = (vdeg-deldeg) *math.pi/180.0;
Canvas.drawline (float) (X1+arrowsize*math.cos (Vrad)), (float) (Y1+arrowsize*math.sin (Vrad)), x2,y2,paint);
Vrad = (vdeg+deldeg) *math.pi/180.0;
Canvas.drawline (float) (X1+arrowsize*math.cos (Vrad)), (float) (Y1+arrowsize*math.sin (Vrad)), x2,y2,paint);

}



How to make a simple Android animation with thread

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.