我有一个带有两个按钮和一个 ViewFlipper 的 android 活动。这两个按钮只是在 viewflipper 上翻转到下一个和上一个。
我想要在 38 个图像之间切换,因此我已将它们作为 ImageViews 添加到活动的 xml 文件中的 ViewFlipper 中。
出于某种原因,设备似乎有内存限制,当我添加超过 20 个图像视图时超出了该限制。它与 21 完美配合,但不适用于 25。图像只有 50-150 kb 之间,所以我真的不明白为什么华硕 Transformer Pad TF300T 不能处理它。这是java代码
import dk.fourway.divepool.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ViewFlipper;
public class FlipperFourActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set main.XML as the layout for this Activity
setContentView(R.layout.activity_flipper_four);
// Set the listener for Button_Next, a quick and dirty way to create a listener
Button buttonNext = (Button) findViewById(R.id.Button_next);
buttonNext.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
flipNext(view);
}
});
// Set the listener for Button_Previous, a quick and dirty way to create a listener
Button buttonPrevious = (Button) findViewById(R.id.Button_previous);
buttonPrevious.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
flipPrevious(view);
}
});
}
private void flipNext(View view){
// Get the ViewFlipper from the layout
ViewFlipper vf = (ViewFlipper) findViewById(R.id.flipper);
// Set an animation from res/anim: I pick push left in
vf.setOutAnimation(AnimationUtils.loadAnimation(view.getContext(), R.anim.push_left_out));
vf.setInAnimation(AnimationUtils.loadAnimation(view.getContext(), R.anim.slide_left));
vf.showNext();
}
private void flipPrevious(View view){
// Get the ViewFlipper from the layout
ViewFlipper vf = (ViewFlipper) findViewById(R.id.flipper);
// Set an animation from res/anim: I pick push left out
vf.setOutAnimation(AnimationUtils.loadAnimation(view.getContext(), R.anim.push_right_out));
vf.setInAnimation(AnimationUtils.loadAnimation(view.getContext(), R.anim.slide_right));
vf.showPrevious();
}
}
这里的 xml 很简单,因为有许多几乎相同的 ImageView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical" >
<ViewFlipper
android:id="@+id/flipper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/CockpitLayout"
android:layout_weight="3" >
<ImageView
android:id="@+id/foura"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/fourway"
android:src="@drawable/a" />
<ImageView
android:id="@+id/fourb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/fourway"
android:src="@drawable/b" />
<ImageView
android:id="@+id/fourc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/fourway"
android:src="@drawable/c" />
...//I want 35 more of these
</ViewFlipper>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#CCCCCC"
android:orientation="horizontal" >
<Button
android:id="@+id/Button_previous"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="prev" />
<Button
android:id="@+id/Button_next"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="next" />
</LinearLayout>
</LinearLayout>
最后是错误信息:
05-23 22:04:03.830: E/AndroidRuntime(16152): FATAL EXCEPTION: main
05-23 22:04:03.830: E/AndroidRuntime(16152): java.lang.RuntimeException: Unable to start activity ComponentInfo{dk.fourway.divepool/dk.fourway.divepool.notebook.FlipperFourActivity}: android.view.InflateException: Binary XML file line #173: Error inflating class <unknown>
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.app.ActivityThread.access$600(ActivityThread.java:130)
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.os.Looper.loop(Looper.java:137)
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.app.ActivityThread.main(ActivityThread.java:4745)
05-23 22:04:03.830: E/AndroidRuntime(16152): at java.lang.reflect.Method.invokeNative(Native Method)
05-23 22:04:03.830: E/AndroidRuntime(16152): at java.lang.reflect.Method.invoke(Method.java:511)
05-23 22:04:03.830: E/AndroidRuntime(16152): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-23 22:04:03.830: E/AndroidRuntime(16152): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-23 22:04:03.830: E/AndroidRuntime(16152): at dalvik.system.NativeStart.main(Native Method)
05-23 22:04:03.830: E/AndroidRuntime(16152): Caused by: android.view.InflateException: Binary XML file line #173: Error inflating class <unknown>
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.view.LayoutInflater.createView(LayoutInflater.java:613)
05-23 22:04:03.830: E/AndroidRuntime(16152): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-23 22:04:03.830: E/AndroidRuntime(16152): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.app.Activity.setContentView(Activity.java:1867)
05-23 22:04:03.830: E/AndroidRuntime(16152): at dk.fourway.divepool.notebook.FlipperFourActivity.onCreate(FlipperFourActivity.java:18)
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.app.Activity.performCreate(Activity.java:5008)
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
05-23 22:04:03.830: E/AndroidRuntime(16152): ... 11 more
05-23 22:04:03.830: E/AndroidRuntime(16152): Caused by: java.lang.reflect.InvocationTargetException
05-23 22:04:03.830: E/AndroidRuntime(16152): at java.lang.reflect.Constructor.constructNative(Native Method)
05-23 22:04:03.830: E/AndroidRuntime(16152): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.view.LayoutInflater.createView(LayoutInflater.java:587)
05-23 22:04:03.830: E/AndroidRuntime(16152): ... 25 more
05-23 22:04:03.830: E/AndroidRuntime(16152): Caused by: java.lang.OutOfMemoryError
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:500)
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:353)
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:781)
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.content.res.Resources.loadDrawable(Resources.java:1930)
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.widget.ImageView.<init>(ImageView.java:120)
05-23 22:04:03.830: E/AndroidRuntime(16152): at android.widget.ImageView.<init>(ImageView.java:110)
05-23 22:04:03.830: E/AndroidRuntime(16152): ... 28 more