[英]Android: How to change the size of a GradientDrawable within a layer-list programmatically
我在layers.xml文件中有一个层列表:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/outer_rect">
<shape android:shape="rectangle">
<stroke
android:width="3dp"
android:color="@color/gray4"/>
<solid android:color="@color/white"/>
</shape>
</item>
<item android:top="10dp" android:bottom="10dp" android:left="10dp" android:right="10dp"
android:drawable="@drawable/dot_pattern_bitmap"/>
</layer-list>
这在定制的FrameLayout customView.xml中使用:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/layers"/>
</merge>
它本身例如用于
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/space_m"
android:orientation="vertical">
<my.namespace.customView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
此外,我还有CustomView.java类来编辑customView(尤其是图层)的行为。
在这里,我试图根据customView的大小(和参数“比率”)实现图层列表大小的更新。 我这样尝试过:
// ...
@Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
layoutWidth = MeasureSpec.getSize(widthMeasureSpec);
layoutHeight = MeasureSpec.getSize(heightMeasureSpec);
readjustSize(ratio);
}
private void readjustSize(final double ratio) {
if (layoutHeight != 0 && layoutWidth != 0) {
// determine desired width and height using the layout width
int desiredWidth = layoutWidth;
int desiredHeight = (int) (layoutWidth / ratio);
// if it does not fit, adjust using the layout height
if (desiredHeight > layoutHeight) {
desiredHeight = layoutHeight;
desiredWidth = (int) (layoutHeight * ratio);
}
// get the basic layer
final LayerDrawable ld =
(LayerDrawable) ContextCompat.getDrawable(getContext(), R.layers);
final GradientDrawable outerRectShape =
(GradientDrawable) ld.findDrawableByLayerId(R.id.outer_rect);
// and adapt the size
outerRectShape.setSize(desiredWidth, desiredHeight);
outerRectShape.setBounds(0, 0, desiredWidth, desiredHeight);
// redraw
invalidate();
requestLayout();
}
}
因此,这是很多代码...不幸的是,这无法正常工作。 如果是第一次加载框架布局,则只能看到一个非常小的矩形。
仅当我来回切换到该视图时,才按照我的意愿应用大小:
有人有想法吗?
最后,我自己找到了答案。 :)实际上,这很简单。 当我掌握了可绘制的图层时
final LayerDrawable ld =
(LayerDrawable) ContextCompat.getDrawable(getContext(), R.layers);
我只是获得一般资源,而不是获取可绘制图像的具体源代码。 当我将代码修改为
final View root = inflate(getContext(), R.layout.custom_view, this);
final ImageView imageView = (ImageView) root.findViewById(R.id.layers);
final LayerDrawable ld = (LayerDrawable) imageView.getDrawable();
在CustomView初始化中,为ImageView提供id“ layers”,它按预期工作。
另外,我不得不在onMeasure中更改调用方法的顺序:
@Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
layoutWidth = MeasureSpec.getSize(widthMeasureSpec);
layoutHeight = MeasureSpec.getSize(heightMeasureSpec);
readjustSize(ratio);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
编辑:这仅适用于> = 6.0的Android版本。 对于较旧的版本,我仍然遇到问题中所述的相同问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.