将图像添加到imageButton时,应用程序停止工作

时间:2015-04-10 16:32:07

标签: android image android-studio

我正在尝试使用ScrollView制作ImageButtons

如果我用空白按钮创建它,效果很好。

我的xml是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainScreen">


<HorizontalScrollView
    android:id="@+id/filtersScroll"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">

    <LinearLayout
        android:id="@+id/filtersLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/filtro1"
            android:layout_width="64dp"
            android:layout_height="64dp"/>
        <ImageButton
            android:id="@+id/filtro2"
            android:layout_width="64dp"
            android:layout_height="64dp" />
        <ImageButton
            android:id="@+id/filtro3"
            android:layout_width="64dp"
            android:layout_height="64dp"/>
        <ImageButton
            android:id="@+id/filtro4"
            android:layout_width="64dp"
            android:layout_height="64dp"/>
        <ImageButton
            android:id="@+id/filtro5"
            android:layout_width="64dp"
            android:layout_height="64dp"/>
        <ImageButton
            android:id="@+id/filtro6"
            android:layout_width="64dp"
            android:layout_height="64dp"/>
        <ImageButton
            android:id="@+id/filtro7"
            android:layout_width="64dp"
            android:layout_height="64dp"/>
        <ImageButton
            android:id="@+id/filtro8"
            android:layout_width="64dp"
            android:layout_height="64dp"/>




    </LinearLayout>

</HorizontalScrollView>

我得到的是:

Emulation picture

但是当我将图片添加到drawable文件夹时(不知道它是否重要但图片是2000x2000)并将其添加到ImageButtons之一,如下所示:

<ImageButton
            android:id="@+id/filtro1"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:scaleType="centerInside"
            android:src="@drawable/smiley" />

我不仅不能将图像大小调整为按钮的大小,而且一旦我尝试运行它,应用程序也会停止工作。

这是预览的样子:

buttonPreview

我错过了什么? 它与源图片的大小有关吗? 我的意思是,我只是想让它调整到按钮大小并工作。

此外,曾经在drawable/中的其他文件夹发生了什么?现在你不必根据它们的大小来分离图像吗?

1 个答案:

答案 0 :(得分:1)

  

是否与源图片的大小有关

是。您使用的是非常高分辨率的图像,肯定会在不同的设备上产生问题(特别是在低端设备上)。您应该使用按比例缩小的图像,或尝试按比例缩放代码。

  

现在您不必根据图像大小分隔图像

不,根据图像密度(不是尺寸)将图像放在不同的可绘制文件夹中

见以下链接:

Loading Large Bitmaps Efficiently

Displaying Bitmaps Efficiently

High resolution Image - OutOfMemoryError

相关问题