android一连串图片组成动画

/**
 * Created by Bellion on 2016/3/21.
 */
public class PersonRunSurface extends SurfaceView implements SurfaceHolder.Callback {

    private static final String TAG = "PersonGoSurface";
    private Context mContext;
    private SurfaceHolder mHolder;
    private int widthPixels;
    private String[] runs;

    public PersonRunSurface(Context context) {
        super(context);
        init(context);
    }

    public PersonRunSurface(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public PersonRunSurface(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        //1 获取Holder
        mHolder = getHolder();

        //2.添加回调
        mHolder.addCallback(this);

        //3获取屏幕宽高 以及资源文件
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(metrics);
        widthPixels = metrics.widthPixels;
        int heightPixels = metrics.heightPixels;
        Log.i(TAG, "widthPixels:" + widthPixels + "heightPixels:" + heightPixels);
        try {
            runs = context.getAssets().list("run");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //重写的SurfaceHolder.Callback的方法
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        thread.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    Thread thread = new Thread(){
        @Override
        public void run() {
            super.run();
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            boolean isRun = true;
            boolean direction = true;
            int position = 0;
            float locationX = 0;
            float step = 13;
            while(isRun){
                Canvas canvas = mHolder.lockCanvas();
                Paint clearPaint = new Paint();
                clearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
                canvas.drawPaint(clearPaint);
                clearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
                int imgWidth = 0;

                try {
                    InputStream open = mContext.getAssets().open("run" + "/" + runs[position]);
                    Bitmap bitmap = BitmapFactory.decodeStream(open);
                    imgWidth = bitmap.getWidth();
                    canvas.drawBitmap(bitmap,locationX,200,paint);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                mHolder.unlockCanvasAndPost(canvas);
                position ++;
                //判断方向
                if (direction) {
                    locationX += step;
                } else {
                    locationX -= step;
                }
                //由当前位置判断应该行走的方向
                if (locationX >= widthPixels - imgWidth/2) {
                    direction = false;
                }else if (locationX <= (-imgWidth/2)) {
                    direction = true;
                }
                try {
                    sleep(40);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                //加载某张图片
                if (position >= runs.length) {
                    position = 0;
                }

            }

        }
    };
}

图片位置


演示图片:

百度云盘下载


演示截图

      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值