4张图片时崩溃

时间:2014-02-27 13:31:45

标签: android imageview

当我有3个ImageViews但是当我添加第4个或第5个ImageView时,应用程序没有崩溃。所以我能做什么,甚至可以在这个Horizo​​ntalScrollView中拥有大约50张图片。我需要更改堆大小吗?

<?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:background="@drawable/black"
    android:orientation="vertical"
    >

<ImageView 
    android:layout_width="200dp"    
    android:layout_height="200dp"
    android:src="@drawable/wp2"
    android:layout_gravity="center"
    android:id="@+id/display"
    />

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="15dp"
    android:text="Als Hintergrundbild festlegen" />

<HorizontalScrollView android:layout_width="200dp" android:layout_height="wrap_content" android:layout_gravity="center">
    <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal">
<ImageView 
    android:layout_width="125dp"    
    android:layout_height="125dp"
    android:id="@+id/wp1"
    android:padding="15dp"
    />
<ImageView 
    android:layout_width="125dp"    
    android:layout_height="125dp"
    android:id="@+id/wp2"
    android:padding="15dp"
    />
<ImageView 
    android:layout_width="125dp"    
    android:layout_height="125dp"
    android:id="@+id/wp3"
    android:padding="15dp"
/>
<ImageView 
    android:layout_width="125dp"    
    android:layout_height="125dp"
    android:id="@+id/wp4"
    android:padding="15dp"
/>

</LinearLayout>
</HorizontalScrollView>
</LinearLayout>

3 个答案:

答案 0 :(得分:0)

您可能遇到了应用程序内存不足的问题。您可以在android:largeHeap="true"的应用标记中添加AndroidManifest.xml,但我不建议这样做。我建议在Android开发者网站上阅读this文章。它向您展示了如何有效地加载大位图,这将减少您的应用程序的内存使用量,并为用户提供更好的体验。

答案 1 :(得分:0)

如果要添加大量图像,最好使用ListView并创建ListViewAdapter来显示图像。这样做的好处是您不必手动添加所有图像视图,Android将重用ListViewAdapter中的视图来帮助管理内存。我不知道它们是否可以水平显示。确保显示图像的缩小版本 - 不要只是按原样加载它。例如,像:

public static Bitmap GetThumbnail(string fullPath, int width, int height)
    {
        Bitmap bMap = null;
        try
        {
            Bitmap src = BitmapFactory.DecodeFile(fullPath);

            bMap = Bitmap.CreateScaledBitmap(src, width, height, false);
        }
        catch (Exception ex)
        {
            Log.WriteLog("An error occurred producing a bitmap for file [" + fullPath + "]: " + ex.Message, ELogType.FILE);
        }
        return bMap;
    }

答案 2 :(得分:0)

请看一下这个Android教程 - Displaying Bitmaps Efficiently。位图的问题对于android来说很常见。