2

First I would like to say please do not mark this as a duplicate as this question is different from most others. I am trying to create an artificial screensaver of sorts. I am trying to accomplish this by creating an activity with a ViewFlipper, throwing some pictures in there, and then setting the flipInterval to get things rolling. When I try to run this activity, I get an OutOfMemory error. Most suggestions say reducing the size of the picture, but that is not possible as the pictures I have already included are a minimum size. Also their total file sizes range from 20kb-70kb. I have a total of six images now, but the more images I load into my application for other activities the fewer pictures I seem to be able to play in my screensaver. Is there some way that I am increase the number of pictures? Maybe calling `.finish()' on other activities? I'm obviously not sure. Any solutions?

Thanks everyone!

2
  • Please tell the images you used in viewflipper are of same dimension(width and height)? Commented Jun 4, 2016 at 7:31
  • @ManishJain all of my images are 1280p x 720p Commented Jun 4, 2016 at 16:14

1 Answer 1

3

Hope this may be useful

public class AnimateBackgroundActivity extends Activity{

protected int[] backgroundImages;
protected ImageView mainBackground;
protected boolean isRunning;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    isRunning = true;
    backgroundImages = new int[6];
    backgroundImages[0] = R.drawable.background0;
    backgroundImages[1] = R.drawable.background1;
    backgroundImages[2] = R.drawable.background2;
    backgroundImages[3] = R.drawable.background3;
    backgroundImages[4] = R.drawable.background4;
    backgroundImages[5] = R.drawable.background5;
}

protected void animateBackground()
{
    Runnable animate = new Runnable() {

        public void run() {
            for (int i = 0; isRunning; i = (++i % backgroundImages.length)) {
                final int j = i;
                runOnUiThread(new Runnable() {
                    public void run() {
                        mainBackground.
                        setImageResource(
                                backgroundImages[j]);
                    }
                });

                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {

                }
            }
        }
    };

    Thread animBackgroundThread = new Thread(animate);
    animBackgroundThread.start();
}

@Override
protected void onResume() {
    isRunning = true;
    animateBackground();
    super.onResume();
}

@Override
protected void onDestroy() {
    isRunning = false;
    super.onDestroy();
}

@Override
protected void onPause() {
    isRunning = false;
    super.onPause();
}
}


public class InitScreen extends AnimateBackgroundActivity {


private void initImageLoader() {
    Context context = getApplicationContext();

    DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory().cacheOnDisc().build();

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).memoryCacheExtraOptions(480, 800)
            // default
            // =
            // device
            // screen
            // dimensions
            .discCacheExtraOptions(480, 800, CompressFormat.JPEG, 100).memoryCache(new LruMemoryCache(4 * 1024 * 1024)).memoryCacheSize(4 * 1024 * 1024).discCacheSize(50 * 1024 * 1024).discCacheFileCount(1000).defaultDisplayImageOptions(options).build();

    ImageLoader.getInstance().init(config);

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    initImageLoader();

    setContentView(R.layout.inital);
    mainBackground = (ImageView) findViewById(R.id.init_screen_background);
    animateBackground();

}
Sign up to request clarification or add additional context in comments.

1 Comment

I can't seem to import DisplayImageOptions, ImageLoaderConfiguration LruMemoryCache or ImageLoader

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.