[英]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.