如何在Android中动态合并两个drawable?
因此,我有两个不同的如何在Android中动态合并两个drawable?,android,android-layout,drawable,android-drawable,layerdrawable,Android,Android Layout,Drawable,Android Drawable,Layerdrawable,因此,我有两个不同的Drawable,需要在运行时合并并获得一个Drawable。我希望第一个可绘制的位于顶部,另一个位于底部。我遇到了LayerDrawable,它看起来正是我所需要的,但是我在尝试安排Drawable时遇到了问题 因此,我有一个ImageButton,它是48x48dp,这就是最终的可绘制的的位置。第一个可绘制是一个加号按钮(20x20dp),第二个是加号按钮下方的一个小点(4x4dp) 加号按钮可绘制使用字体图示符加载。我正在使用以下xml片段创建点按钮Drawable:
Drawable
,需要在运行时合并并获得一个Drawable
。我希望第一个可绘制的位于顶部,另一个位于底部。我遇到了LayerDrawable
,它看起来正是我所需要的,但是我在尝试安排Drawable
时遇到了问题
因此,我有一个ImageButton
,它是48x48dp
,这就是最终的可绘制的
的位置。第一个可绘制
是一个加号按钮(20x20dp
),第二个是加号按钮下方的一个小点(4x4dp
)
加号按钮可绘制
使用字体图示符加载。我正在使用以下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>
上述结果如下:
我尝试的第二种方法是使用setLayerInset
尝试并定位两个可绘图项
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);
上面的代码片段最终将圆点放置在正确的位置,但它也开始影响加号按钮的位置和大小,结果如下:
但我真正想要的是在ImageButton
的中央有一个加号按钮,加号图标就在它下面。有人知道我哪里出了问题吗?我怎样才能正确地定位这两个抽屉
PS:我的应用程序支持API 15+,所以我不能使用LayerDrawable
的API中的一系列方法,比如setLayerGravity
,`setPaddingMode,等等。Edit
此代码适用于低于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:
我就是这样解决的:
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(…)
来等待视图的当前可用高度嘿,Ben,谢谢你的回答。但是setLayerGravity
仅在API 23之后的iirc中提供。我的应用程序支持API 15及以上。嘿,非常感谢!经过编辑的答案使它起作用。不过,这只是一个小问题,我发现如果我更改底部圆点的大小,加号图标必须向上移动,以便为圆点图标腾出空间,因为它在ImageButton
中的相对位置取决于圆点的大小。有没有什么方法可以将加号图标精确定位在中心(Gravity.center)和点图标,比如说它下面的4dp
?我想现在我们已经超出了LayerDrawable真正要做的范围。我可能会考虑创建一个包含DoP和Plus的矢量绘制,或者使用FrAMLayayOut/FufftDead(在你的按钮上面)定位这两个可绘图项。是的,问题是我不能使用布局来定位可绘图项,因为我只能返回一个应用于ImageButton
的Drawable
。因此,这正是我试图使用LayerDrawable
实现这一点的原因。有什么想法吗?看起来LayerDrawable的每一层都将被放大以适应LayerDrawable的大小,这是它的任何层中最大的大小。此“层大小”包括插图。因此,如果定义一些大小填充,可以在所有边上插入加号by(填充+点直径),在顶部插入加号by(点直径+填充+加高度+填充),在每边插入((2*直径+2*填充+加宽度)-直径)/2。
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);
}
});