Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageView is pixelated on older Android devices

The Problem

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:

Nexus 5

On a Note 2 (Android 4.4) however, the icon gets really pixelated and ugly:

Note 2

How do I get the ImageView (with a Vector as src) sharp on every device? Preferably with an only xml solution.

I already found:

  • Adding android:adjustViewBounds="true" -> did nothing
  • ImageView images seem pixelated and low quality -> Doesn't apply to vector
  • Setting BitmapFactory.inscaled=false -> doesn't work with only xml and I didn't know where to put this in a custom ImageView class.
like image 385
Jonathan Avatar asked Feb 08 '23 01:02

Jonathan


1 Answers

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">
like image 78
Jonathan Avatar answered Feb 11 '23 01:02

Jonathan