Blurry Image on API 21+ : AppCompat v23.2.0 using VectorDrawables with srcCompat

耗尽温柔 提交于 2019-12-21 19:40:07

问题


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"  
  }  
 }  

Image View in XML:

<ImageView
   android:id="@+id/landing_img_slide"
   android:layout_width="225dp"
   android:layout_height="225dp"
   android:layout_centerHorizontal="true"
   android:scaleType="centerCrop" />

Java Code :

ImageView iconView = (ImageView) itemView.findViewById(R.id.landing_img_slide);
iconView.setImageResource(R.drawable.laptopscreen);

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


回答1:


This is to do with the scaleType in your ImageView which does inconsistent things at these different API levels when it comes to VectorDrawables.

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"
.../>

By doing this there will be no scaling necessary in the ImageView.



来源:https://stackoverflow.com/questions/36016596/blurry-image-on-api-21-appcompat-v23-2-0-using-vectordrawables-with-srccompat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!