如何在android中的位图中设置计时器

时间:2014-03-30 04:51:39

标签: android bitmap android-animation

这是我的代码,我试图使用位图添加动画到我的背景.. !!我能够动画一切,但问题是我不知道如何在一定时间后启动特定的图像..所有的位图图像( heartfal,heartfal1,heartfal2,heartfal3)同时开始。我需要为某些bmp图像设置计时器,所以我可以比其他图像启动几秒钟。而且所有的bmp图像都是从顶部开始的.. !我需要很少的图像才能从某个地方开始>我不知道要改变高度>如果我给“40”(说)代替“改变y”我能够改变位置但是bmp图像没有移动..它仍然在相同的“40”位置..我刚接触到android plz帮助.. !!提前致谢

public class Mybringback extends View {
        Bitmap heartsfal;
        Bitmap heartsfal2;
        Bitmap heartsfal3;
        Bitmap heartsfal4;
        float changingY;

        public Mybringback(Context context, AttributeSet attrs) {
            super(context);

            heartsfal = BitmapFactory.decodeResource(getResources(),
                    R.drawable.animation1);
            heartsfal2 = BitmapFactory.decodeResource(getResources(),
                    R.drawable.animation2);
            heartsfal3 = BitmapFactory.decodeResource(getResources(),
                    R.drawable.animation3);
            heartsfal4 = BitmapFactory.decodeResource(getResources(),
                    R.drawable.animation4);
            changingY = 0;

        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            canvas.drawBitmap(heartsfal, 20, changingY, null);
            if (changingY < canvas.getHeight()) {
                changingY += 1;
            } else {
                changingY = 0;
            }
            canvas.drawBitmap(heartsfal2, 120, changingY, null);
            if (changingY < canvas.getHeight()) {
                changingY += 1;
            } else {
                changingY = 0;
            }
            canvas.drawBitmap(heartsfal3, 260, changingY, null);
            if (changingY < canvas.getHeight()) {
                changingY += 1;
            } else {
                changingY = 0;
            }
            canvas.drawBitmap(heartsfal4, 400, changingY, null);
            if (changingY < canvas.getHeight()) {
                changingY += 1;
            } else {
                changingY = 0;
            }

            invalidate();
        }
    }

1 个答案:

答案 0 :(得分:0)

private ScheduledThreadPoolExecutor execAds = null;

private void startCheckTimer() {
        execAds = new ScheduledThreadPoolExecutor(1);       
        execAds.scheduleAtFixedRate(new MyTaskAds(), 0, 10, TimeUnit.MILLISECONDS);
    }

private class MyTaskAds implements Runnable {

        @Override
        public void run() {
            changingY += 1;
            invalidate();
        }
    }

调用startCheckTimer(),changingY将移动....(如果需要,可以更改时间延迟。)