My AnimationDrawable object's run method slows down my application, any solutions?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



My AnimationDrawable object's run method slows down my application, any solutions?



I am writing a basic Android app. I did create an AnimationDrawable object to display my multiple images in my layout and set it. After that, my application started to slow down. And i even tried to work that code block on new thread but it didn't work as i imagined. What did I do wrong? or Is there any solution to that? Thanks in advance.



I'm calling this method on my onCreate() method;


/*
* This method configures the background animation
* */
private void configureAnimation()
animation = new AnimationDrawable();
for (int i = 0; i < bgImage.length; i++)
animation.addFrame(getResources().getDrawable(bgImage[i]), 5000);

animation.setEnterFadeDuration(1000);
animation.setOneShot(false);

linearLayout = (LinearLayout) findViewById(R.id.mLayout);
linearLayout.setBackground(animation);

animation.start();



Whole Class;


package com.example.yekta.loginapp;

import android.graphics.drawable.AnimationDrawable;
import android.os.Build;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Toast;



public class MainActivity extends AppCompatActivity

private LinearLayout linearLayout;
private AnimationDrawable animation;
private int bgImage = R.drawable.wallpaper00, R.drawable.wallpaper01;

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.placeholder_fragment, new LoginFragment());
ft.commit();

hideActionBar();
changeStatusBarColor(R.color.black);

// First Approach
//configureAnimation();

// Second Approach with using Thread
//startProgress();

// Third Approach
animation = new AnimationDrawable();
handleAnimation(100,animation);
animation.start();



/*
* This is simple method that changes the color of status bar.
* @param colorCode is the color code that given in value file
* */
private void changeStatusBarColor(final int colorCode)
if (Build.VERSION.SDK_INT >= 21)
Window window = this.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(ContextCompat.getColor(this, colorCode));



/*
* This is simple method that hides the Action bar.
* */
private void hideActionBar()
ActionBar actionBar = getSupportActionBar();
actionBar.hide();


/*
* This method configures the background animation
* */
private void configureAnimation()
animation = new AnimationDrawable();
for (int i = 0; i < bgImage.length; i++)
animation.addFrame(getResources().getDrawable(bgImage[i]), 5000);

animation.setEnterFadeDuration(1000);
animation.setOneShot(false);

linearLayout = (LinearLayout) findViewById(R.id.mLayout);
linearLayout.setBackground(animation);

animation.start();


public void startProgress()
final Handler handler = new Handler();
handler.post(new Runnable()
@Override
public void run()
configureAnimation();

);


private void handleAnimation(final int time, final AnimationDrawable animation)
Handler handler = new Handler();
handler.postDelayed(new Runnable()
@Override
public void run()
for (int i = 0; i < bgImage.length; i++)
animation.addFrame(getResources().getDrawable(bgImage[i]), 5000);

animation.setEnterFadeDuration(1000);
animation.setOneShot(false);
linearLayout = (LinearLayout) findViewById(R.id.mLayout);
linearLayout.setBackground(animation);

, time);






what is the value of bgImage.length? and also how big the background images are
– Lino
Aug 10 at 15:55



bgImage.length





@Lino There are only two images in that array at the moment so it is 2, right now.
– Yekta Sarıoğlu
Aug 10 at 16:01





Let me see, you want to make a splash screen animation before the login screen ?, Please remove the animation.start() from OnCreate
– Daniel
Aug 10 at 18:05





2 Answers
2



I think it is safer to call it onResume(), if you start the animation on OnCreate() you will probably get ANR, and you could try this approach:


animation = new AnimationDrawable();
animation.start();
handleAnimation(100,animation);

private void handleAnimation(final int time, final AnimationDrawable animationDrawable)
Handler handler = new Handler();
handler.postDelayed(new Runnable()
@Override
public void run()
for (int i = 0; i < bgImage.length; i++)
animation.addFrame(getResources().getDrawable(bgImage[i]), 5000);

animation.setEnterFadeDuration(1000);
animation.setOneShot(false)
linearLayout = (LinearLayout) findViewById(R.id.mLayout);
linearLayout.setBackground(animation);

, time);





Still the same. I even tried to switch animation.start() line and handleAnimation() implementation line between them. But UI still runs considerably slow.
– Yekta Sarıoğlu
Aug 10 at 17:12






Can you post the complete class ?
– Daniel
Aug 10 at 17:21





I edited my post just now.
– Yekta Sarıoğlu
Aug 10 at 17:30



I find my own problem :D. And that was a stupid problem which based on asset plugin that I installed to Android Studio, Android Drawable Importer. Problem is image was so huge to load to HD Screen Emulator so it slowed down the application naturally. When I configure it to normal sizes that as it should be, it solved my problem. I am so sorry to take your time :D. Thank you to everyone who tried to help me out. Happy coding everyone.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard