I've got the following drawable resource:
<layer-list>
<item><shape>
<gradient android:angle="225" android:endColor="#080517" android:startColor="#241c4d" />
<stroke android:width="2.5dp" android:color="#FFFFFF" />
<corners android:radius="5dp" />
</shape></item>
<item>
<bitmap android:gravity="right|center_vertical" android:src="@drawable/tri_arrow_down_blue64" />
</item>
</layer-list>
the problem is: I can not specify the exact size of the bitmap (in dp). as a result - the bitmap affects the height of a control with background set to that drawable.
I can workaround the problem by putting the control inside the FrameLayout, an extracting the bitmap portion of the drawable into an ImageView placed into the same FrameLayout. This solves the problem because ImageView can define the exact width and height of the bitmap (through layout_width and layout_height). But drawable does not have such (or similar) attributes. I wonder why this is so? and is there a more elegant way of specifying the size of a bitmap drawable right inside the drawable XML - instead of implementing that ugly workaround with FrameLayout?
thanks
This example demonstrates how do I convert Drawable to a Bitmap in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
scaleToFitWidth(bitmap, 100); public static Bitmap scaleToFitWidth(Bitmap b, int width) { float factor = width / (float) b. getWidth(); return Bitmap. createScaledBitmap(b, width, (int) (b. getHeight() * factor), true); } // Scale and maintain aspect ratio given a desired height // BitmapScaler.
Scaling the entire TextView smaller will decrease the size of both the text and the drawable, however the text size can then be increased to counteract this change. For example, a text size of 16 is equal to a text size of 20, when scaled to 0.8 (80%). This is because 20 = 16 / 0.8 .
I had a similar issue, and from my research there isn't a way to actually set the size of the item or bitmap in a layer list (which seems ridiculous).
I solved my problem simply by shrinking the size of the png image I was using for my bitmap drawable.
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