- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
情况是这样的:我有一个具有三种不同布局的 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/
背景 我一直在使用ViewAnimator/ViewSwitcher很长一段时间。 我最常见的用例是从加载阶段切换到内容阶段,或者在向导的阶段之间切换,有时甚至会出现错误阶段。 问题 当我建议向“an
情况是这样的:我有一个具有三种不同布局的 ViewAnimator。每个布局都有不同的背景图像。对于 xhdpi 设备,图像大小为 1280x800 像素,大小约为 150KB。现在的问题是:当我以
我想使用 ViewAnimator 从一个 View 过渡到另一个 View (在我的测试应用中, View 是 TextView)。下面列出了我的两个动画。我看到的行为是两种动画都在我触发动画制作器
我有一个 ViewAnimator(准确地说是 ViewSwitcher),它带有淡入/淡出动画和两个 View,我在它们之间进行转换使用 ViewAnimator.showNext() 和 View
这是我的问题: 我有一个像这样的 ViewAnimator: 当我为当前之后的 View stub 设置动画时,没有问题。问题是,当我之前尝试为 viewstub 制
我将与以下之一合作: ViewAnimator ViewFlipper ViewSwitcher 它们都有一种方法 setDisplayedChild 可以将 View 从一个 View 更改为另一个
这不是一个问题,更像是和别人分享我遇到的一个问题以及我是如何解决的。 基本上,我试图创建一个 ViewAnimator,它会创建额外的子项以响应用户点击。 为了在我对下一个 View 进行动画处理后进
问题:我在运行 Espresso 测试时遇到问题,在登录按钮上调用 perform(click()) 方法后,测试会继续运行,但不会继续运行,直到 45 秒过去并且测试自动失败。同时,登录正常进行。
我不知道为什么,但我突然得到这个警告,我以前没有得到的: CoreAnimation: stiffness must be greater than 0. CoreAnimation: damping
我是一名优秀的程序员,十分优秀!