如何在android中的两个视图之间画线?

时间:2012-05-19 09:41:27

标签: android android-canvas android-view

我想在两个视图之间绘制一条线,如何使用canvas

2 个答案:

答案 0 :(得分:3)

简单在两个视图之间为它设置一个linearLayout

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="#285A8C" > //Give Color As u want
        </LinearLayout>

答案 1 :(得分:0)

感谢每一个我得到以下解决方案我使用三个布局我用于画布的中间布局和点击事件按钮上的画布中的绘制点

DrawView.java

  package demo.example;

 import android.content.Context;
 import android.graphics.Canvas;
 import android.graphics.Color;
 import android.graphics.Paint; 
 import android.view.View;


public class DrawView extends View {
  Paint paint = new Paint();
   float [][] points;
    public DrawView(Context context, float[][] points2, int k) {
    super(context);
    paint.setColor(Color.BLUE);
    paint.setStrokeWidth(5);
    this.points=points2;
}

@Override
 public void onDraw(Canvas canvas) {
    for(int i=0;i<points.length;i++)
    {
    canvas.drawLine(points[i][0],points[i][1],points[i][2],points[i][3], paint);
    }    
  }
 }

使用以下点击侦听器获取积分

        public  class listener implements OnClickListener
   {
      int i;   
      listener(int k)
       {
           this.i=k;
       }
        public void onClick(View v)
        {
            for(int k=0;k<match1.length;k++)
            {   
                    if(match1[k].isClickable()==false)
                   {    
                     if(match1[k].getId()==match2[i].getId())
                     {

                        points[k][0]=match1[k].getLeft();
                             points[k]1]=match1[k].getTop()+30+linearLayout2.getTop();                       points[k][2]=200;
                         points[k][3]=match2[i].getTop()+30+linearLayout2.getTop();

                         match1[k].setCompoundDrawablesWithIntrinsicBounds(null, null,null,null);
                         match2[i].setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.bubble), null,null, null);

                         Toast.makeText(getBaseContext(),points[0]+"-"+points[1]+"-"+points[2]+"-"+points[3],Toast.LENGTH_SHORT).show();    

                     ViewGroup.LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

                     drawView = new DrawView(getBaseContext(),points,k);
                                     linearLayout2.addView(drawView);

                     } 
                   }
                  }
        }
}