简体   繁体   English

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

[英]How to set timer in bitmap in android

This is my code and im trying to add animation to my background using bitmap..!!im able to animate everything but the problem is i dont know how to start a particular image after certain time..All the bitmap images (heartfal,heartfal1,heartfal2,heartfal3)are starting at the same time.I need to set timer for certain bmp images so tat i can start a few seconds later than other images.And also all the bmp images are starting from the top..!!这是我的代码,我正在尝试使用位图将动画添加到我的背景中..!!我能够为所有内容设置动画,但问题是我不知道如何在特定时间后启动特定图像..所有位图图像(heartfal,heartfal1 ,heartfal2,heartfal3) 同时开始。我需要为某些 bmp 图像设置计时器,以便我可以比其他图像晚几秒钟开始。而且所有 bmp 图像都从顶部开始..!! I need few images to start somewhere>I dont know to change the height>If i give "40" (say)in place of "changing y" im able to change the position but bmp image is not moving ..Its satnding still in the same"40" position..Iam new to android plz help..!!Thanks in advance我需要很少的图像从某个地方开始>我不知道要改变高度>如果我给“40”(比如)代替“改变y”我可以改变位置但bmp图像没有移动..它仍然在相同的“40”位置..我是 android 新手请帮忙..!!提前致谢

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();
        }
    }
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();
        }
    }

Call startCheckTimer(), changingY will moving.... (Change time delay if you want.)调用 startCheckTimer(),changedY 将移动....(如果需要,更改时间延迟。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM