Create character walking animations by cutting pictures in Android

Source: Internet
Author: User

I used to use sequential pictures to animate, resulting in a huge waste of space, so I wanted to try cutting pictures to achieve animation.

, is a character walk graph generated by the 66RPG Paper Doll system. This program must be implemented to convert the whole picture of the character into a 4x4 array to dynamically load.

Set up 4 ImageView in the main layout file, respectively, to show the character's walking action at different angles. Then in the main activity file:

public class Mainactivity extends Actionbaractivity {
Private ImageView myimageview,myimageview2,myimageview3,myimageview4;//
Private Handler myhandler;//define thread run identity
Private Boolean iswalking=true;

Class MyThread extends thread{
@Override
public void Run () {
int what=1;
while (iswalking) {
Myhandler.sendemptymessage ((what++)%4), or//generates 0-3 sequentially incremented numbers to serve as subscripts for the image array.
try{
Thread.Sleep (250);//Sleep once every 250 milliseconds
}
catch (Interruptedexception e) {
E.printstacktrace ();
}}}}
Establishes a thread class in the main activity file that is used to periodically pass parameters to the handler object.

Public Bitmap Bitmapclipbitmap (Bitmap bitmap,int x,int y,int w,int h) {
Return Bitmap.createbitmap (Bitmap, x, Y, w,h);
}

Public Bitmap Readbitmap (context context, int resId) {
Bitmapfactory.options opt = new bitmapfactory.options ();
Opt.inpreferredconfig = Bitmap.Config.RGB_565;
Opt.inpurgeable = true;
Opt.ininputshareable = true;
The input stream of the picture
InputStream is = Context.getresources (). Openrawresource (ResId);
Return Bitmapfactory.decodestream (IS, null, opt);
}

These two methods are used to cut pictures and read picture information, respectively. The Readbitmap context parameter can be used at the call Getapplicationcontext ()

To pass.

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

final int anim_count=4,anim_down=0,anim_left=1,anim_right=2,anim_up=3;
Context Context=getapplicationcontext ();//Get context
Bitmap TestMap = Readbitmap (Context,r.drawable.girl);
Final Bitmap [][]bitmap = new Bitmap[anim_count][anim_count]; Create a 4x4 two-dimensional array to store images from 4 angles
int tilewidth = Testmap.getwidth ()/anim_count;
int tileheight = Testmap.getheight ()/anim_count;
int i = 0,x = 0,y = 0;
for (i =0; i < Anim_count; i++) {
y = 0;
Bitmap[anim_down][i] = Bitmapclipbitmap (testmap,x,y,tilewidth,tileheight);
Y+=tileheight;
Bitmap[anim_left][i] = Bitmapclipbitmap (testmap,x,y,tilewidth,tileheight);
Y+=tileheight;
Bitmap[anim_right][i] = Bitmapclipbitmap (testmap,x,y,tilewidth,tileheight);
Y+=tileheight;
Bitmap[anim_up][i] = Bitmapclipbitmap (testmap,x,y,tilewidth,tileheight);
x+= Tilewidth;

Setrequestedorientation (Activityinfo.screen_orientation_landscape);
Setting the screen to landscape
myimageview= (ImageView) Findviewbyid (R.id.myimageview);
Myimageview2= (ImageView) Findviewbyid (R.ID.MYIMAGEVIEW2);
myimageview3= (ImageView) Findviewbyid (R.ID.MYIMAGEVIEW3);
myimageview4= (ImageView) Findviewbyid (R.ID.MYIMAGEVIEW4);
Myhandler=new Handler () {//Instantiate Handler Object
@Override
public void Handlemessage (message msg) {//Receive Message
Super.handlemessage (msg);
Myimageview.setimagebitmap (Bitmap[anim_down][msg.what]);//Set the bitmap to be loaded in the ImageView
Myimageview2.setimagebitmap (Bitmap[anim_left][msg.what]);
Myimageview3.setimagebitmap (Bitmap[anim_right][msg.what]);
Myimageview4.setimagebitmap (Bitmap[anim_up][msg.what]);
}
};
New MyThread (). Start ();
}
}
}

1. Through the width and height of the image, and the width and height of the defined tile, 16 frames of the desired frame image can be cut out, and the TestMap is the total plot to be cut.

2. According to the people walking around, can be divided into four segments of animation, you can define a length of 4 animation array.

3. The final realization on the screen simultaneously shows four different angles of the character walking state.

Reference: http://blog.csdn.net/duancanmeng/article/details/7261900

Http://blog.sina.com.cn/s/blog_89429f6d0101188f.html

Android Game Development Case Tutorial 4th Chapter

Create character walking animations by cutting pictures in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.