// AnimationDemoActivity.java Copyright (c) Kari Laitinen // http://www.naturalprogramming.com/ // 2012-01-30 File created. // 2015-03-22 Last modification. /* This program demonstrates how a thread can be used to produce animation onto the screen. "Animation" in this program means that there is a blinking ball on the screen. To run this program set up an Andoid Studio project and set AnimationDemoActivity as the name of its main activity. Use the package name given below. Overwrite the automatically created .java file with this file. This program does not need any specific .xml files. See notes at the end of this file. */ // http://developer.android.com/reference/android/app/Activity.html // http://developer.android.com/reference/android/graphics/Canvas.html // http://developer.android.com/reference/android/view/View.html package animation.demo ; import android.app.Activity ; import android.os.Bundle ; import android.graphics.* ; // Classes Canvas, Color, Paint, RectF, etc. import android.view.* ; // Classes View, Display, WindowManager, etc. import android.content.Context ; final class AnimationDemoView extends View implements Runnable { int view_width, view_height ; Thread animation_thread = null ; boolean thread_must_be_executed = false ; boolean ball_must_be_shown = true ; int ball_center_point_x, ball_center_point_y ; int ball_color = 0xFF008B8B ; // DarkCyan public AnimationDemoView( Context context, int given_display_width, int given_display_height ) { super( context ) ; view_width = given_display_width ; view_height = given_display_height ; ball_center_point_x = view_width / 2 ; ball_center_point_y = view_height / 2 ; setBackgroundColor( 0xFFFFF5EE ) ; // Light color } public void start_animation_thread() { if ( animation_thread == null ) { animation_thread = new Thread( this ) ; thread_must_be_executed = true ; animation_thread.start() ; } System.out.print( "\n Method start() executed. " ) ; } public void stop_animation_thread() { if ( animation_thread != null ) { animation_thread.interrupt() ; thread_must_be_executed = false ; animation_thread = null ; } System.out.print( "\n Method stop() executed. " ) ; } public void run() { System.out.print( "\n Method run() started." ) ; while ( thread_must_be_executed == true ) { System.out.print( " X " ) ; try { Thread.sleep( 1000 ) ; // Delay of 1000 milliseconds. } catch ( InterruptedException caught_exception ) { thread_must_be_executed = false ; } // Here we must use the method postInvalidate() instead of // invalidate(). postInvalidate() must be used to invalidate // a View from a non-UI thread. postInvalidate() ; } System.out.print( "\n Method run() stopped. " ) ; } protected void onDraw( Canvas canvas ) { Paint filling_paint = new Paint() ; filling_paint.setStyle( Paint.Style.FILL ) ; filling_paint.setColor( ball_color ) ; if ( ball_must_be_shown == true ) { canvas.drawCircle( ball_center_point_x, ball_center_point_y, 64, filling_paint ) ; ball_must_be_shown = false ; } else { ball_must_be_shown = true ; } } } public class AnimationDemoActivity extends Activity { AnimationDemoView animation_demo_view; public void onCreate( Bundle savedInstanceState ) { super.onCreate( savedInstanceState ) ; Display display = ( (WindowManager) getSystemService( Context.WINDOW_SERVICE )).getDefaultDisplay() ; int display_width = display.getWidth() ; int display_height = display.getHeight() ; animation_demo_view = new AnimationDemoView( this, display_width, display_height ) ; setContentView( animation_demo_view ) ; } public void onStart() { super.onStart() ; animation_demo_view.start_animation_thread() ; } public void onStop() { super.onStop() ; animation_demo_view.stop_animation_thread() ; } } /* NOTES: A common mistake made by new Android developers is to use the width and height of a view inside its constructor. When a View constructor is called, Android does not know yet how big the view will be, so the sizes are set to zero. The real sizes are calculated during the layout stage, which occurs after construction but before anything is drawn. You can use the onSizeChanged( )method to be notified of the values when they are known, or you can use the getWidth( ) and getHeight( )methods later, such as in the onDraw() method. (This was written by Ed Burnette at http://www.coderanch.com) */