【问题标题】:Blurry Image on API 21+ : AppCompat v23.2.0 using VectorDrawables with srcCompatAPI 21+ 上的模糊图像:AppCompat v23.2.0 使用 VectorDrawables 和 srcCompat
【发布时间】:2016-03-15 16:12:56
【问题描述】:

我在 API 21+ 上遇到图像显示问题,但在较低的设备和 API 22+ 上一切正常。我使用的是 Gradle 插件 1.5,所以我的 build.gradle 看起来像这样:

// 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);

下面的屏幕截图将显示带有 VectorDrawable 的 ImageView 与 Pre-lollipop 和 Marshmallow 配合良好,但在 Android 5.0.1 中显示模糊图像

Android 4.4.4

Android 5.0.1

Android 6.0.1

【问题讨论】:

  • 你的java代码在哪里?
  • @pskink 我已经更新了 Java 代码,目前我在这个布局中使用 Java 代码。但在其他布局中,我在 XML 中使用矢量仍然在 API 21+ 中得到模糊的图像
  • 使用 23.2.1 会发生什么?
  • @ianhanniballake 还是一样的效果。我也试过 23.2.1。
  • 似乎你有一些生成的 png 挂在周围,检查一下

标签: android imageview android-support-library android-vectordrawable


【解决方案1】:

这与 ImageView 中的 scaleType 有关,当涉及到 VectorDrawables 时,它在这些不同的 API 级别执行不一致的操作。

有一个 scaleType 在缩放时似乎始终能提供清晰的图像:android:scaleType="fitXY" 但在使用它时,您必须确保 ImageView 与 Vector Drawable 具有相同的纵横比(例如,如果您使用 fitXY 和方形 VectorDrawable 和一个矩形 ImageView 它将拉伸图像)。

或者,您可以通过设置更改 VectorDrawable 本身的大小

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="225dp"
android:height="225dp"
.../>

通过这样做,ImageView 中将不需要缩放。

【讨论】:

  • 当 'width' 和 height 被修复时,它确实解决了我的问题。但是当“ImageView”作为动态“宽度”和height 时,即为match_parentwrap_content。您的解决方案不起作用。因为它会在 imageview 中创建混乱的图像。
猜你喜欢
  • 1970-01-01
  • 2016-06-08
  • 2016-08-12
  • 2016-03-28
  • 2016-08-27
  • 2015-10-23
  • 1970-01-01
  • 1970-01-01
  • 2020-03-08
相关资源
最近更新 更多