我制作了半圆自定义布局。我想把我的视图放到半圆的外表面。但它并没有完全显示我想要的。
我在下面的代码中覆盖了视图组的OnLayout方法 我在下面发布了我的代码: -
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int mAngleRange = 180;
final int childs = getChildCount();
float totalWeight = 0f;
for (int i = 0; i < childs; i++) {
totalWeight += 1;
}
float startAngle = 270;
final int width = getWidth();
final int height = getHeight();
for (int i = 0; i < childs; i++) {
final View child = getChildAt(i);
final LayoutParams lp = layoutParams(child);
final float angle = mAngleRange / totalWeight * 1;
final float centerAngle = startAngle + angle / 2f;
final int x;
final int y;
if (childs > 1) {
int radius = getResources().getDimensionPixelOffset(R.dimen.item_radius);
x = (int) (radius * Math.cos(Math.toRadians(centerAngle)) + 0);
y = (int) (radius * Math.sin(Math.toRadians(centerAngle))) + height / 2;
} else {
x = width / 2;
y = height / 2;
}
final int left = lp.width != LayoutParams.MATCH_PARENT ? x : 0;
final int top = lp.height != LayoutParams.MATCH_PARENT ? y : 0;
final int right = lp.width != LayoutParams.MATCH_PARENT ? x + child.getMeasuredWidth() : width;
final int bottom = lp.height != LayoutParams.MATCH_PARENT ? y + child.getMeasuredHeight() : height;
Log.d(CircularOverlayLayout.class.getName(), "left =" + left + " top =" + top + " right =" + right + " bottom = " + bottom);
child.layout(left, top, right, bottom);
startAngle += angle;
}
}
<com.trignodev.customuidemo.CircularOverlayLayout
android:id="@+id/normalWithRange"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<View
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#ff0000" />
<View
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#00ff00" />
<View
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#0000ff" />
</com.trignodev.customuidemo.CircularOverlayLayout>