Android Bitmap Image Size In XML
Answer :
Only API 23 or later
Just use
<item android:width="200dp" android:height="200dp" android:drawable="@drawable/splash_background" android:gravity="center" />
instead
<item android:left="200dp" android:right="200dp"> <bitmap android:src="@drawable/splash_background" android:gravity="center" /> </item>
Although there is no width and height parameters for bitmap, you can set bitmap size using gravity and item width/height.
<item android:width="230dp" android:height="70dp"> <bitmap android:gravity="fill_horizontal|fill_vertical" android:src="@drawable/screen" /> </item>
You can set item width and height and scale bitmap inside of it using gravity.
I know this is old question but maybe someone will find it useful.
WARNING:
As already mentioned in comments, this method ONLY works with API level 23 (Android 6.0) and up.
If you want to reduce the width and height so it fits on the screen, and you don't need to set exact dimensions, you can simply set left
and right
on the parent item. That will essentially add a margin around the image so it takes up less space:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:left="20dp" android:right="20dp"> <bitmap android:src="@drawable/Icon" android:gravity="center" /> </item> </layer-list>
Ideally, though, you should upload different size images for each screen resolution (e.g. hdpi
, xxhdpi
) and then you won't need to programmatically set the size at all. You can see the full list here.
Comments
Post a Comment