我试图通过拉伸(stretch)图像以覆盖整个区域来将可绘制图像设置为 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)
最佳答案
在 XML 中设置 LinearLayout 的背景似乎不受此内存不足问题的影响(可能图像以不同的方式加载)。
无论如何,在我的例子中,仅使用这个就可以在布局文件中解决它:
android:background="@drawable/cash_bg"
我不知道它如何居中(以及它是否会覆盖整个容器)。
关于android - 将背景可绘制对象设置为 LinearLayout 会导致 OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12404917/