1.左右移动小人

public class MySurfaceView2 extends SurfaceView implements SurfaceHolder.Callback,Runnable {    Thread th;    SurfaceHolder sh;    private   boolean flag=true;    //    Bitmap[] bmaps=new Bitmap[6];    Bitmap bmaps;    int curFrame=0,x=200,y=200;    int dir=4;    //当点击手机屏幕时促发    @Override    public boolean onTouchEvent(MotionEvent event) {        if(event.getX()>x){dir=8;}        else {dir=4;}        return super.onTouchEvent(event);    }    //图片初始化    public MySurfaceView2(Context context) {        super(context);        sh=getHolder();        sh.addCallback(this);        bmaps= BitmapFactory.decodeResource(getResources(),R.drawable.zhujue1);    }    @Override    public void surfaceCreated(SurfaceHolder holder) {        th=new Thread(this);        th.start();    }    @Override    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {    }    @Override    public void surfaceDestroyed(SurfaceHolder holder) {flag=false;}    public void logi(){        curFrame++;        if(curFrame>=4)curFrame=0;        if (dir==4){ x -=5; if (x<0)x=0;}        if (dir==8){ x +=5; if (x>600)x=600;}    }    //绘图    public void Draw(Canvas cv){        cv.drawColor(0xFFaaaaaa);        int framew=bmaps.getWidth()/4;        int frameh=bmaps.getHeight()/4;        int dx=(dir+curFrame)% 4 * framew;        int dy=(dir+curFrame)/4 * frameh;        cv.save();        cv.clipRect(x,y,x+framew,y+frameh);        cv.drawBitmap(bmaps, x-dx,y-dy,null);        cv.restore();    }    @Override    public void run() {        while(flag){            Canvas cv=sh.lockCanvas();            Draw(cv);            logi();            sh.unlockCanvasAndPost(cv);            try {                Thread.sleep(200);            } catch (Exception e){                e.printStackTrace();            }        }    }}

2.跑动的忍者神龟

public class MySurfaceView3 extends SurfaceView implements SurfaceHolder.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 MySurfaceView3(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    }}

参考:https://blog.csdn.net/weixin_42562102/article/details/81262642

https://www.cnblogs.com/wisher/archive/2013/08/31/3293065.html

https://www.2cto.com/kf/201704/634779.html

更多相关文章

  1. android绘图之Paint(1)
  2. Android(安卓)筆記-Linux Kernel SMP (Symmetric Multi-Processo
  3. Android(安卓)TextToSpeech语音播放文本
  4. [cocos2d-x 学习] Scene(场景)学习
  5. Android学习--Android带删除按钮的ListView
  6. 说说在 Android(安卓)如何在全局获取 Context
  7. Android中添加布局和初始化布局总结
  8. ActivityGroup + ViewPager 实现可滑动的底部Tab
  9. Android初始化语言 (init.*.rc、init.conf文件格式)

随机推荐

  1. Android: Bluetooth profile
  2. ANDROID截屏
  3. Android(安卓)Verified Boot 2.0
  4. Android(安卓)快速模糊
  5. android SQLite使用
  6. android获取指定路径下目录文件
  7. Appcelerator Cloud Push Notification i
  8. Android(安卓)Edittext Inputtype
  9. Android(安卓)Dependencies 是什么?
  10. Android(安卓)快速启动