// PacmanAnimationActivity.java Copyright (c) Kari Laitinen // http://www.naturalprogramming.com/ // 2012-09-14 File created. // 2015-03-22 Last modification. /* This program displays a "Pacman" whose mouth opens and closes constantly. This is thus yet another example of simple animation. This program uses Animator-based classes that are available in Android 3.0 and later versions. More information related to animation in Android: http://developer.android.com/guide/topics/graphics/index.html To run this program set up an Andoid Studio project and set PacmanAnimationActivity 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. */ package pacman.animation ; import android.app.Activity ; import android.os.Bundle ; import android.graphics.* ; // Classes Canvas, Color, Paint, RectF, etc. import android.view.View ; import android.view.MotionEvent ; import android.view.GestureDetector ; import android.content.Context ; import java.util.ArrayList ; import android.animation.* ; // ValueAnimator, ObjectAnimator, etc. class Pacman { int pacman_center_point_x, pacman_center_point_y ; int pacman_color = Color.BLACK ; int[] pacman_mouth_angles = { 90, 75, 60, 45, 30, 15, 0, 15, 30, 45, 60, 75 } ; int mouth_angle_index = 0 ; public void set_position( int given_center_point_x, int given_center_point_y ) { pacman_center_point_x = given_center_point_x ; pacman_center_point_y = given_center_point_y ; } public void move() { mouth_angle_index ++ ; if ( mouth_angle_index >= pacman_mouth_angles.length ) { mouth_angle_index = 0 ; } } public void draw( Canvas canvas ) { Paint filling_paint = new Paint() ; filling_paint.setStyle( Paint.Style.FILL ) ; filling_paint.setColor( pacman_color ) ; int pacman_mouth_angle = pacman_mouth_angles[ mouth_angle_index ] ; canvas.drawArc( new RectF( pacman_center_point_x - 110, pacman_center_point_y - 90, pacman_center_point_x + 110, pacman_center_point_y + 90 ), pacman_mouth_angle / 2, 360 - pacman_mouth_angle, true, filling_paint ) ; } } final class PacmanAnimationView extends View { int view_width, view_height ; Pacman pacman = new Pacman() ; public PacmanAnimationView( Context context ) { super( context ) ; setBackgroundColor( 0xFFFFFF40 ) ; // yellow //ValueAnimator.setFrameDelay( 500 ) ; // This did not slow down the animation. // The following animator will automatically generate calls to the // setAndUpdate() method of this class. ValueAnimator updating_animator = ObjectAnimator.ofInt( this, "andUpdate", 1, 1000 ) ; updating_animator.setDuration( 3000 ) ; updating_animator.setRepeatCount( ValueAnimator.INFINITE ) ; updating_animator.start() ; } public void onSizeChanged( int current_width_of_this_view, int current_height_of_this_view, int old_width_of_this_view, int old_height_of_this_view ) { view_width = current_width_of_this_view ; view_height = current_height_of_this_view ; pacman.set_position( view_width / 2, view_height / 2 ) ; } public boolean onTouchEvent ( MotionEvent motion_event ) { return true ; } public void setAndUpdate( int given_integer_value ) { pacman.move() ; invalidate() ; } @Override protected void onDraw( Canvas canvas ) { pacman.draw( canvas ) ; } } public class PacmanAnimationActivity extends Activity { public void onCreate( Bundle savedInstanceState ) { super.onCreate( savedInstanceState ) ; setContentView( new PacmanAnimationView( this ) ) ; } }