Reputation: 81
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
Upvotes: 7
Views: 2097
Reputation: 477
This may seem late but I actually found out a very simple solution to this problem ,just declare a regular Drawable xml which I found won't surpass 4kbs not another vector asset and get your vector asset there as a drawable item and use that as another xml for drawing the same image more than once without blur like so :-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/yourvectorxml"
/>
</selector>
if its in the same activity declare more of these for each draw .
For me the blur was only occurring when the device locks or when opening the softkeyboard other than that it can be fine to use the same XML .
Upvotes: 0
Reputation: 49
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.)
Upvotes: 3
Reputation: 207
It can be happened when you give a fix size of a imageview. But when you use wrap content for width and height, it might be small, and may be you need larger than that. So if you need larger image without bluring, you can customize vectorDrawable resource very easily. When you collect your asset from android studio, there is a option to override the size.
Upvotes: 0