2

I had generated a 96x96 SVG image via Android Studio.

enter image description here

Then, I apply SVG file in the following layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:gravity="center">

    <LinearLayout
        android:padding="8dp"
        android:id="@+id/linear_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:background="?selectableItemBackgroundBorderless"
        android:gravity="center"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/image_view"
            android:duplicateParentState="true"
            android:clickable="false"

            android:layout_width="96dp"
            android:layout_height="96dp"
            android:scaleType="fitXY"
            app:srcCompat="@drawable/ic_content_paste_black_24dp" />
        <TextView
            android:id="@+id/text_view"
            android:layout_marginTop="8dp"
            android:duplicateParentState="true"
            android:clickable="false"

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tap_to_add_note"
            android:textSize="24sp" />
    </LinearLayout>

</LinearLayout>

Take note that,

  1. I use app:srcCompat in ImageView
  2. I has added vectorDrawables.useSupportLibrary = true, as mentioned in https://stackoverflow.com/a/35795933/72437
  3. I use android:scaleType="fitXY" as mentioned in https://stackoverflow.com/a/35930518/72437

It looks both blurry in API 27 and API 16.

API 27

enter image description here

API 16

enter image description here

Note

You will notice the icon is in grey color instead of black color. This is because I had applied

DrawableCompat.setTint(this.imageView.getDrawable(), greyIconColor);

However, I'm not sure why API 16 doesn't appear as grey.

My build.gradle file looks like

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.yocto.noteplus"
        minSdkVersion 14
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }

dependencies {
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.android.support:gridlayout-v7:27.1.1'

SVG File

My SVG file content is

<vector android:height="96dp" android:viewportHeight="24.0"
    android:viewportWidth="24.0" android:width="96dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#FF000000" android:pathData="M19,2h-4.18C14.4,0.84 13.3,0 12,0c-1.3,0 -2.4,0.84 -2.82,2L5,2c-1.1,0 -2,0.9 -2,2v16c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2L21,4c0,-1.1 -0.9,-2 -2,-2zM12,2c0.55,0 1,0.45 1,1s-0.45,1 -1,1 -1,-0.45 -1,-1 0.45,-1 1,-1zM19,20L5,20L5,4h2v3h10L17,4h2v16z"/>
</vector>

Extra note

You may run the actual device to see the blurry, or use the Screen Capture in Logcat to see the blurry effect.

enter image description here

May I know, why my SVG image looks blurry in device API 16 and API 27?

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
  • See if this is of any help https://stackoverflow.com/questions/36016596/blurry-image-on-api-21-appcompat-v23-2-0-using-vectordrawables-with-srccompat, https://stackoverflow.com/questions/34936590/why-isnt-my-vector-drawable-scaling-as-expected, https://stackoverflow.com/questions/26548354/vectordrawable-is-it-available-somehow-for-pre-lollipop-versions-of-android – Tarun Lalwani May 09 '18 at 17:18
  • 1
    I tried your layout and vector drawable on an Android Studio emulator running API 16 (Pixel 6) and on another emulator running API 27 (Pixel 5X) and the images are sharp and clear. Can you 1) be more specific about where you are seeing the blurry images? Are these real devices or emulations? What kind of emulation or device?, and, 2) maybe put up a project that exhibits the blurriness? Is the image clear on APIs other then 16 and 27? Otherwise, it is going to be difficult to find the problem. – Cheticamp May 10 '18 at 01:47
  • @Cheticamp You need to run on actual device, or use the Screen Capture in Logcat to see the blurry effect. See my updated question. – Cheok Yan Cheng May 10 '18 at 02:51
  • I'd advise to change vector's `android:height`/`android:width` to be the same as `android:viewportHeight`/`android:viewportWidth`. In your case `android:height="24dp"` and `android:width="24dp"`. The image will still be `96dp` x `96dp` Because you apply that size to `ImageView` – R. Zagórski May 10 '18 at 08:32
  • Found an old LG phone running API 16. Unfortunately, the graphic looks clear and sharp on that device and in a screen capture, so it is not just being on API 16 that is the problem. – Cheticamp May 10 '18 at 12:28
  • I've tried to reproduce the issue. Unfortunately I couldn't achieve that. [Here's](https://i.stack.imgur.com/X78WE.png) a screenshot of Nexus S API 16. [Here's](https://i.stack.imgur.com/tU9Av.png) a screenshot of Nexus 5X API 27. – azizbekian May 10 '18 at 13:52
  • Trouble reproducing the problem. If you have a sample project that demonstrates this problem, it would be very helpful. Are you on the latest releases of Android Studio and Gradle? – Cheticamp May 10 '18 at 21:54
  • @CheokYanCheng are you running API 16 on hardware or an emulator ?? if it is hardware, maybe try to run it on the emulator once - in order to rule out any device-specific quirks. – Martin Zeitler May 10 '18 at 23:54

2 Answers2

3

I have encountered similar issues before. In my case, it was due to having both SVG & PNG files with same name, under drawable folder.

As you can see below, I am having ic_content_paste_black_24dp.png & ic_content_paste_black_24dp.xml (SVG).

enter image description here

So when we specify:

app:srcCompat="@drawable/ic_content_paste_black_24dp" />

System got confused & pick up the PNG file, as no file extension is specified.

Android should consider to allow specifying file extension to avoid such confusion.

Shuwn Yuan Tee
  • 5,578
  • 6
  • 28
  • 42
  • Exactly! How careless I was, to let such problem happens. Now recheck my project, and I realize I previously contains 5 different dpi PNG, before creating another SVG with same name. Thanks Shuwn! – Cheok Yan Cheng May 12 '18 at 16:29
0

For me, the problem was that I accidentally used android:src instead of android:srcCompat, which you don't want to use for SVGs.

Here you can find more information about the differences between the two: https://stackoverflow.com/a/40624625/10226383

nitnatsnoK
  • 57
  • 2
  • 4