滚动自定义视图-Android
下面是我需要制作的菜单的图像 我在自定义视图中使用画布绘制了三角形。我还需要滚动这个菜单,当在滚动视图中添加自定义视图时,它将每个视图作为一个矩形项,不允许像这样绘制三角形 有没有办法做到这一点滚动自定义视图-Android,android,scrollview,Android,Scrollview,下面是我需要制作的菜单的图像 我在自定义视图中使用画布绘制了三角形。我还需要滚动这个菜单,当在滚动视图中添加自定义视图时,它将每个视图作为一个矩形项,不允许像这样绘制三角形 有没有办法做到这一点 提前感谢。首先,您必须创建两个自定义视图,使用不同的三角形绘制。然后在您的RelativeLayout或另一个窗口中创建这些三角形,并设置负值margin\u top值以正确显示,如我的示例中所示: 现在让我们看一下代码 不要批评我的代码。这只是一个没有真正OOP结构的示例:) CustomSha
提前感谢。首先,您必须创建两个自定义视图,使用不同的三角形绘制。然后在您的
RelativeLayout
或另一个窗口中创建这些三角形,并设置负值margin\u top
值以正确显示,如我的示例中所示:
现在让我们看一下代码
不要批评我的代码。这只是一个没有真正OOP结构的示例:)
- CustomShapeLeft.class: }
isTouched()
需要知道用户何时点击形状,而不是在视图上,
导致在最后一次点击时,采取正确的操作是有问题的,因为它们像分层系统一样放置
该方法根据丝锥的形状确定丝锥的位置并进行计算。以您的方式,您将不会有onClickListener
,而只会有ontouchtlistener
- CustomShaperRight.class:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setColor(backgroundColor);
paint.setStyle(Paint.Style.FILL);
points[0] = new Point(viewWidth, 0);
points[1] = new Point(0, (viewHeight / 2));
points[2] = new Point(viewWidth, (viewHeight));
path.setFillType(Path.FillType.EVEN_ODD);
path.moveTo(points[0].x, points[0].y);
path.lineTo(points[1].x, points[1].y);
path.lineTo(points[2].x, points[2].y);
path.close();
canvas.drawPath(path, paint);
}
- Layout.xml:
等等。我希望这将有助于你,在你的版面中删除“X”项,你将独自处理。谢谢你的提问。喜欢 主要问题是如何滚动这些自定义形状。我在滚动视图中使用了这些自定义视图。为此,我必须重写onMeasure()方法,该方法只需要固定的高度和宽度。@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); paint.setColor(backgroundColor); paint.setStyle(Paint.Style.FILL); points[0] = new Point(viewWidth, 0); points[1] = new Point(0, (viewHeight / 2)); points[2] = new Point(viewWidth, (viewHeight)); path.setFillType(Path.FillType.EVEN_ODD); path.moveTo(points[0].x, points[0].y); path.lineTo(points[1].x, points[1].y); path.lineTo(points[2].x, points[2].y); path.close(); canvas.drawPath(path, paint); }
<null_anal.customshape.CustomShapeLeft android:layout_width="match_parent" android:layout_height="70dp" android:id="@+id/triangle_1" android:background="@android:color/holo_red_dark"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:textSize="22dp" android:padding="5dp" android:textColor="@android:color/black" android:text="MEN" android:background="@android:color/transparent"/> </null_anal.customshape.CustomShapeLeft> <null_anal.customshape.CustomShapeRight android:layout_width="match_parent" android:layout_height="70dp" android:id="@+id/triangle_2" android:layout_marginTop="-36dp" android:background="@android:color/holo_orange_dark" android:layout_below="@+id/triangle_1"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:textSize="22dp" android:layout_alignParentRight="true" android:padding="5dp" android:textColor="@android:color/black" android:text="HOME" android:background="@android:color/transparent"/> </null_anal.customshape.CustomShapeRight> <null_anal.customshape.CustomShapeLeft android:layout_width="match_parent" android:layout_height="70dp" android:id="@+id/triangle_3" android:layout_marginTop="-36dp" android:background="@android:color/holo_purple" android:layout_below="@+id/triangle_2"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:textSize="22dp" android:padding="5dp" android:textColor="@android:color/black" android:text="WOMEN" android:background="@android:color/transparent"/> </null_anal.customshape.CustomShapeLeft> <null_anal.customshape.CustomShapeRight android:layout_width="match_parent" android:layout_height="70dp" android:id="@+id/triangle_4" android:layout_marginTop="-36dp" android:background="@android:color/holo_green_dark" android:layout_below="@+id/triangle_3"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:textSize="22dp" android:layout_alignParentRight="true" android:padding="5dp" android:textColor="@android:color/black" android:text="ART" android:background="@android:color/transparent"/> </null_anal.customshape.CustomShapeRight> <null_anal.customshape.CustomShapeLeft android:layout_width="match_parent" android:layout_height="70dp" android:id="@+id/triangle_5" android:layout_marginTop="-36dp" android:background="@android:color/holo_blue_dark" android:layout_below="@+id/triangle_4"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:textSize="22dp" android:padding="5dp" android:textColor="@android:color/black" android:text="KIDS" android:background="@android:color/transparent"/> </null_anal.customshape.CustomShapeLeft> <null_anal.customshape.CustomShapeRight android:layout_width="match_parent" android:layout_height="70dp" android:id="@+id/triangle_6" android:layout_marginTop="-36dp" android:background="@android:color/holo_blue_bright" android:layout_below="@+id/triangle_5"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:textSize="22dp" android:layout_alignParentRight="true" android:padding="5dp" android:textColor="@android:color/black" android:text="MUSIC" android:background="@android:color/transparent"/> </null_anal.customshape.CustomShapeRight>
CustomShapeRight customShapeRight = (CustomShapeRight) findViewById(R.id.triangle_6); customShapeRight.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (((CustomShapeRight)v).isTouched(event.getX(), event.getY())) { Toast.makeText(getApplicationContext(), ((TextView)((CustomShapeRight) v).getChildAt(0)).getText(), Toast.LENGTH_SHORT).show(); } return false; } });