2016-08-16 6 views
0

私はRelativeLayoutの2つのビューの間に線を描こうとしていますが、これを行うことはできません。私は、ビューが自分の位置を変更したときに変更して再描画することができ、その中心から二つのビューの間に線を描きたいImageButtonCustomAndroidの2つのビュー間に線を引く

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

編集

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


    } 
} 

+1

あなたは線色、幅1 dp(線の太さ)として背景を持つ2 ..の間にもう1つのビューを使用することができました。 – Pr38y

+0

あなたはすでにあなたのonDraw()をデバッグしましたか? getX()とgetY()に実際にビューの座標が含まれていますか? – Chris623

答えて

0

this応答で見た、高さ1DPとRelativeLayout間の新しいViewを追加するだけでは十分でなければならない:

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

は、それはあなたがこれを使用することができます!

0

を役に立てば幸い!あなたは垂直方向の分離をしたい場合は、水平分離、その後

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

をしたい場合は

<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

私の知りたいことを聞きました – Nepster

0

その後、

<View 
    android:id="@+id/view" 
    android:layout_width="1dp" 

``アンドロイド:layout_height = "match_parent" アンドロイド:背景= "@アンドロイド:カラー/ darker_gray" />

私はあなたがリストビューの分離をしたい、あなたがタグ アンドロイド追加する必要があります。私が思うデバイダ= "5DP"

+0

私のplzを参照 – Nepster

+0

私はxmlファイルを表示できますか? – user6615010

+0

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

0

をここでの本当の問題は、onDraw()メソッドが呼び出していないことです。コンストラクタでこれを追加します。参照のためにこれを見てください:Own defined Layout , onDraw() method not getting called

this.setWillNotDraw(false); 
関連する問題