Move bitmap items in layer-list programaticaly

时间:2018-08-22 13:43:57

标签: java android layer-list

I would need move-change position of individual bitmap items in layer-list such as this: background.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape>
        <gradient
            android:centerX="0.5"
            android:centerY="0.3"
            android:endColor="@color/colorBackgroundEnd"
            android:gradientRadius="300dp"
            android:startColor="@color/colorBackgroundStart"
            android:type="radial" />

    </shape>
</item>

<item
    android:id="@+id/peas"
    android:left="35dp"
    android:top="110dp"
    >
    <bitmap
        android:gravity="left|start|top"
        android:src="@drawable/peas"
        />
</item>

<item
    android:id="@+id/orange"
    android:right="-35dp"
    android:bottom="110dp">
    <bitmap
        android:gravity="right|end|bottom"
        android:src="@drawable/orange" />
</item>

<item
    android:id="@+id/salad"
    android:bottom="-40dp"
    android:left="-40dp">
    <bitmap
        android:gravity="left|start|bottom"
        android:src="@drawable/salad"/>
</item>

I was able to reference items in java code:

layerDrawable = (LayerDrawable) ContextCompat.getDrawable(MainActivity.this,R.drawable.background);
Drawable salad = (Drawable) layerDrawable.findDrawableByLayerId(R.id.salad);

and get-set some layer properties, such as getAlpha and setAlpha

layerDrawable.findDrawableByLayerId(R.id.salad).setAlpha(0);
    down.setText( String.valueOf(layerDrawable.findDrawableByLayerId(R.id.salad).getAlpha()));

, but I need code access to left and top property that would enable setting layer bitmap position, like it can be done from XML file. Does anybody know/have workaround on how to do this? Thanks.

0 个答案:

没有答案