首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android SurfaceView兑现人物动画

2012-09-16 
android SurfaceView实现人物动画经常看到别人的游戏中有人物跑动的效果,对这个东西很好奇,刚好群里上传了

android SurfaceView实现人物动画

经常看到别人的游戏中有人物跑动的效果,对这个东西很好奇,刚好群里上传了“忍者突袭”的代码,我看了里面的代码,但对画人物那段还是没咋弄,所以自己写一个测试程序,程序中使用到的图片资源是来自“忍者突袭”的代码,真心的感谢写“忍者突袭”代码的人~~

人物角色使用的图片如下:

android SurfaceView兑现人物动画

在画人物角色时会对这张图片进行剪切,使用的函数是

package com.example.runmanenvironmenttest;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Rect;import android.util.AttributeSet;import android.view.SurfaceHolder;import android.view.SurfaceView;public class MyView extends SurfaceView implements SurfaceHolder.Callback, Runnable{private SurfaceHolder holder;  private Canvas mCanvas;private Bitmap mBg1;private Bitmap mPlay1;private int mWidth;private int mHeight;private Paint mPaint;private String tag = "xiao";private BitmapFactory.Options ops;private Rect mRect;private Rect mClipRect;private int mPosition = 20;private int mPicPosition = 0;private int mStep = 5;private int mBamHeight = 600;public MyView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stubholder = this.getHolder();          holder.addCallback(this);        mPaint = new Paint();        mPaint.setColor(Color.YELLOW);                ops = new BitmapFactory.Options();        mBg1 = BitmapFactory.decodeResource(this.getResources(),R.drawable.bg1, ops);               mPlay1 = BitmapFactory.decodeResource(getResources(), R.drawable.dartman, ops);}@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {// TODO Auto-generated method stubmWidth = width;mHeight = height;mRect = new Rect(0, 0, mWidth, mHeight);new Thread(this).start();}@Overridepublic void surfaceCreated(SurfaceHolder holder) {// TODO Auto-generated method stub}@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {// TODO Auto-generated method stub}@Overridepublic void run() {// TODO Auto-generated method stub//myDraw();while(true){try {mClipRect = new Rect(mPosition * mStep + mPlay1.getWidth() / 10, mBamHeight,mPosition * mStep + 2 * mPlay1.getWidth() / 10, mBamHeight - mPlay1.getHeight());mCanvas = holder.lockCanvas();if (mCanvas != null) {mCanvas.drawBitmap(mBg1, null,mRect, mPaint);mCanvas.save();mCanvas.clipRect(mClipRect);mCanvas.drawBitmap(mPlay1, mPlay1.getWidth() / 10 + mPosition * mStep - mPicPosition * mPlay1.getWidth() / 10, mBamHeight - mPlay1.getHeight(), mPaint);mCanvas.restore();mPicPosition++;if(mPosition * mStep > mWidth){mPosition = 0;}if(mPicPosition > 9){mPicPosition = 0;}}} catch (Exception e) {e.printStackTrace();} finally {if (mCanvas != null) {holder.unlockCanvasAndPost(mCanvas);}}try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}

源代码下载地址:http://download.csdn.net/detail/xiaoxiaobian3310903/4565995

热点排行