[Android]SurfaceView繪製奔跑的小人

轉自:http://blog.csdn.net/xiaoxiaobian3310903/article/details/7971962android

原文源代碼使用的是橫屏顯示,奔跑的小人多是因爲座標緣由沒法顯示在屏幕上,在這裏我只做了正常豎屏顯示app

 

代碼以下:ide

import android.app.Activity;
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.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.WindowManager;

import com.wisher.android2d.R;

public class Activity04 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        RZSurfaceView view = new RZSurfaceView(this);
        setContentView(view);
    }
    
    private class RZSurfaceView extends SurfaceView implements Callback, Runnable {
        
        private SurfaceHolder holder;
        private Canvas mCanvas;
        private Bitmap mBg1;
        private Bitmap mPlay1;
        private int mWidth, mHeight;
        private Paint mPaint;
        private BitmapFactory.Options ops;
        private Rect mRect, mClipRect;
        private int mPosition = 20;
        private int mPicPosition = 0;
        private int mStep = 5;
        private int mBamHeight = 600;
        

        public RZSurfaceView(Context context) {
            super(context);
            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.renzhe, ops);
        }

        @Override
        public void run() {
            // TODO Auto-generated method stub
            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(100);  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
            }  
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            /*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
            mWidth = getWidth();  
            mHeight = getHeight();  
            mRect = new Rect(0, 0, mWidth, mHeight);  
            new Thread(this).start();
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            
        } 
    }
}

忍者奔跑圖片this

 

程序運行效果(靜態),由於沒法截取動態奔跑spa

相關文章
相關標籤/搜索