【问题标题】:Draw line between two Views in Android在Android中的两个视图之间画线
【发布时间】:2016-08-16 10:57:13
【问题描述】:

我试图在 RelativeLayout 中的两个视图之间画线,但无法做到这一点。

    public class CustomRelativeLayout extends RelativeLayout {

    private Context mContext;
    private ImageButtonCustom[] imageButtonCustoms = new ImageButtonCustom[3];
    private Paint paint;
    CustomRelativeLayout customRelativeLayout;

    //private LineView lineView;
    public CustomRelativeLayout(Context context) {
        super(context);
        this.mContext = context;
        init();
    }

    public CustomRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init();
    }

    public CustomRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        init();
    }

    private void init() {
        //setBackgroundColor(Color.BLACK);
        customRelativeLayout = this;
        setWillNotDraw(false);
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStrokeWidth((float) 25);

        for(int x = 0 ; x < 3 ; x++){
            imageButtonCustoms[x] = new ImageButtonCustom(mContext,customRelativeLayout);
            imageButtonCustoms[x].setOnMoveListener(new ImageButtonCustom.OnMoveListener() {
                @Override
                public void onMove(Position positionXY) {

                }
            });
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


        if(imageButtonCustoms[0] != null && imageButtonCustoms[1] != null)
            canvas.drawLine(imageButtonCustoms[0].getCenterX(),imageButtonCustoms[0].getCenterY()
        ,imageButtonCustoms[1].getCenterX(),imageButtonCustoms[1].getCenterY(),paint);

        if(imageButtonCustoms[1] != null && imageButtonCustoms[2] != null)
            canvas.drawLine(imageButtonCustoms[1].getCenterX(),imageButtonCustoms[1].getCenterY()
                    ,imageButtonCustoms[2].getCenterX(),imageButtonCustoms[2].getCenterY(),paint);

        if(imageButtonCustoms[0] != null && imageButtonCustoms[2] != null)
            canvas.drawLine(imageButtonCustoms[0].getCenterX(),imageButtonCustoms[0].getCenterY()
                    ,imageButtonCustoms[2].getCenterX(),imageButtonCustoms[2].getCenterY(),paint);


    }
}

在哪里 ImageButtonCustom

   public class ImageButtonCustom extends ImageButton implements View.OnTouchListener{

    float dX, dY;

    private RelativeLayout rootView;
    private ImageButtonCustom imageButtonCustom;
    private OnMoveListener onMoveListener;

    public ImageButtonCustom(Context context,RelativeLayout rootView){
        super(context);
        this.rootView = rootView;
        init();

    }
    public ImageButtonCustom(Context context) {
        super(context);
        init();
    }

    public ImageButtonCustom(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ImageButtonCustom(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        imageButtonCustom = this;
        setImageResource(R.drawable.bobo2);
        setBackgroundColor(Color.TRANSPARENT);
        setOnTouchListener(this);

        /*RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        rl.addRule(RelativeLayout.ALIGN_BOTTOM);*/

        rootView.addView(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                dX = v.getX() - event.getRawX();
                dY = v.getY() - event.getRawY();
                break;
            case MotionEvent.ACTION_UP:
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                break;
            case MotionEvent.ACTION_POINTER_UP:
                break;
            case MotionEvent.ACTION_MOVE:
                v.animate()
                        .x(event.getRawX() + dX)
                        .y(event.getRawY() + dY)
                        .setDuration(0)
                        .start();
                //no use of ViewPositionUtil
                onMoveListener.onMove(ViewPositionUtil.getXYPositionRelativeToRoot(imageButtonCustom));//positionXY);
                break;
        }
        rootView.invalidate();
        return true;
    }

    public void setOnMoveListener(OnMoveListener onMoveListener){
        this.onMoveListener = onMoveListener;
    }

    public float getCenterX(){
        return getX() + getWidth()  / 2;

    }
    public float getCenterY(){
        return getY() + getHeight() / 2;

    }

    public interface OnMoveListener{
        void onMove(Position positionXY);
    }
}

编辑

我想在两个视图之间从它们的中心画线,当视图改变它们的位置时可以改变和重绘。

【问题讨论】:

  • 您可以在 2.. 之间再使用一个视图,背景为线条颜色,宽度 1 dp(线条粗细)。
  • 你已经调试过你的 onDraw() 了吗? getX() 和 getY() 真的包含视图的坐标吗?
  • @Jawad Zeb:你解决问题了吗?请分享。
  • 我现在无法访问代码。但固定
  • @Jawad Zeb:我能看到实现它的应用程序吗?

标签: android android-canvas draw android-custom-view


【解决方案1】:

如果你想要水平分离那么

<View
    android:id="@+id/view"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray"/>

如果你想要垂直分离那么

<View
      android:id="@+id/view"
      android:layout_width="1dp"
      android:layout_height="match_parent"
      android:background="@android:color/darker_gray"/>

如果你想在列表视图中分离,那么你需要添加到&lt;ListView /&gt;标签

android:divider="5dp" 

【讨论】:

  • getListView().setDivider(null); getListView().setDividerHeight(5);
  • 我在没有 xml 的情况下动态生成视图。
  • 所以你可以在上面使用我的评论。 getListView().setDiv‌​iderHeight(5);或者你可以使用 yourListView.setDivider(new ColorDrawable(android.R.color.transparent)); yourListView.setDividerHeight(5);
  • 我不想在两个视图之间画一条线分隔符。我想在两个能够在屏幕上移动的圆心之间画一条线。当它们从一个位置移动到另一个位置时,这条线也随之移动
  • 绘画 fgPaintSel = new Paint(); fgPaintSel.setARGB(255, 0, 0,0); fgPaintSel.setStyle(Style.STROKE); fgPaintSel.setPathEffect(new DashPathEffect(new float[] {10,20}, 0));
【解决方案2】:

this 响应中看到,在高度为1dp 的RelativeLayout 之间添加一个新的View 应该足够了:

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray"/>

希望对你有帮助!

【讨论】:

    【解决方案3】:

    你可以用这个!

     <TextView
                android:id="@+id/textView1"
                style="@style/behindMenuItemLabel1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="1dp"
                android:text="FaceBook Feeds" />
    
             <View
                 android:layout_width="fill_parent"
                 android:layout_height="2dp"
                 android:background="#d13033"/><!-- this is line -->
    
             <ListView
                android:id="@+id/list1"
                android:layout_width="350dp"
                android:layout_height="50dp" />
    

    【讨论】:

      【解决方案4】:

      我认为这里真正的问题是您的 onDraw() 方法没有调用。将此添加到您的构造函数中。供参考:Own defined Layout , onDraw() method not getting called

      this.setWillNotDraw(false);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-06
        • 1970-01-01
        • 2013-10-07
        • 2016-05-13
        • 1970-01-01
        • 1970-01-01
        • 2013-10-03
        相关资源
        最近更新 更多