android SurfaceView實現人物動畫

來源:互聯網
上載者:User

經常看到別人的遊戲中有人物跑動的效果,對這個東西很好奇,剛好群裡上傳了“忍者突襲”的代碼,我看了裡面的代碼,但對畫人物那段還是沒咋弄,所以自己寫一個測試程式,程式中使用到的圖片資源是來自“忍者突襲”的代碼,真心的感謝寫“忍者突襲”代碼的人~~

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

在畫人物角色時會對這張圖片進行剪下,使用的函數是

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 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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.