我试图在这张照片中定位我的视图(只是点图案)但没有成功。 有人可以帮我弄清楚我应该如何使用onLayout做到这一点? 感谢。
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
final int count = getChildCount();
int curWidth, curHeight, curLeft, curTop, maxHeight;
//get the available size of child view
int childLeft = this.getPaddingLeft();
int childTop = this.getPaddingTop();
int childRight = (int) getResources().getDimension(R.dimen.circle_width) - this.getPaddingRight();
int childBottom = (int) getResources().getDimension(R.dimen.circle_width) - this.getPaddingBottom();
int childWidth = childRight - childLeft;
int childHeight = childBottom - childTop;
maxHeight = 0;
curLeft = childLeft;
curTop = childTop;
//walk through each child, and arrange it from left to right
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
if (child.getVisibility() != GONE) {
//Get the maximum size of the child
child.measure(MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.AT_MOST),
MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.AT_MOST));
curWidth = child.getMeasuredWidth();
curHeight = child.getMeasuredHeight();
//wrap is reach to the end
if (curLeft + curWidth >= childRight) {
curLeft = childLeft;
curTop += maxHeight;
maxHeight = 0;
}
//do the layout
child.layout(curLeft, curTop, curLeft + curWidth, curTop + curHeight);
//store the max height
if (maxHeight < curHeight)
maxHeight = curHeight;
curLeft += curWidth;
}
}
}
答案 0 :(得分:0)
首先你不应该在onLayout
里面这样做,它被多次调用,你可能不需要这个。为什么不在constructor
内准备,或者只是在onDraw
内绘制点,将比仅使用简单位图添加大量View
更有效。可能会显示一些屏幕你有什么,应该有任何动画吗?
答案 1 :(得分:0)
实际上,您可以轻松地在XML布局中构建它。在它最简单的表示中,它只是三个点视图,彼此垂直移位。定义具有垂直方向的线性布局,权重和= 3,布局三视图,权重= 1.然后使用布局边距相对于彼此移动每个视图。这种场景的完美布局是Contraint Layout,它更灵活。
这是一个很好的教程,可以整合自定义手势处理http://fieldguide.gizmodo.com/how-to-add-customized-gesture-controls-to-your-android-1705180921