Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 4.4.4 Screenshot

Android 5.0.1

Android 5.0.1 Screenshot

Android 6.0.1

Android 6.0.1 Screenshot

like image 807
Vipul Asri Avatar asked Oct 18 '22 13:10

Vipul Asri


1 Answers

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.

like image 170
Lewis McGeary Avatar answered Oct 21 '22 05:10

Lewis McGeary