将ImageView ScaleType设置为“center”无法正常工作

时间:2011-10-18 13:38:37

标签: android imageview scaling

我的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="wrap_content"
    android:orientation="horizontal"
    android:descendantFocusability="blocksDescendants">
    <ImageView
        android:id="@+id/imageview_icon"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:scaleType="fitCenter" />
    (...)
</LinearLayout>

这些48dp分别在ldpimdpihdpi中等于36px,48px和72px。将在ImageView中使用的所有图像基本上都是图标,我在网上找到了我想要的应用程序(免费许可证)。但不幸的是,它没有大于48px的图像,并且使用不同的图像是不可能的。所以我需要一个新的解决方案......

现在我分别在ldpimdpi文件夹中有36px和48px图像。但我遇到了hdpi的问题。对于在hdpi上运行的设备,我想要的是让他们使用mdpi图像(可用的较大图像)并按原样使用它,而不进行缩放。基本上,ImageView hdpi为72像素,所以我希望48像素图像位于中心的72像素内,不需要缩放。为此,我只是尝试将scaleType上方的ImageView更改为center,但无论如何图像都会缩放。

我在这里的最终问题是,解决上述问题的正确方法是什么?如何在各自的密度中使用ldpimdpi图片,但设备运行hdpi会获取最大的可用图像(mdpi个)防止任何缩放,只需将图像拟合到ImageView

的中心

修改
我自己回答了这个问题,但也许是其他人来这里寻找的答案。但我确实提供了一些关于真实情况和潜在问题的见解。如果可以,请查看并提供解决方法/修复方法。我敢说你:)。

7 个答案:

答案 0 :(得分:13)

这个问题结果证明是一个非问题,我向任何创建它的人道歉,希望接受的答案将是他们在寻找答案的时候。

为清楚起见,scaleType=centerInside按预期工作。也就是说,如果您的图像小于ImageView本身,那么该图像将不会缩放到ImageView的边界,它将保留在中心并且未缩放。

但是为了使上述工作符合预期,必须将drawable放在nodpi文件夹中。我知道这并不总是可以接受的。因此,当必须将该drawable放入其中一个密度文件夹而不是nodpi文件夹时,scaleType属性仅适用于特定情况。

什么时候可行:

  • 您正在具有Xdpi密度的设备/模拟器上运行该应用 Xdpi密度文件夹中有一个drawable(这里X表示, lmh甚至xh)。
  • 您正在运行应用设备/模拟器, 例如,密度为hdpi,但是没有可绘制的 hdpi文件夹,系统从中选择替代drawable nodpi文件夹(并不总是知道它会选择哪个文件夹 从)。

什么时候不起作用

  • 您正在使用hdpi运行应用设备/模拟器 密度,但hdpi文件夹中没有可绘制的 系统从任何其他密度中挑选替代抽屉 文件夹(不是nodpi文件夹),drawable将缩放到 ImageView边界scaleTypeImageView属性不起作用 任何东西。

总之,我的问题没有“正确”的答案,这实际上取决于你想要实现的目标。答案我的问题,我只需要做两件事:a)将scaleType centerInside设置为mdpi,然后b)复制hdpi中的所有drawable {1}}文件夹中的{1}}文件夹(如上所述,scaleType=centerInside将使其正常工作)。

当然,复制drawables并不是最佳选择,但我找不到任何其他解决方案,到目前为止,没有其他人可以......所以,在目前,我会将此标记为已接受。< / p>

那么最佳答案/解决方案是什么?
在我看来,如果设备/模拟器在hdpi中运行并且hdpi文件夹中没有匹配的drawable,则应该从mdpi文件夹中选择drawable而不进行缩放,允许scaleType属性做到这一点。或者,如果在相应的密度文件夹中找不到匹配的drawable,可能强制系统转到nodpi文件夹,这也可能是一个解决方案。

因此,如果任何人都可以为此问题提供解决方法/修复方法,那将是真正正确的答案。如果它发生了,我将改变接受的状态。

答案 1 :(得分:1)

我用这种方式解决了它:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <!-- Here is where the money is -->
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_horizontal|center_vertical">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        ...
    </LinearLayout>
</LinearLayout>

答案 2 :(得分:1)

在我的情况下,我将图像设置为android:background。当我将其设置为android:src时,它有效!

答案 3 :(得分:0)

尝试将其从hdpi文件夹中删除,并将其放在drawable-nodpi文件夹中。我不确定层次结构是什么(nodpi是否会覆盖ldpi和mdpi文件夹)但这将是我的第一次尝试。

答案 4 :(得分:0)

试试这个:

<FrameLayout android:layout_width="48dp" android:layout_height="48dp">
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"

    android:maxWidth="48px"
    android:maxHeight="48px"
    android:adjustViewBounds="true"
    android:src="..."
    />
</FrameLayout>

将图像正常放入ldpi / mdpi文件夹中。

虽然我不建议使用像素值。

答案 5 :(得分:0)

尝试

imageview.setAdjustViewBounds(false);

答案 6 :(得分:-1)

创建自定义ImageView:

package x;

public class MaxMDPIImageView extends ImageView {

    public MaxMDPIImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        float density = getResources().getDisplayMetrics().density;
        if (density>1) {
            canvas.translate(getWidth()/2-getWidth()/density/2, getHeight()/2-getHeight()/density/2);
            canvas.scale(1/density, 1/density);
        }
        super.onDraw(canvas);
    }

}

简单地用作:

<x.MaxMDPIImageView
    android:src="@drawable/icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
    />