Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 两个视图之间的自定义绘制线工作不正常_Android_Android Canvas_Android Custom View - Fatal编程技术网

Android 两个视图之间的自定义绘制线工作不正常

Android 两个视图之间的自定义绘制线工作不正常,android,android-canvas,android-custom-view,Android,Android Canvas,Android Custom View,我试图在安卓系统中的按钮之间划清界限。我创建了一个自定义类,它在相对布局(相对布局是父布局)中的按钮之间绘制线 下面是我的MatchTheColumnDrawView.java类,它接受上下文、开始视图、结束视图、线条颜色、结束循环颜色、厚度(浮动)、方向(从左到右或从右到左) 导入android.content.Context; 导入android.graphics.Canvas; 导入android.graphics.Color; 导入android.graphics.DashPathEff

我试图在安卓系统中的按钮之间划清界限。我创建了一个自定义类,它在相对布局(相对布局是父布局)中的按钮之间绘制线

下面是我的MatchTheColumnDrawView.java类,它接受上下文、开始视图、结束视图、线条颜色、结束循环颜色、厚度(浮动)、方向(从左到右或从右到左)

导入android.content.Context;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.graphics.DashPathEffect;
导入android.graphics.Paint;
导入android.graphics.Path;
导入android.util.Log;
导入android.view.view;
/**
*由阿披实于2016年12月21日创建。
*
*2017年10月9日根据新设计进行的变更
*
*/
公共类匹配ColumnDrawView扩展视图{
公共静态最终int LEFT_至_RIGHT=1,RIGHT_至_LEFT=2;
私家漆、胶漆、磁漆;
私有视图开始视图、结束视图;
私人int方向;
私人帆布;
专用浮点数宽度=15f;
私人浮动dashGap=8f;
浮点[]间隔=新浮点[]{dashWidth,dashGap};
浮相=0;
private int LINE_COLOR=COLOR.parseColor(“#BEBEBE”);
private int END_CIRCLE_COLOR=COLOR.parseColor(“#FF99CC00”);
/**
*
*参数化构造函数从
*@startView
*到
*@endView
*依照
*@方向
*及
*@lineColor
*及
*@endCircleColor
*
*什么时候
*@lineColor==null
*默认线条颜色为灰色
*
*什么时候
*@endCircleColor==null
*默认endCircleColor为绿色
* */
公共匹配ColumnDrawView(上下文,
查看开始视图,查看结束视图,
字符串lineColor、字符串endCircleColor、,
浮动厚度(整数方向){
超级(上下文);
mLinePaint=新油漆();
如果(lineColor!=null)LINE\u COLOR=COLOR.parseColor(lineColor);
MLINPAINT.setColor(线条颜色);
M油漆设置行程宽度(厚度);
MLINPAINT.setStyle(油漆、样式、笔划);
mCirclePaint=新油漆();
如果(endCircleColor!=null)END\u CIRCLE\u COLOR=COLOR.parseColor(endCircleColor);
mCirclePaint.setColor(结束圆颜色);
M循环漆设置行程宽度(厚度);
this.startView=startView;
this.endView=endView;
这个方向=方向;
//将背景颜色设置为透明
super.setBackgroundColor(颜色.透明);
}
公共视图getStartView(){
返回startView;
}
公共无效设置开始视图(视图开始视图){
this.startView=startView;
}
公共视图getEndView(){
返回端视图;
}
公共无效setEndView(视图endView){
this.endView=endView;
}
公共画布getCanvas(){
返回画布;
}
公共int getDirection(){
返回方向;
}
公共方向(内部方向){
这个方向=方向;
}
@凌驾
受保护的void onDraw(画布){
this.canvas=画布;
Log.d(“方向”,String.valueOf(方向));
Log.d(“开始视图Y:”,String.valueOf(startView.getY());
Log.d(“开始视图H:”,String.valueOf(startView.getHeight());
Log.d(“结束视图Y:”,String.valueOf(endView.getY());
Log.d(“结束视图H:”,String.valueOf(endView.getHeight());
//默认情况下,从左到右
如果(方向==从右到左){
//从右到左
//计算左侧X和高度中间Y
/*
*                   ______________
*                  |              |
*这一点==>>。||
*                  |              |
*                  |______________|
* */
float startView-eftx=startView.getX();
float startViewMidHeightY=startView.getY()+startView.getheighty()/2;
//计算右侧X和高度中间Y
/*
*     ______________
*    |              |

*| |.您好,请尝试以下代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp">

 <Button
    android:layout_weight="1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<View
    android:layout_weight="5"
    android:layout_width="match_parent"
    android:background="#313131"
    android:layout_gravity="center_vertical"
    android:layout_height="3dp"/>

<Button
    android:layout_weight="1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" /></LinearLayout>

目前,您正在以编程方式将
匹配ColumnDrawView
添加到
匹配以下尝试的
中,而无需设置
LayoutParams
。有
LayoutParams
的默认值,因此只要有一个
匹配以下尝试的
实例,您就很幸运了

因此,要解决此问题,必须将
LayoutParams
设置为
匹配ColumnDrawView
s,例如:

for (int i = 0; i < numberOfOneSideButtons; i++) {

    MatchTheColumnDrawView matchTheColumnDrawView = new MatchTheColumnDrawView(
                       mContext, leftSideButtons.get(i), 
                       rightSideButtons.get(numberOfOneSideButtons - 1 - i),
                       null, null,
                       2.0f, MatchTheColumnDrawView.LEFT_TO_RIGHT);

    RelativeLayout.LayoutParams layoutParams = new LayoutParams(
                                      ViewGroup.LayoutParams.MATCH_PARENT, 
                                      ViewGroup.LayoutParams.MATCH_PARENT);
    layoutParams.addRule(ALIGN_TOP, 
                  leftSideButtons.get(0).getId());
    layoutParams.addRule(ALIGN_BOTTOM, 
                  leftSideButtons.get(numberOfOneSideButtons - 1).getId());
    matchTheColumnDrawView.setLayoutParams(layoutParams);

    matchTheColumnDrawViewArrayList.add(matchTheColumnDrawView);

    addView(matchTheColumnDrawView);
}
由于
按钮
有自己的填充(看起来比实际尺寸小),圆圈仍然不会与可见的
按钮
边缘重叠。要实现这一点,可以使用自己的按钮背景,或者(有点粗俗,因为填充在未来的Android版本中可能会改变)从x坐标中减去填充(左侧)分别将填充添加到x坐标(右侧)

下面的屏幕截图显示了一个示例,其中包含一些自定义的
视图
s和一些
按钮
s:


我只想通过以下匹配添加它。因为我想动态地使用绘制线。然后你必须根据场景使用列表适配器添加视图,你可以隐藏和显示线。我会尝试让你知道。谢谢你的解释。@Abhishek-不客气!解决这个问题花了一些时间,我非常喜欢,不是吗至少因为你的详细问题让我能够进行测试,而不仅仅是猜测。我如何从边的中点开始选择线?有什么解决方案吗?@Abhishek-你是说绿点应该以中点为中心点吗?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <MatchTheFollowingAttempted
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            </MatchTheFollowingAttempted>

        </LinearLayout>

    </ScrollView>

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <MatchTheFollowingAttempted
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            </MatchTheFollowingAttempted>

            <MatchTheFollowingAttempted
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            </MatchTheFollowingAttempted>

        </LinearLayout>

    </ScrollView>

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp">

 <Button
    android:layout_weight="1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<View
    android:layout_weight="5"
    android:layout_width="match_parent"
    android:background="#313131"
    android:layout_gravity="center_vertical"
    android:layout_height="3dp"/>

<Button
    android:layout_weight="1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" /></LinearLayout>
for (int i = 0; i < numberOfOneSideButtons; i++) {

    MatchTheColumnDrawView matchTheColumnDrawView = new MatchTheColumnDrawView(
                       mContext, leftSideButtons.get(i), 
                       rightSideButtons.get(numberOfOneSideButtons - 1 - i),
                       null, null,
                       2.0f, MatchTheColumnDrawView.LEFT_TO_RIGHT);

    RelativeLayout.LayoutParams layoutParams = new LayoutParams(
                                      ViewGroup.LayoutParams.MATCH_PARENT, 
                                      ViewGroup.LayoutParams.MATCH_PARENT);
    layoutParams.addRule(ALIGN_TOP, 
                  leftSideButtons.get(0).getId());
    layoutParams.addRule(ALIGN_BOTTOM, 
                  leftSideButtons.get(numberOfOneSideButtons - 1).getId());
    matchTheColumnDrawView.setLayoutParams(layoutParams);

    matchTheColumnDrawViewArrayList.add(matchTheColumnDrawView);

    addView(matchTheColumnDrawView);
}
float endViewMidHeightY = endView.getY() + endView.getHeight() / 2 - 10;