剪切动画案例

材料图片:

代码:

package sdsdf.u;

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.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;

public class MySurfaceView extends SurfaceView implements Callback, Runnable {
    private SurfaceHolder sfh;
    private Paint paint;
    private Thread th;
    private boolean flag;
    private Canvas canvas;
    //声明位图
    private Bitmap bmpClipBmp;
    //声明当前帧
    private int cureentFrame;

    /**
     * SurfaceView初始化函数
     */
    public MySurfaceView(Context context) {
        super(context);
        sfh = this.getHolder();
        sfh.addCallback(this);
        paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setAntiAlias(true);
        setFocusable(true);
    }

    /**
     * SurfaceView视图创建,响应此函数
     */
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        bmpClipBmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.loading2);
        flag = true;
        //实例线程
        th = new Thread(this);
        //启动线程
        th.start();
    }

    /**
     * 游戏绘图
     */
    public void myDraw() {
        try {
            canvas = sfh.lockCanvas();
            if (canvas != null) {
                canvas.drawColor(Color.WHITE);
                canvas.save();
                //设置画布可视区域(大小是每帧的大小)
                canvas.clipRect(10, 10, bmpClipBmp.getWidth() / 10+10, bmpClipBmp.getHeight()+10);
                //绘制位图
                canvas.drawBitmap(bmpClipBmp, -cureentFrame * bmpClipBmp.getWidth() / 10+9, 10, paint);
                canvas.restore();
            }
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            if (canvas != null)
                sfh.unlockCanvasAndPost(canvas);
        }
    }

    /**
     * 触屏事件监听
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }

    /**
     * 按键事件监听
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 游戏逻辑
     */
    private void logic() {
        cureentFrame++;
        if(cureentFrame>=10){
            cureentFrame=0;
        }
    }

    @Override
    public void run() {
        while (flag) {
            long start = System.currentTimeMillis();
            myDraw();
            logic();
            long end = System.currentTimeMillis();
            try {
                if (end - start < 100) {
                    Thread.sleep(100 - (end - start));
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * SurfaceView视图状态发生改变,响应此函数
     */
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }

    /**
     * SurfaceView视图消亡时,响应此函数
     */
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        flag = false;
    }
}
引用方式:直接添加到视图中就行   
  LinearLayout mainlayout=  (LinearLayout)      findViewById(R.id.mainLayout);
     mainlayout.addView(new MySurfaceView(this));
View Code

效果:

原文地址:https://www.cnblogs.com/clarence/p/3580393.html