How to Bounce a Ball in Android

Introduction

 
This article explains how to bounce a ball in Android. Android Studio is used to create the sample.. This application will show a ball that follows a path to reach its destination and returns to the initial position.
 
First, create the XML file and set the content to the Activity. Now create another class and extend the Image View. Now create the constructor of that class and the context and attribute set to the super class constructor. In the onDraw method get the ball by using Bitmapdrawable and drawable folder where you copied the ball image. You need to download the image and copy it into the drawable folder.
 
Step 1
 
You will add the image of a ball by copying to the drawable folder like this:
 
Clipboard003.jpg
 
Step 2
 
Create a project like this:
 
Clipboard002.jpg
 
Step 3
 
Create an XML file and provide this for it:
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical"  
  6.     android:background="#000000">  
  7.    
  8.     <com.authorwjf.bounce.AnimatedView    
  9.               android:id="@+id/anim_view"    
  10.               android:layout_height="fill_parent"    
  11.               android:layout_width="fill_parent"/>  
  12.                
  13. </LinearLayout> 
Step 4
 
Create a Java file and provide this for it:
  1. import android.os.Bundle;  
  2. import android.app.Activity;  
  3.    
  4. public class MainActivity extends Activity {  
  5.    
  6.     @Override  
  7.     public void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_main);  
  10.     }  
Step 5
 
Create another Java class and provide this for it:
  1. import android.content.Context;  
  2. import android.graphics.Canvas;  
  3. import android.graphics.drawable.BitmapDrawable;  
  4. import android.os.Handler;  
  5. import android.util.AttributeSet;  
  6. import android.widget.ImageView;  
  7.    
  8. public class AnimatedView extends ImageView{  
  9.    
  10.        private Context context;  
  11.        int x = -1;  
  12.        int y = -1;  
  13.        private int VelocityxDirection = 10;  
  14.        private int VelocityyDirection = 5;  
  15.        private Handler handler;  
  16.        private final int FRAME_RATE = 30;  
  17.    
  18.    
  19.        public AnimatedView(Context context, AttributeSet attrs)  {   
  20.               super(context, attrs);   
  21.               context = context;   
  22.               handler = new Handler();  
  23.     }  
  24.    
  25.        private Runnable r = new Runnable() {  
  26.               @Override  
  27.               public void run() {  
  28.                      invalidate();  
  29.               }  
  30.        };  
  31.    
  32.        protected void onDraw(Canvas canvas) {   
  33.    
  34.               BitmapDrawable drawableball = (BitmapDrawable) context.getResources().getDrawable(R.drawable.ball);   
  35.            if (x<0 && y <0) {  
  36.               x = this.getWidth()/2;  
  37.               y = this.getHeight()/2;  
  38.            } else {  
  39.               x += VelocityxDirection;  
  40.               y += VelocityyDirection;  
  41.               if ((x > this.getWidth() - drawableball.getBitmap().getWidth()) || (x < 0)) {  
  42.                      VelocityxDirection = VelocityxDirection*-1;  
  43.               }  
  44.               if ((y > this.getHeight() - drawableball.getBitmap().getHeight()) || (y < 0)) {  
  45.                      VelocityyDirection = VelocityxDirection*-1;  
  46.               }  
  47.            }  
  48.            canvas.drawBitmap(drawableball.getBitmap(), x, y, null);   
  49.            handler.postDelayed(r, FRAME_RATE);  
  50.     }  
Step 6
 
Android manifest.xml file:
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     package="com.authorwjf.bounce"  
  3.     android:versionCode="1"  
  4.     android:versionName="1.0" >  
  5.    
  6.     <uses-sdk  
  7.         android:minSdkVersion="4"  
  8.         android:targetSdkVersion="15" />  
  9.    
  10.     <application  
  11.         android:icon="@drawable/ic_launcher"  
  12.         android:label="@string/app_name"  
  13.         android:theme="@style/AppTheme" >  
  14.         <activity  
  15.             android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden"  
  16.             android:name=".MainActivity"  
  17.             android:label="@string/title_activity_main" >  
  18.             <intent-filter>  
  19.                 <action android:name="android.intent.action.MAIN" />  
  20.    
  21.                 <category android:name="android.intent.category.LAUNCHER" />  
  22.             </intent-filter>  
  23.         </activity>  
  24.     </application>  
  25. </manifest> 
Step 7
 
Image 1
 
Clipboard004.jpg
 
Image 2
 
Clipboard02.jpg
 
Image 3
 
Clipboard03.jpg