I have an ImageView in my XML like that:
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_person_add"
android:alpha="0.87" />
The ic_person_add
is the normal Material design Vectoricon.
On my Nexus 5 (Android 6), the icon scales beautiful and is really sharp:
On a Note 2 (Android 4.4) however, the icon gets really pixelated and ugly:
How do I get the ImageView (with a Vector as src) sharp on every device? Preferably with an only xml solution.
android:adjustViewBounds="true"
-> did nothingBitmapFactory.inscaled=false
-> doesn't work with only xml and I didn't know where to put this in a custom ImageView class.The real problem wasn't the DPI of the Note 2, it was its Android Version.
The Link Melllvar posted states:
Once you have a vectorDrawable image in your res/drawable, the Gradle plugin will automatically generate raster PNG images for API level 20 and below during build time
So my Android 6 device really had a Vectorgraphic as source for the ImageView so it managed to scale it up flawlessly. For the Note 2 (running on an older than Android 5 OS) Android Studio automatically converted the Vectorgraphic in my Drawables Folder to a PNG.
You can change the size of the converted PNG in the VectorXML by changing the
android:height android:width
to the size in dp your vectorgraphic needs to be displayed on pre API 20 devices. So in my Case (maximum of 100dp) I had to change the Values to:
<vector android:height="100dp" android:viewportHeight="24.0"
android:viewportWidth="24.0" android:width="100dp">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With