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 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:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical"

    android:background="#000000">

 

    <com.authorwjf.bounce.AnimatedView  

              android:id="@+id/anim_view"  

              android:layout_height="fill_parent"  

              android:layout_width="fill_parent"/>

             

</LinearLayout>

 

 

Step 4

Create a Java file and provide this for it:
 

import android.os.Bundle;

import android.app.Activity;

 

public class MainActivity extends Activity {

 

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }

}

 

Step 5

Create another Java class and provide this for it:
 

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.drawable.BitmapDrawable;

import android.os.Handler;

import android.util.AttributeSet;

import android.widget.ImageView;

 

public class AnimatedView extends ImageView{

 

       private Context context;

       int x = -1;

       int y = -1;

       private int VelocityxDirection = 10;

       private int VelocityyDirection = 5;

       private Handler handler;

       private final int FRAME_RATE = 30;

 

 

       public AnimatedView(Context context, AttributeSet attrs)  { 

              super(context, attrs); 

              context = context; 

              handler = new Handler();

    }

 

       private Runnable r = new Runnable() {

              @Override

              public void run() {

                     invalidate();

              }

       };

 

       protected void onDraw(Canvas canvas) { 

 

              BitmapDrawable drawableball = (BitmapDrawable) context.getResources().getDrawable(R.drawable.ball); 

           if (x<0 && y <0) {

              x = this.getWidth()/2;

              y = this.getHeight()/2;

           } else {

              x += VelocityxDirection;

              y += VelocityyDirection;

              if ((x > this.getWidth() - drawableball.getBitmap().getWidth()) || (x < 0)) {

                     VelocityxDirection = VelocityxDirection*-1;

              }

              if ((y > this.getHeight() - drawableball.getBitmap().getHeight()) || (y < 0)) {

                     VelocityyDirection = VelocityxDirection*-1;

              }

           }

           canvas.drawBitmap(drawableball.getBitmap(), x, y, null); 

           handler.postDelayed(r, FRAME_RATE);

    }

}


Step 6

Android manifest.xml file:
 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.authorwjf.bounce"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="4"

        android:targetSdkVersion="15" />

 

    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden"

            android:name=".MainActivity"

            android:label="@string/title_activity_main" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

</manifest>

 

Step 7

Image 1

 

Clipboard004.jpg

Image 2

Clipboard02.jpg

Image 3


Clipboard03.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all