2016-08-16 120 views
0

我想在RelativeLayout中的兩個視圖之間畫線,但無法做到這一點。在Android中的兩個視圖之間畫線

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); 
    } 
} 

編輯

我想從他們的中心兩個視圖可以改變,重繪之間畫線的時候,視圖改變它們的位置。

+1

你可以使用一個更多的視圖之間2 ..有背景作爲線條顏色,寬度1 DP(線的厚度)。 – Pr38y

+0

你有沒有調試過你的onDraw()? getX()和getY()是否真的包含視圖的座標? – Chris623

回答

0

看到this響應,加入RelativeLayout之間的新View隨高度1DP應該足夠了:

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

希望它可以幫助!

0

您可以使用此!

<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" /> 
+0

查看我的編輯plz – Nepster

0

如果你想水平分離,然後

<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" 

``機器人:layout_height = 「match_parent」 的android:背景= 「@安卓顏色/ darker_gray」/>

我要在列表視圖中分離,那麼你需要添加標籤 機器人:devider = 「5DP」

+0

查看我的編輯plz – Nepster

+0

你能告訴我xml文件嗎? – user6615010

+0

getListView()。setDivider(null); getListView()。setDividerHeight(5); – user6615010

相關問題