Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vector Drawables sometimes become blurry

I am working on an existing app that needed some update and as soon as I replaced some png icons with vector drawables something very odd happens:

these vector drawables sometimes become blurry. I say sometimes because they are not always blurred.

For instance I have a recycler view with several cards, every card contains some icons and other elements. Everything looks good. But then if a insterstitial ad is show, when I close it, those icons become blurred. If i scroll down the recycler view, and then back up again, icons are cleary redrawn and now they look good and sharp.

Another example: I have a toolbar with an icon menu on the right (the classic lens) and a back arrow on the left (both are vector drawable). If the user tap on the icon then the toolbar turn into a searchview and the keyboard appears...but the back arrow is now blurred. As soon as I digit the first letter in the searchview, the icon return good and sharp.

These are only two examples, but happens in many other parts of the app.

In the gradle I have

android {
compileSdkVersion 27
defaultConfig {
    applicationId '...'
    minSdkVersion 21
    targetSdkVersion 27
    versionCode 12310106
    versionName '4.1.1'

    renderscriptTargetApi 21
    renderscriptSupportModeEnabled true
    vectorDrawables.useSupportLibrary = true

I'm getting crazy, any idea?

Update

It turns out that if I call invalidateSelf() on Drawables, then the problem is gone, and they look good again. I don't want to invalidate every Drawable and I don't think this is a solution

like image 939
Pizzetto Avatar asked May 07 '18 09:05

Pizzetto


1 Answers

After finding out, I found the cause. Because you use the same vector image on multiple dimensions. For example, if you have 2 images 100 dp x 100 dp and 50 dp x 50dp, you must create 2 vector images with separate names, You must not use 1 vector image on many sizes, even if you switch to another activity. For example Activity A has a 50 dp x 50 dp image, you open activity B with a 100dp x100 dp image using the same image vector and you return to the Activity A image will be deformed. The reason is that the Android operating system optimizes performance, on the image stored on the cache, especially weak configuration devices. (In summary, if you use a vector image on multiple dimensions, you have to name it differently.)

like image 148
Van Tung Avatar answered Sep 22 '22 06:09

Van Tung