【问题标题】:Android image added programatically from file not sizing properly从文件中以编程方式添加的 Android 图像未正确调整大小
【发布时间】:2013-12-31 19:48:57
【问题描述】:

我有一个 Android 应用程序,它在 ExpandableListView 中显示数据集合。可扩展列表视图只有一个子视图。在那个孩子中,我添加了几个 TextView 元素,最后一个之后是 ImageView。

那个 ImageView 是问题。

如果我使用

从可绘制文件夹中设置图像资源
R.drawable.test_image
显示如下:

但如果我以编程方式添加图像,如下所示:

ImageView drinkImage = (ImageView) inflater.inflate(R.layout.drink_image, null, false);
// FileString is "/storage/sdcard0/.WhatToDrink/drinkImages/1388450237870.jpg" 
Drawable drinkImageDrawable = Drawable.createFromPath(fileString);

drinkImage.setImageDrawable(drinkImageDrawable);

显示如下:

相同的文件。我从存储中抓取文件本身并将其移动到我的可绘制文件夹中,并且以这种方式添加它非常有效。

ImageView xml 代码:

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentDescription="Drink Image"
    android:id="@+id/drink_image"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

ExpandableListView XML 代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bluelimecrew.whattodrink.DrinkListActivity.DrinkListFragment">
    <LinearLayout
            android:id="@+id/listContainer"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:orientation="vertical">
        <ExpandableListView
                android:id="@+id/drinkList"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
        </ExpandableListView>
    </LinearLayout>

    <Button
            android:id="@+id/backToDashboard"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            style="@style/nextButtons"
            android:text="@string/backToDashboard" />
</LinearLayout>

有什么想法吗?

我在运行 Android 4.1.1 的 Galaxy Nexus 上运行它

【问题讨论】:

    标签: android android-layout android-image android-resources android-drawable


    【解决方案1】:

    Drawable.createFromPath 将没有上下文信息,因此无法正确缩放位图(因为它不知道屏幕密度等)。

    正如您在源代码中看到的,它将调用drawableFromBitmap(null, bm, null, null, pathName)。第一个参数(null)是资源引用。

    改为使用接收Context 或Resources 的构造函数,以便正确缩放位图。

    查看new BitmapDrawable(Resources res, String path)

    可以看到没有接收到资源的BitmapDrawable构造函数是deprecated。

    【讨论】:

    • 你的意思是使用BitmapDrawable吗?不同的构造函数?我很困惑。
    • 既然你知道它是一个位图,是的,我建议直接使用BitmapDrawable,这样你就可以传递资源。如果你查看 Drawable 类的源代码,你会看到 Drawable.createFromPath 加载了 Bitmap 并调用了 drawableFromBitmap,这也创建了一个 BitmapDrawable。
    【解决方案2】:

    不确定,因为图像可能很棘手,但试试这个

    <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
        android:contentDescription="Drink Image"
        android:id="@+id/drink_image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
    

    【讨论】:

    • 我已经尝试了所有不同的 layout_width 和 layout_height 选项。
    • 这就是整个 XML 布局。我将其膨胀并将其添加到 ExpandableListView。编辑了我的问题,使其具有 ExpandableListView。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多