Scale bitmap keeping aspect ratio in layer-list
Asked Answered
F

0

16

I'm trying to create a background drawable made of a gradient that fills the entire view and a pattern with left gravity that resizes to fit vertically while keeping its aspect ratio.

This is what I currently have:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/myGradient"/>
    <item>
        <bitmap android:src="@drawable/myPattern"
            android:gravity="left" />
    </item>
</layer-list>

Both drawables are VectorDrawables.

This is meant to be the background of a wrap_content height view.

This does not resize the pattern, but rather centers it vertically. If I set the gravity to fill_vertical, it only resizes in that axis and the aspect ratio breaks.

Is there any way to achieve this in XML without using ImageViews or other layouts? Can it be done programatically? Or do I require a different layout?

Fay answered 6/1, 2017 at 16:57 Comment(2)
were you able to solve this problem?Bunchy
No, I ended up using multiple drawables. It proved to be simpler, though less efficient.Fay

© 2022 - 2024 — McMap. All rights reserved.