【问题标题】:App stops working when adding image to imageButton将图像添加到 imageButton 时应用程序停止工作
【发布时间】:2015-04-10 16:32:07
【问题描述】:

我正在尝试用一些ImageButtons 制作ScrollView

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

我的 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>

我得到的是:

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

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

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

这是预览的样子:

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

另外,以前位于drawable/ 中的其他文件夹发生了什么变化?现在您不必根据图像的大小来分隔图像了吗?

【问题讨论】:

  • 如果崩溃,您需要发布堆栈跟踪,以便我们查看原因/位置

标签: android image android-studio


【解决方案1】:

和源图片的大小有关系吗

是的。你正在使用一个非常高分辨率的图像,它肯定会在不同的设备上产生问题(尤其是在低端设备上)。您应该使用按比例缩小的图像,或尝试通过代码进行缩放。

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

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

查看以下链接:

Loading Large Bitmaps Efficiently

Displaying Bitmaps Efficiently

High resolution Image - OutOfMemoryError

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-08
    • 2017-05-16
    • 1970-01-01
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多