經常看到別人的游戲中有人物跑動的效果,對這個東西很好奇,剛好群裡上傳了“忍者突襲”的代碼,我看了裡面的代碼,但對畫人物那段還是沒咋弄,所以自己寫一個測試程序,程序中使用到的圖片資源是來自“忍者突襲”的代碼,真心的感謝寫“忍者突襲”代碼的人~~
人物角色使用的圖片如下:
在畫人物角色時會對這張圖片進行剪切,使用的函數是
mCanvas.clipRect(mClipRect);
繪制人物角色的思想是使用mCanvas.clipRect(mClipRect);函數來設置畫布顯示的位置及大小,假設為(presentX,presentY,presentX + width/10, presentY + height)(presentX和presentY為現在圖片的位置,width和height為圖片的寬度和高度),然後使用mCanvas.drawBitmap來繪制圖片,第一次繪制圖片的位置是(presentX,presentY),然後將繪制圖片的位置修改為(presentX - width / 10, presentY),第二次設置的畫布顯示的位置仍然是presentX,presentY,presentX + width/10, presentY + height,繪制圖片的位置是(presentX - width / 10, presentY),而第二張圖片的位置剛好是presentX,presentY,所以顯示的是第二張圖片,依次類推的實現其它圖片的顯示。
實例代碼如下:
- 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 stub
- holder = 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);
- }
-
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width,
- int height) {
- // TODO Auto-generated method stub
- mWidth = width;
- mHeight = height;
- mRect = new Rect(0, 0, mWidth, mHeight);
-
- new Thread(this).start();
- }
-
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public 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 block
- e.printStackTrace();
- }
- }
- }
- }
源代碼下載地址:
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /2012年資料/9月/13日/Android SurfaceView實現人物動畫