I am trying to set a drawable image as a background to a LinearLayout by stretching the image to cover the entire area, without distorting its proportions (meaning that some of the sides of the image may be out of the screen, which is fine by me).
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
LinearLayout mainLL = (LinearLayout) findViewById(R.id.main_ll);
Integer lWidth = mainLL.getWidth();
Integer lHeight = mainLL.getHeight();
// Read your drawable from somewhere
Drawable dr = getResources().getDrawable(R.drawable.cash_bg);
Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
// Scale it the LinearLayout's size
Drawable d = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap, lWidth, lHeight, true));
mainLL.setBackgroundDrawable(d);
}
I am getting the layout’s width and height in the onWindowFocusChanged instead of onCreate, because I heard otherwise they will return 0 (since the layout is not fully rendered in the constructor). The image is not that large so I don’t know why this out of memory issue occurs. It’s 800x945px JPG with 113kb size.
Here’s the error log:
09-13 11:00:00.067: E/AndroidRuntime(2010): FATAL EXCEPTION: main
09-13 11:00:00.067: E/AndroidRuntime(2010): java.lang.OutOfMemoryError
09-13 11:00:00.067: E/AndroidRuntime(2010): at android.graphics.Bitmap.nativeCreate(Native Method)
09-13 11:00:00.067: E/AndroidRuntime(2010): at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
09-13 11:00:00.067: E/AndroidRuntime(2010): at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
09-13 11:00:00.067: E/AndroidRuntime(2010): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
09-13 11:00:00.067: E/AndroidRuntime(2010): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:524)
09-13 11:00:00.067: E/AndroidRuntime(2010): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:499)
09-13 11:00:00.067: E/AndroidRuntime(2010): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
09-13 11:00:00.067: E/AndroidRuntime(2010): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
09-13 11:00:00.067: E/AndroidRuntime(2010): at android.content.res.Resources.loadDrawable(Resources.java:1937)
09-13 11:00:00.067: E/AndroidRuntime(2010): at android.content.res.Resources.getDrawable(Resources.java:664)
09-13 11:00:00.067: E/AndroidRuntime(2010): at com.myapp.something.FVCalculator.onWindowFocusChanged(FVCalculator.java:66)
09-13 11:00:00.067: E/AndroidRuntime(2010): at com.android.internal.policy.impl.PhoneWindow$DecorView.onWindowFocusChanged(PhoneWindow.java:2346)
09-13 11:00:00.067: E/AndroidRuntime(2010): at android.view.View.dispatchWindowFocusChanged(View.java:5676)
09-13 11:00:00.067: E/AndroidRuntime(2010): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:853)
09-13 11:00:00.067: E/AndroidRuntime(2010): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2530)
09-13 11:00:00.067: E/AndroidRuntime(2010): at android.os.Handler.dispatchMessage(Handler.java:99)
09-13 11:00:00.067: E/AndroidRuntime(2010): at android.os.Looper.loop(Looper.java:137)
09-13 11:00:00.067: E/AndroidRuntime(2010): at android.app.ActivityThread.main(ActivityThread.java:4340)
09-13 11:00:00.067: E/AndroidRuntime(2010): at java.lang.reflect.Method.invokeNative(Native Method)
09-13 11:00:00.067: E/AndroidRuntime(2010): at java.lang.reflect.Method.invoke(Method.java:511)
09-13 11:00:00.067: E/AndroidRuntime(2010): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-13 11:00:00.067: E/AndroidRuntime(2010): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-13 11:00:00.067: E/AndroidRuntime(2010): at dalvik.system.NativeStart.main(Native Method)
It seems like setting the background of the LinearLayout in the XML is not affected by this out-of-memory issue (maybe the image is loaded in a different manner).
Anyway in my case using just this solved it in the layout file solved it:
I don’t know how it will center though (and if it will cover the entire container).