我正在尝试通过拉伸图像以覆盖整个区域而不扭曲其比例来将可绘制图像设置为 LinearLayout 的背景(这意味着图像的某些侧面可能超出屏幕,这对我来说很好)。
@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);
}
我在onWindowFocusChanged
而不是得到布局的宽度和高度onCreate
,因为我听说否则它们将返回 0(因为布局没有在构造函数中完全呈现)。图像不是那么大,所以我不知道为什么会出现这种内存不足的问题。它是 800x945px JPG,大小为 113kb。
这是错误日志:
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)