如何在Android中动态合并两个Drawable?

时间:2017-07-17 20:04:51

标签: android android-layout drawable android-drawable layerdrawable

所以我有两个不同的Drawables我需要合并并在运行时获得一个Drawable。我希望第一个Drawable位于顶部,另一个位于底部。我遇到了LayerDrawable,看起来它正是我需要的,但我在尝试安排Drawables时遇到了麻烦。

所以我的ImageButton为48x48 dp,这是最终的Drawable。第一个Drawable是加号按钮(20x20 dp),第二个是加号按钮下方的小点(4x4 dp)。

使用字体字形加载加号按钮Drawable。我正在使用此xml片段创建点按钮Drawable

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
       android:shape="oval">
    <solid
        android:color="@color/white_40"/>
    <size
        android:width="4dp"
        android:height="4dp"/>
</shape>

我的第一种方法是将Drawables添加到LayerDrawable,但是当我这样做时,xml中指定的点的宽度/高度属性将被忽略,并且它会延伸到覆盖加号图标。

LayerDrawable finalDrawable = new LayerDrawable(new Drawable[] {plusIcon, dotIcon});

以上结果如下:



我尝试的第二种方法是使用setLayerInset尝试定位两个Drawables

    LayerDrawable finalDrawable = new LayerDrawable(new Drawable[] {plusIcon, dotIcon});
    finalDrawable.setLayerInset(0, 0, 0, 0, 0);
    finalDrawable.setLayerInset(1, dp(22), dp(44), dp(22), 0);

上面的代码片段最终将点放在正确的位置,但它也开始影响加号按钮的位置和大小,最终看起来像这样:
enter image description here

但我真正想要的是在ImageButton的中心加上加号按钮,在它下面加上加号图标。有没有人知道我哪里出错了?如何才能正确定位两个抽屉?

PS:我的应用程序支持API 15+,所以我不能使用LayerDrawable的API中的一堆方法,如setLayerGravity,`setPaddingMode等。

2 个答案:

答案 0 :(得分:7)

修改

此代码适用于低于23的API级别:

ImageButton button = (ImageButton) findViewById(R.id.button);

Drawable plusIcon = ContextCompat.getDrawable(this, R.drawable.plus);
Drawable dotIcon = ContextCompat.getDrawable(this, R.drawable.oval);

int horizontalInset = (plusIcon.getIntrinsicWidth() - dotIcon.getIntrinsicWidth()) / 2;

LayerDrawable finalDrawable = new LayerDrawable(new Drawable[] {plusIcon, dotIcon});
finalDrawable.setLayerInset(0, 0, 0, 0, dotIcon.getIntrinsicHeight());
finalDrawable.setLayerInset(1, horizontalInset, plusIcon.getIntrinsicHeight(), horizontalInset, 0);

button.setImageDrawable(finalDrawable);

原始

以下代码适用于我:

ImageButton button = (ImageButton) findViewById(R.id.button);

Drawable plusIcon = ContextCompat.getDrawable(this, R.drawable.plus);
Drawable dotIcon = ContextCompat.getDrawable(this, R.drawable.oval);

LayerDrawable finalDrawable = new LayerDrawable(new Drawable[] {plusIcon, dotIcon});
finalDrawable.setLayerInsetBottom(0, dotIcon.getIntrinsicHeight());
finalDrawable.setLayerGravity(1, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);

button.setImageDrawable(finalDrawable);

这会产生以下ui:

enter image description here

答案 1 :(得分:0)

这就是我解决这个问题的方法:

final View view = findViewById(R.id.view_in_layout);

Drawable bottom = ContextCompat.getDrawable(context, R.drawable.ic_bottom);
Drawable top = ContextCompat.getDrawable(context, R.drawable.ic_top);

//bottom = index 0, top = index 1
final LayerDrawable layer = new LayerDrawable(new Drawable[]{bottom, top});

view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
    @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom,
                  int oldLeft, int oldTop, int oldRight, int oldBottom) {
        //get the real height after it is calculated
        int height = view.getHeight();
        //set the height half of the available space for example purposes,
        //the drawables will have half of the available height
        int halfHeight = height / 2;
        //the bottom drawable need to start when the top drawable ends
        layer.setLayerInset(0, 0, halfHeight, 0, 0);
        //the top drawable need to end before the bottom drawable starts
        layer.setLayerInset(1, 0, 0, 0, halfHeight);

        view.setBackground(layer);
        view.removeOnLayoutChangeListener(this);
    }
});

您可以为绘图选择不同的尺寸或使用索引移动它们。例如,我使用View.OnLayoutChangeListener(...)等待视图的当前可用高度