• TekArt

    TekArt is an Organisation where people develop Android App through innovative ideas. App for the next Generation....

Sunday 30 March 2014

Posted by Unknown
No comments | 02:13
Hello guys, how are you today? Its Super Sunday, while you guys are enjoying i am busy writing this blog. Never mind, lets get started.
In this blog i will show you how to create animation of sprites in android. Basically what we are going to do is to display the sprites one by one quickly so that it looks like as if it is an animation.


Download http://www.2shared.com/file/9aHDwyXe/Blog4.html

1) First create an Android Application Project in Eclipse.Lets name it "Blog4" .

2) Now go to "res/layout/activity_main.xml" and create an image view there, just like i did.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id = "@+id/image" />

</RelativeLayout>

3) Now go to "src/your_package_name/MainActivity.java" . Here we are going to create two classes one is "Rotate class" and other is "MyHandler class". Rotate class extends TimerTask and rotates our Sprite after a certain period of time.

private class Rotate extends TimerTask
    {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            handler.sendEmptyMessage(_index);
             if(_index==7) _index =0;
             else _index++;
           
        }
    }

MyHandler Class handles the request from the Rotate class and sets the image in the ImageView that we have previous created.

private class MyHandler extends Handler
    {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);

            try {
                    Bitmap bmp= BitmapFactory.decodeStream(MainActivity.this.getAssets().open("costume"+_index+".png"));
                    image.setImageBitmap(bmp);

                    Log.v("Loaing Image: ",_index+"");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.v("Exception in Handler ",e.getMessage());
            }
        }
    }

4) The Sprite pictures need to be saved in the assets folder under res directory. For this project i am using sprites from this site http://scratch.mit.edu/projects/1307379/  . Thanks a lot to the developer of these Sprites.

5) So our whole code under MainActivity.java looks like.

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.widget.ImageView;

public class MainActivity extends Activity {
    ImageView image;
    Rotate t;
    private Timer _timer;
    private int _index;
    private MyHandler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image = (ImageView) findViewById(R.id.image);
        handler= new MyHandler();
        _index=0;
        _timer= new Timer();
         t =new Rotate();
        _timer.schedule(t, 100, 200);
    }

    private class Rotate extends TimerTask
    {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            handler.sendEmptyMessage(_index);
             if(_index==7) _index =0;
             else _index++;
           
        }
    }
    private class MyHandler extends Handler
    {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);

            try {
                    Bitmap bmp= BitmapFactory.decodeStream(MainActivity.this.getAssets().open("costume"+_index+".png"));
                    image.setImageBitmap(bmp);

                    Log.v("Loaing Image: ",_index+"");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.v("Exception in Handler ",e.getMessage());
            }
        }
    }

}

So, now we are done. Post your problems in the comments, i will love to handle those.

For more info https://www.facebook.com/androidcoolstuffs

Thank you

0 comments:

Post a Comment