如何通过获取ImageView的位置在两个ImageView之间绘制一条线?

时间:2019-02-21 14:51:31

标签: android android-layout

这是MainActivity.java代码,它获取imagViews的位置,并尝试通过获取ImageViews的坐标来在ImageViews之间生成lineView。

我正在获取ImageViews的坐标,但未显示它们之间的线。

public class MainActivity extends AppCompatActivity {

    private ImageView one,two;
    private LineView lineView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lineView=findViewById(R.id.lineView);

        one=findViewById(R.id.one);
        two=findViewById(R.id.two);  
        Random random = new Random();
        int x=random.nextInt(500);
        int y=random.nextInt(500);
        one.setX(x);
        one.setY(y);
        int c=x;
        int d=y;
        PointF pointA=new PointF(c,d);
        x=random.nextInt(500);
        y=random.nextInt(500);
        two.setX(x);
        two.setY(y);
        c=x;
        d=y;
        PointF pointB=new PointF(c,d);
        lineView.setPointA(pointA);
        lineView.setPointB(pointB);
        lineView.draw();
    }
}

这是activity_main.xml,其中包含ImageViews和LineView。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
<xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    >

    <ImageView
        android:id="@+id/one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="116dp"
        android:layout_marginTop="197dp"

        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/one" />

    <ImageView
        android:id="@+id/two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="133dp"

        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/two" />

    <com.example.testing.LineView
        android:id="@+id/lineView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.constraint.ConstraintLayout>

这是Lineview.java代码,用于生成线视图小部件及其操作。

public class LineView extends View {

    PointF pointA,pointB;
    private Paint paint =new Paint();
    public LineView(Context context) {
        super(context);
    }

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

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

    @Override
    protected void onDraw(Canvas canvas) {


        super.onDraw(canvas);

        paint.setColor(Color.RED);
        paint.setStrokeWidth(20);
        canvas.drawLine(pointA.x,pointA.y,pointB.x,pointB.y,paint);
    }

    public void setPointA(PointF point)
    {
        pointA=point;
    }
    public void setPointB(PointF point)
    {
        pointB=point;
    }

    public void draw()
    {
        invalidate();
        requestLayout();
    }

}

3 个答案:

答案 0 :(得分:0)

您可以在自己的视图中进行操作-将View放在图像之间:

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

答案 1 :(得分:0)

android中两个图像视图之间的垂直线

<View
  android:layout_width="1dp"
  android:layout_height="match_parent"
  android:background="#FF0000FF"/>

答案 2 :(得分:0)

android中两个图像视图之间的水平线

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#FF0000FF"/>