安卓之在SurfaceView中实现动画效果

之前用安卓的Animation实现过一个刷新的动画效果,具体链接在此:animation动画

但是当把这样的动画效果嵌入进整个项目代码中后,有可能会因为主线程任务的繁重而导致动画效果发生卡顿。

下面换了一种方法来实现,在这里是利用SurfaceView这个类来实现的。关于SurfaceView的了解,可以点这个链接:安卓之SurfaceView


下面贴上实现代码,实现效果跟animation动画一样。


MainActivity的代码:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(new MySurfaceView(this));
    }
}


MySurfaceView类的代码:

public class MySurfaceView extends SurfaceView implements Callback, Runnable {
    private Thread th = new Thread(this);
    private SurfaceHolder sfh;
    private int screenHeight, screenWidth;
    private static final int mVISIBLE = 1, mINVISIBLE = 0, rightUp = 2, rightDown = 3, leftDown = 4, leftUp = 5;
    private static int animationFlag = rightUp; // 移动图片起初位于中间布局的左上方,刚开始准备向布局的右上方移动
    private static boolean occurFlag = true; //  设置浅颜色图片的出现标志, 初始为不出现
    private Canvas canvas;
    private static final int delayTime = 10;
    private Paint paint, transparentPaint;
    private Resources res;
    private Bitmap bmpDong, bmpJing_1, bmpJing_2, bmpJing_3, bmpJing_4;
    private int bmp_x, bmp_y;
    private static final float offsetValue = 2;
    private static float pic_1_location[] = {0, 0};
    private static float pic_2_location[] = {0, 0};
    private static float pic_3_location[] = {0, 0};
    private static float pic_4_location[] = {0, 0};
    private static float pic_Dong_location[] = {0,0};
    public MySurfaceView(Context context) {
        super(context);
        res = this.getResources();
        bmpDong = BitmapFactory.decodeResource(res, R.drawable.loading_pic1);
        bmpJing_1 = BitmapFactory.decodeResource(res, R.drawable.loading_pic2);
        bmpJing_2 = BitmapFactory.decodeResource(res, R.drawable.loading_pic2);
        bmpJing_3 = BitmapFactory.decodeResource(res, R.drawable.loading_pic2);
        bmpJing_4 = BitmapFactory.decodeResource(res, R.drawable.loading_pic2);

        sfh = this.getHolder();
        sfh.addCallback(this);
        paint = new Paint();
        paint.setColor(Color.YELLOW);
        paint.setAntiAlias(true);

        transparentPaint = new Paint();
        transparentPaint.setStyle(Paint.Style.STROKE);  //设置空心
        transparentPaint.setAlpha(0);
        transparentPaint.setAntiAlias(true);

        setFocusable(true);  
    }
    public void surfaceCreated(SurfaceHolder holder) {
        screenHeight = this.getHeight();
        screenWidth = this.getWidth();
        bmp_x = bmpDong.getWidth();
        bmp_y = bmpDong.getHeight();
        pic_1_location[0] = screenWidth / 2 - bmp_x; pic_1_location[1] = screenHeight / 2 - bmp_y;
        pic_Dong_location[0] = screenWidth / 2 - bmp_x; pic_Dong_location[1] = screenHeight / 2 - bmp_y;
        pic_2_location[0] = screenWidth / 2 ; pic_2_location[1] = screenHeight / 2 - bmp_y;
        pic_3_location[0] = screenWidth / 2 ; pic_3_location[1] = screenHeight / 2 ;
        pic_4_location[0] = screenWidth / 2 - bmp_x; pic_4_location[1] = screenHeight / 2 ;

        th.start();
    }
    public void draw() {
        try {
            canvas = sfh.lockCanvas();
            canvas.drawColor(Color.WHITE);

            if (occurFlag == true && animationFlag == rightUp){
                drawBitmap1(canvas,mVISIBLE);
                drawBitmap2(canvas,mINVISIBLE);
                drawBitmap3(canvas,mINVISIBLE);
                drawBitmap4(canvas, mINVISIBLE);
            }else if (occurFlag == false && animationFlag == rightUp){
                drawBitmap1(canvas,mINVISIBLE);
                drawBitmap2(canvas,mVISIBLE);
                drawBitmap3(canvas,mVISIBLE);
                drawBitmap4(canvas, mVISIBLE);
            }
            if (occurFlag == true && animationFlag == rightDown){
                drawBitmap2(canvas,mVISIBLE);
                drawBitmap1(canvas, mVISIBLE);
                drawBitmap3(canvas,mINVISIBLE);
                drawBitmap4(canvas, mINVISIBLE);
            }else if (occurFlag == false && animationFlag == rightDown){
                drawBitmap1(canvas, mINVISIBLE);
                drawBitmap2(canvas,mINVISIBLE);
                drawBitmap3(canvas,mVISIBLE);
                drawBitmap4(canvas, mVISIBLE);
            }
            if (occurFlag == true && animationFlag == leftDown){
                drawBitmap3(canvas,mVISIBLE);
                drawBitmap1(canvas, mVISIBLE);
                drawBitmap2(canvas, mVISIBLE);
                drawBitmap4(canvas, mINVISIBLE);
            }else if (occurFlag == false && animationFlag == leftDown){
                drawBitmap3(canvas,mINVISIBLE);
                drawBitmap2(canvas,mINVISIBLE);
                drawBitmap1(canvas,mINVISIBLE);
                drawBitmap4(canvas, mVISIBLE);
            }
            if (occurFlag == true && animationFlag == leftUp) {
                drawBitmap4(canvas,mVISIBLE);
                drawBitmap2(canvas,mVISIBLE);
                drawBitmap3(canvas,mVISIBLE);
                drawBitmap1(canvas,mVISIBLE);
                if (pic_Dong_location[1] == pic_1_location[1]){
                    occurFlag = false;         //如果移动的图片回归到左上角位置,则开始浅颜色图片消失的过程
                }
            }else if (occurFlag == false && animationFlag == leftUp){
                drawBitmap4(canvas,mINVISIBLE);
                drawBitmap2(canvas,mINVISIBLE);
                drawBitmap3(canvas,mINVISIBLE);
                drawBitmap1(canvas,mINVISIBLE);
                if (pic_Dong_location[1] == pic_1_location[1]){
                    occurFlag = true;         //如果移动的图片回归到左上角位置,则开始浅颜色图片出现的过程
                }
            }

            drawBitmapDong(canvas, mVISIBLE);

            canvas.save();
        }catch (Exception ex){
        }finally {
            if (canvas != null){
                sfh.unlockCanvasAndPost(canvas);
            }
        }
    }

