Frame Animation, that is, frame-by-frame animation, is a continuous display of images to achieve the animation effect,
The key is to control the image switching frequency. We started to try System. currentTimeMillis (),
The API is interpreted as follows:
Returns the current system time in milliseconds since January 1, 1970 00:00:00 UTC.
Returns the number of milliseconds that have elapsed since midnight, January 1, January 1, 1970, UTC,
Pass
Long nowTime = System. currentTimeMillis ();
If (nowTime-startTime> = 100 ){
StartTime = nowTime;
// Do something
}
You can specify the interval,
But about System. currentTimeMillis (), because of the differences in the system, the overhead of the access timer, and so on, the accuracy of each returned value cannot be guaranteed. In actual tests, the screen pauses and flashes, so use a thread,
Prepare the Animated Image first,
The name cannot be a pure number. In this case, the generated variable in R. java will have an error:
Public static final int23 = 0x7f020001;
The following code is used:
FrameAnimationActivity. java
[Java] public class FrameAnimationActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (new Drawing (this ));
}
}
Class Drawing extends View implements Runnable {
Bitmap [] bitmap = new Bitmap [6];
// Create a paint brush
Paint paint = new Paint ();
// Save the image switching variable
Int temp = 0;
// Create a thread
Thread t = new Thread (this );
// Constructor
Public Drawing (Context context ){
Super (context );
Bitmap [0] = BitmapFactory. decodeResource (this. getResources (), R. drawable. r23 );
Bitmap [1] = BitmapFactory. decodeResource (this. getResources (), R. drawable. r24 );
Bitmap [2] = BitmapFactory. decodeResource (this. getResources (), R. drawable. r25 );
Bitmap [3] = BitmapFactory. decodeResource (this. getResources (), R. drawable. r26 );
Bitmap [4] = BitmapFactory. decodeResource (this. getResources (), R. drawable. r27 );
Bitmap [5] = BitmapFactory. decodeResource (this. getResources (), R. drawable. r28 );
// Start thread
T. start ();
}
@ Override
Protected void onDraw (Canvas canvas ){
Super. onDraw (canvas );
// Set the canvas color
Canvas. drawColor (Color. GRAY );
// Draw an image
Canvas. drawBitmap (bitmap [temp], 50, 20, paint );
}
Public void run (){
While (true ){
Temp ++;
If (temp = 5 ){
Temp = 0;
}
// Refresh the screen, interval 70 milliseconds
This. postInvalidate ();
Try {
Thread. sleep (70 );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
}
}
Public class FrameAnimationActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (new Drawing (this ));
}
}
Class Drawing extends View implements Runnable {
Bitmap [] bitmap = new Bitmap [6];
// Create a paint brush www.2cto.com
Paint paint = new Paint ();
// Save the image switching variable
Int temp = 0;
// Create a thread
Thread t = new Thread (this );
// Constructor
Public Drawing (Context context ){
Super (context );
Bitmap [0] = BitmapFactory. decodeResource (this. getResources (), R. drawable. r23 );
Bitmap [1] = BitmapFactory. decodeResource (this. getResources (), R. drawable. r24 );
Bitmap [2] = BitmapFactory. decodeResource (this. getResources (), R. drawable. r25 );
Bitmap [3] = BitmapFactory. decodeResource (this. getResources (), R. drawable. r26 );
Bitmap [4] = BitmapFactory. decodeResource (this. getResources (), R. drawable. r27 );
Bitmap [5] = BitmapFactory. decodeResource (this. getResources (), R. drawable. r28 );
// Start thread
T. start ();
}
@ Override
Protected void onDraw (Canvas canvas ){
Super. onDraw (canvas );
// Set the canvas color
Canvas. drawColor (Color. GRAY );
// Draw an image
Canvas. drawBitmap (bitmap [temp], 50, 20, paint );
}
Public void run (){
While (true ){
Temp ++;
If (temp = 5 ){
Temp = 0;
}
// Refresh the screen, interval 70 milliseconds
This. postInvalidate ();
Try {
Thread. sleep (70 );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
}
}
Like java, threads are used to implement the Rannable interface, override the run () method, and create threads through the Thread class,
There are two ways to refresh the screen,
Invalidate (): instantiate a Handler object and override the handleMessage method to call invalidate () to refresh the interface,
PostInvalidate (): Call it directly,
Attached in accordance with international practice:
From Hu HU's column