gpt4 book ai didi

Android ViewAnimator 和 OutOfMemoryError

转载 作者:太空狗 更新时间:2023-10-29 15:16:35 28 4
gpt4 key购买 nike

情况是这样的:我有一个具有三种不同布局的 ViewAnimator。每个布局都有不同的背景图像。对于 xhdpi 设备,图像大小为 1280x800 像素,大小约为 150KB。现在的问题是:当我以 1280x800 的大小和 320dp 的密度启动模拟器时,我总是收到此异常:

09-02 08:22:46.408: D/dalvikvm(1291): GC_EXTERNAL_ALLOC freed 428K, 51% free 2874K/5831K, external 18484K/20407K, paused 83ms
09-02 08:22:46.419: E/dalvikvm-heap(1291): 1366400-byte external allocation too large for this process.
09-02 08:22:46.498: I/dalvikvm-heap(1291): Clamp target GC heap from 25.089MB to 24.000MB
09-02 08:22:46.498: E/GraphicsJNI(1291): VM won't let us allocate 1366400 bytes
09-02 08:22:49.208: E/AndroidRuntime(1291): java.lang.OutOfMemoryError: bitmap size exceeds VM budget

有人可以解决我的问题吗?我阅读了一些关于 BitmapFactory 的内容。有帮助吗?最好的问候!

编辑:这是我的代码:布局文件main_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >

<ViewAnimator
android:id="@+id/viewAnimator"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:id="@+id/LayoutMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:background="@drawable/background"
...some Buttons etc. ...
</LinearLayout>

<LinearLayout
android:id="@+id/layoutGameMenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:background="@drawable/gameMenu"
...some Buttons etc. ...
</LinearLayout>

<LinearLayout
android:id="@+id/layoutHighscores"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/highscores"
...some ListViews etc. ...
</LinearLayout>

</ViewAnimator>
</LinearLayout>

和 MainActivity:

private ViewAnimator va;   

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
va = (ViewAnimator) findViewById(R.id.viewAnimator);
....
}

....
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
if (va.getCurrentView() == findViewById(R.id.layoutHighscores)) {
va.setInAnimation(inFromRight);
va.setOutAnimation(outToLeft);
va.setDisplayedChild(0);
return true;
}
if (va.getCurrentView() == findViewById(R.id.layoutGameMenu)) {
va.setInAnimation(inFromLeft);
va.setOutAnimation(outToRight);
va.setDisplayedChild(0);
return true;
}
}
return super.onKeyDown(keyCode, event);
}

有了这个,我可以通过按钮在主菜单、高分和游戏菜单之​​间切换以启动游戏模式。我选择它是因为我想要“酷”的 View 动画。

我尝试用 BitmapFactory 方法解决它,但没有任何变化。

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {

// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}

最佳答案

在主要 Android 站点的 Android 培训 部分中有一节涵盖这种情况的类(class)。教训,Displaying Bitmaps Efficiently ,分为四个部分,包括工作示例,并支持 API 级别 7 及更高级别(我认为这不是问题。)

如果这没有帮助,我建议发布一些代码以便提供更具体的帮助。 ;-)

关于Android ViewAnimator 和 OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12234531/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com