API 21+上的模糊图像:使用带有srcCompat的VectorDrawables的AppCompat v23.2.0 [英] Blurry Image on API 21+ : AppCompat v23.2.0 using VectorDrawables with srcCompat
问题描述
我在API 21+上遇到图像显示问题,但在较低的设备和API 22 +上一切正常。我正在使用Gradle Plugin 1.5,所以我的版本.gradle
看起来像这样:
I have an image display issue on API 21+, but everything works fine on lower devices and API 22+. I'm using Gradle Plugin 1.5, so my build.gradle
look like this:
// Gradle Plugin 1.5
android {
defaultConfig {
generatedDensities = []
}
// This is handled for you by the 2.0+ Gradle Plugin
aaptOptions {
additionalParameters "--no-version-vectors"
}
}
XML格式的图像视图:
<ImageView
android:id="@+id/landing_img_slide"
android:layout_width="225dp"
android:layout_height="225dp"
android:layout_centerHorizontal="true"
android:scaleType="centerCrop" />
Java代码:
ImageView iconView = (ImageView) itemView.findViewById(R.id.landing_img_slide);
iconView.setImageResource(R.drawable.laptopscreen);
屏幕截图下方将显示 ImageView
使用VectorDrawable可以正常使用Pre-lollipop和Marshmallow,但在Android 5.0.1中显示模糊图像
Below Screenshots will shows ImageView
with VectorDrawable works fine with Pre-lollipop and Marshmallow but shows Blurry Image in Android 5.0.1
Android 4.4.4
Android 5.0.1
Android 6.0.1
推荐答案
这与ImageView中的scaleType有关,当涉及到VectorDrawables时,它会在这些不同的API级别上做出不一致的事情。
This is to do with the scaleType in your ImageView which does inconsistent things at these different API levels when it comes to VectorDrawables.
有一个scaleType在缩放时似乎始终给出清晰的图像: android:scaleType =fitXY
但是使用它时你必须确保ImageView具有相同的纵横比为Vector Drawable(例如。如果你使用带有方形VectorDrawable和矩形ImageView的fitXY,它会将图像拉出来。)
There is one scaleType which seems to consistently give a sharp image when scaling: android:scaleType="fitXY"
but when using this you have to ensure that the ImageView has the same aspect ratio as the Vector Drawable (eg. if you use fitXY with a square VectorDrawable and a rectangular ImageView it will stretch the image out).
或者你可以通过设置
Alternatively you can change the size in the VectorDrawable itself by setting
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="225dp"
android:height="225dp"
.../>
通过这样做,ImageView中不需要缩放。
By doing this there will be no scaling necessary in the ImageView.
这篇关于API 21+上的模糊图像:使用带有srcCompat的VectorDrawables的AppCompat v23.2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!