Android 动画绘制移动的小人

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

猜你喜欢

转载自blog.csdn.net/u011489887/article/details/101326124