Create animated splash screen using frames on Android

后端 未结 3 1272
逝去的感伤
逝去的感伤 2020-12-19 19:20

So here\'s the deal, I\'ve searched every single question and link online but none are helpful. I have 120 frames of an animation in .jpg format for my splash screen. I unde

3条回答
  •  情书的邮戳
    2020-12-19 19:35

    You can try to do it without AnimationDrawable using Handler.postDelayed. Something like this:

    final ImageView image = (ImageView) findViewById(R.id.SplashImageView);
    final Handler handler = new Handler();
    
    final Runnable animation = new Runnable() {
        private static final int MAX = 120;
        private static final int DELAY = 50;
    
        private int current = 0;
    
        @Override
        public void run() {
            final Resources resources = getResources();
            final int id = resources.getIdentifier("l" + current, "drawable", getPackageName());
            final Drawable drawable = resources.getDrawable(id);
    
            image.setBackgroundDrawable(drawable);
            handler.postDelayed(this, DELAY);
            current = (current + 1) % MAX;
        }
    };
    
    handler.post(animation);
    

    This solution requires less memory because it keeps only one drawable at the time.

    You can cancel the animation using handler.removeCallbacks(animation);.

    If you want make a one-shot animation you can call handler.postDelayed conditionally:

    if (current != MAX - 1) {
        handler.postDelayed(this, DELAY);
    }
    

提交回复
热议问题