经常看到别人的游戏中有人物跑动的效果,对这个东西很好奇,刚好群里上传了“忍者突袭”的代码,我看了里面的代码,但对画人物那段还是没咋弄,所以自己写一个测试程序,程序中使用到的图片资源是来自“忍者突袭”的代码,真心的感谢写“忍者突袭”代码的人~~
人物角色使用的图片如下:
在画人物角色时会对这张图片进行剪切,使用的函数是
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://download.csdn.net/detail/xiaoxiaobian3310903/4565995