Android 图像缩放,如何去除黑色条纹

Android Image scaling, how to get rid of black stripes

我正在处理一些 Android 的东西,我遇到了图像缩放的奇怪问题。您可以在下图中看到的图像有:

<ImageView
            android:id="@+id/imagePreview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/editor_placeholder"
            android:contentDescription="@string/stfu" />

我一直在问自己为什么会出现那些黑色条纹。我可以让它显示没有那些条纹的图像吗?

解决方案:

android:adjustViewBounds="true"

去掉了不必要的条纹。

当然,您可以通过几种不同的方式来做到这一点。如果您希望图像适合并且不关心图像的裁剪,您可以执行以下操作:

   <ImageView
        android:id="@+id/imagePreview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:src="@drawable/editor_placeholder"
        android:contentDescription="@string/stfu" />

ImageView有多种不同的ImageView.ScaleType:

CENTER -> Center the image in the view, but perform no scaling. From XML, use this syntax: android:scaleType="center".

CENTER_CROP -> Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding). The image is then centered in the view. From XML, use this syntax: android:scaleType="centerCrop".

CENTER_INSIDE -> Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding). The image is then centered in the view. From XML, use this syntax: android:scaleType="centerInside".

FIT_CENTER -> Scale the image using CENTER. From XML, use this syntax: android:scaleType="fitCenter".

FIT_END -> Scale the image using END. From XML, use this syntax: android:scaleType="fitEnd".

FIT_START -> Scale the image using START. From XML, use this syntax: android:scaleType="fitStart".

FIT_XY -> Scale the image using FILL. From XML, use this syntax: android:scaleType="fitXY".

FIT_MATRIX -> Scale using the image matrix when drawing. The image matrix can be set using setImageMatrix(Matrix). From XML, use this syntax: android:scaleType="matrix".

Here 是来自开发者网站的一篇不错的文章,可帮助进行程序化扩展

在 xml 文件中的图像视图中使用 android 图像视图缩放类型 属性 ..

android:scaleType="fitXY".

试试这个

<ImageView
        android:id="@+id/imagePreview"
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/editor_placeholder"
        android:contentDescription="@string/stfu" />