    public void cycle() {
        if (animationFlag == rightUp && pic_Dong_location[0]<pic_2_location[0]){
            pic_Dong_location[0]+=offsetValue;
        }else if (animationFlag == rightUp){
            animationFlag = rightDown;  //接着向布局的右下方移动,即向下移动
        }
        if (animationFlag == rightDown && pic_Dong_location[1]<pic_3_location[1]){
            pic_Dong_location[1]+=offsetValue;
        }else if (animationFlag == rightDown){
            animationFlag = leftDown;
        }
        if (animationFlag == leftDown && pic_Dong_location[0]>pic_4_location[0]){
            pic_Dong_location[0]-=offsetValue;
        }else if (animationFlag == leftDown){
            animationFlag = leftUp;
        }
        if (animationFlag == leftUp && pic_Dong_location[1]>pic_1_location[1]){
            pic_Dong_location[1]-=offsetValue;
        }else if (animationFlag == leftUp){
            animationFlag = rightUp;
        }
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        while (true) {
            draw();
            cycle();
            try {
                Thread.sleep(delayTime);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    private void drawBitmap1(Canvas canvas, int visibleOrNot){
        if (visibleOrNot == 0){
            canvas.drawBitmap(bmpJing_1, pic_1_location[0], pic_1_location[1], transparentPaint);
        }else if (visibleOrNot == 1){
            canvas.drawBitmap(bmpJing_1, pic_1_location[0], pic_1_location[1], paint);
        }
    }private void drawBitmap2(Canvas canvas, int visibleOrNot){
        if (visibleOrNot == 0){
            canvas.drawBitmap(bmpJing_2, pic_2_location[0], pic_2_location[1], transparentPaint);
        }else if (visibleOrNot == 1){
            canvas.drawBitmap(bmpJing_2, pic_2_location[0], pic_2_location[1], paint);
        }
    }private void drawBitmap3(Canvas canvas, int visibleOrNot){
        if (visibleOrNot == 0){
            canvas.drawBitmap(bmpJing_3, pic_3_location[0], pic_3_location[1], transparentPaint);
        }else if (visibleOrNot == 1){
            canvas.drawBitmap(bmpJing_3, pic_3_location[0], pic_3_location[1], paint);
        }
    }private void drawBitmap4(Canvas canvas, int visibleOrNot){
        if (visibleOrNot == 0){
            canvas.drawBitmap(bmpJing_4, pic_4_location[0], pic_4_location[1], transparentPaint);
        }else if (visibleOrNot == 1){
            canvas.drawBitmap(bmpJing_4, pic_4_location[0], pic_4_location[1], paint);
        }
    }private void drawBitmapDong(Canvas canvas, int visibleOrNot){
        if (visibleOrNot == 0){
            canvas.drawBitmap(bmpDong, pic_Dong_location[0], pic_Dong_location[1], transparentPaint);
        }else if (visibleOrNot == 1){
            canvas.drawBitmap(bmpDong, pic_Dong_location[0], pic_Dong_location[1], paint);
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // TODO Auto-generated method stub
    }
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
    }
}

实现的思想就是在run()里面不停调用darw()和cycle(),cycle是进行对绘制位置以及绘制标志的一些更改,这样在draw()里面可以根据相应的标志值来判断下一步需要绘制的

任务。更改图片移动速度的方法可以修改delayTime和offsetValue这两个值,delayTime用于延时时间的设置,offsetValue用于修改每次绘制图片的位置。最后的实现效果跟

利用Animation的TranslateAnimation实现效果完全一样,而且甚至可以获得比其更好地效果。让图片消失就是设置一支透明的画笔------TransparentPaint.

实现的算法逻辑可能有一些复杂,不过多看一下还是可以理解的!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值