Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Declaring static bitmaps? Yay or nay?

Somebody recently commented on my code where I declare the following:

private Bitmap splashBackground;
private Bitmap lightDot;
private static Bitmap scaledBackground;
private static Bitmap scaledLightDot;

They advised me against declaring satic Bitmaps.

However, I've tried everything and my code doesn't work unless I declare them as static.

Also, "public static Bitmap createScaledBitmap (Bitmap src, int dstWidth, int dstHeight, boolean filter)" seems to appear on the official Android Developer site so I'm a bit confused about what I should and shouldn't be doing.

Any pointers would be appreciated - thank you

Edit: For clarification:

When I remove the static from my declaration, then by the time I get to my onDraw() method, the scaled bitmap is null. (I am creating the scaled bitmap object in an initialise() method and once it's been created, it is valid (ie, not null) - but then seems to become null at onDraw unless I declare it as static.

I am calling my initialise() method from my activity class.

Edit: More code as requested.

My OnCreate method: As you can see, I'm passing my screen height and width over so I can create my scaled bitmaps

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        displaySplashScreen= new SplashScreen(this);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // set View
        setContentView(new SplashScreen(this));
        WindowManager w = getWindowManager();   
        Display d = w.getDefaultDisplay();
        int width=d.getWidth();
        int height=d.getHeight();
        displaySplashScreen.initialise(width, height);
}

My initalise method:

 public void initialise(int w, int h)
 {       
     //Get width and height (passed in from Activity)

     vwidth=w;
     vheight=h;

     //Create pre-scaled bitmaps

    scaledBackground = Bitmap.createScaledBitmap(splashBackground,  vwidth, vheight, true);
    scaledLightDot = Bitmap.createScaledBitmap(lightDot, vwidth, vheight, true);

 }

I could add also that if I use a standard variable in the same way (say int number;) and set it in initalise (number = 5;), then number is only equal to 5in my initialise method. If I log it from onDraw() it will always repeatedly return '0'!! It's baffling.

Thanks everyone so far, please let me know if more code is required......

like image 429
Zippy Avatar asked Mar 23 '26 03:03

Zippy


2 Answers

In general, utilizing static for Bitmaps is a very bad idea. There are a lot of good reasons for this, mostly having to do with avoiding memory leaks.

Also, "public static Bitmap createScaledBitmap (Bitmap src, int dstWidth, int dstHeight, boolean filter)" seems to appear on the official Android Developer site ...

This is not a static Bitmap. This is a method call to a class method. Static does not work the same and the return type (Bitmap) is not static. What this means is that the method is static and does not require an instance to be called. It will return a Bitmap to be placed in an appropriate variable of your choice.

I am calling my initialise() method from my activity class.

This statement is quite unhelpful. From where in the class is it being called? Is it in onCreate(), onStart(), onResume(), some other custom method? Where and when you choose to do certain things can have a huge effect as to how successful they are.

... but then seems to become null at onDraw unless I declare it as static.

This could be for several possible reasons, and since we don't have any of your code there really isn't a qualified answer. Here are some things to look at.

  • This could be because the Activity is getting recreated.
  • This could also be because some method that seems unrelated is actually getting called. This would probably be somewhere that you are manually setting it to null.
  • This might be due to improper use of the createScaledBitmap() method.
  • The Bitmap might be getting recycled due to low memory (this actually happens more often than one would think)

EDIT: After reviewing your code

This looks like it may be the culprit. Above, you have...

displaySplashScreen= new SplashScreen(this);

Below, you add...

setContentView(new SplashScreen(this));

This means that you are creating two Splashscreens. One reason why you are getting a null pointer when you are not using static may be because further down the line you use...

displaySplashScreen.initialise(width, height);

But since your contentView is set to a new SplashScreen, you are not actually utilizing that View. To resolve this, make sure you are talking to the same view object. I.E.

setContentView(displaySplashScreen);

This will at least make sure you are looking at the same object. It is possible that you may have to reorganize a bit depending upon what other things are happening. For instance,

setContentView(displaySplashScreen);

... may have to appear below ...

 displaySplashScreen.initialise(width, height);

This is something that you might have to toy with, but I don't see anything else that gives any other immediate indication. Be aware that resolving nullpointerexceptions will often result in revealing more errors in code, at first. Stay the course and resolve each in order.

like image 59
Fuzzical Logic Avatar answered Mar 25 '26 16:03

Fuzzical Logic


This line is wrong:

    // set View
    setContentView(new SplashScreen(this));        // This line is wrong.

Should be this:

    // set View
    setContentView(displaySplashScreen);        // displaySplashScreen is created earlier.

You are created two instances of SplashScreen. You should keep using the same instance.

like image 24
Pang Avatar answered Mar 25 '26 17:03

Pang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!