Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scale and centre a drawable inside layer-list android?

I'm trying to center a drawable image with some padding on either side to use as a splash screen logo. The problem is it's either stretched across the entire screen or ignores any padding if I use gravity.

<?xml version="1.0" encoding="utf-8"?>

<item
    android:drawable="@color/grey_3"/>

<item android:gravity="top|center_horizontal|center_vertical" android:drawable="@drawable/zc_logo_r"
      android:top="100dp"
      android:left="200dp"
      android:right="200dp"
      android:layout_margin="100dp"
    >
</item>

I've tried using a bitmap, android:gravity="fill_horizontal" and various other suggestions on SO with the same result.

How can I scale and center the image in my xml?

like image 343
Kai Avatar asked Oct 27 '16 00:10

Kai


People also ask

What is Layer list in android?

Layer list. A LayerDrawable is a drawable object that manages an array of other drawables. Each drawable in the list is drawn in the order of the list—the last drawable in the list is drawn on top. Each drawable is represented by an <item> element inside a single <layer-list> element.

How do you reference drawable android?

In your Android/Java source code you can also refer to that same image like this: Resources res = getResources(); Drawable drawable = res. getDrawable(R.

Which function is used to load a drawable image resource?

Drawable myImage = ResourcesCompat. getDrawable(res, R. drawable. my_image, null);

What is stroke in android XML?

Sometimes you want an outline around your shape and to do that you can use the stroke tag. You can specify the width and color of the outline using android:width and android:color.


1 Answers

As a reference, if you come across the same issue, I solved it with a square image in drawable folders (i.e. hdpi, mdpi, xhpi, xxhpi and xxxhdpi) instead of resizing a rectangular image which was too big in size and gets distorted.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/grey_3"/>
    <item android:gravity="center">
          <bitmap android:src="@drawable/splash_logo"/>
    </item>
</layer-list>
like image 128
Kai Avatar answered Oct 02 '22 23:10

Kai