使用来自不同视图的坐标绘制三角形

时间:2019-07-10 12:09:16

标签: android canvas

下面的图片说明了后面的想法:

XML看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
        android:orientation="horizontal"
        android:background="@android:color/black">

    <TextureView
            android:id="@+id/background"
            android:layout_width="match_parent"
            android:layout_height="match_parent" app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent" android:layout_marginTop="4dp" android:visibility="gone"/>

    <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/bigController"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            android:animateLayoutChanges="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.Guideline
                android:id="@+id/guideLineForBottomContainer"
                android:orientation="horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintGuide_percent="0.35"/>

        <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/bottomController"
                android:layout_width="225dp"
                android:layout_height="235dp"
                android:background="@drawable/round_rectangle_background"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@+id/guideLineForBottomContainer">

            <TextView
                    android:id="@+id/infoLabel"
                    style="@style/GeneralText"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:text="@string/car_side_recognition_label"
                    android:textStyle="bold"
                    android:textAllCaps="true"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    android:layout_marginTop="4dp"/>

            <ImageView
                    android:id="@+id/bigLogo"
                    android:layout_width="35dp"
                    android:layout_height="70dp"
                    android:src="@drawable/ic_dtc_vehicle"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/infoLabel" android:visibility="visible"/>

            <de.hdodenhof.circleimageview.CircleImageView
                    android:id="@+id/Left"
                    android:layout_width="45dp"
                    android:layout_height="45dp"
                    android:background="@drawable/gray_circle"
                    app:civ_border_width="1dp"
                    android:alpha="1"
                    android:tag="Left"
                    app:civ_border_color="@color/gray_dark"
                    app:layout_constraintEnd_toStartOf="@+id/carSideModeVehicle"
                    app:layout_constraintStart_toStartOf="@+id/FrontLeft" android:layout_marginTop="32dp"
                    app:layout_constraintTop_toBottomOf="@+id/FrontLeft"/>

            <de.hdodenhof.circleimageview.CircleImageView
                    android:id="@+id/FrontLeft"
                    android:layout_width="45dp"
                    android:layout_height="45dp"
                    android:background="@drawable/gray_circle"
                    app:civ_border_width="1dp"
                    android:alpha="1"
                    android:tag="FrontLeft"
                    app:civ_border_color="@color/gray_dark"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toStartOf="@+id/bigLogo"
                    app:layout_constraintHorizontal_bias="0.48"
                    tools:layout_editor_absoluteY="34dp"/>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

我想做的是获取FrontLeft视图的精确坐标,并获得bigLogo视图的精确左下角+左上角坐标,并将它们传递给函数给我画一个三角形我创建了绘图功能,它可以正常工作,但似乎无法传递精确的坐标。

我的解决方案:

    val coordinates = ArrayList<Float>()

    val frontLeftRect = Rect()
    val centerRect = Rect()

    FrontLeft.getLocalVisibleRect(frontLeftRect)
    bigLogo.getLocalVisibleRect(centerRect)


    with (coordinates) {
        add(frontLeftRect.right.toFloat())
        add(frontLeftRect.right.toFloat())

        add(centerRect.top.toFloat())
        add(centerRect.top.toFloat())

        add(centerRect.bottom.toFloat())
        add(centerRect.bottom.toFloat())
    }

    drawTriangle(
        coordinates,
        "xxx"
    )

这将返回错误的坐标。

如果我使用getGlobalVisibleRect / getLocalVisibleRect,则看不到任何内容(创建并添加了视图,但肉眼看不见)。我猜坐标太差了,以至于看不到,因为我之前确实设法画了一个难看的三角形(不同的方法)。有什么建议吗?

0 个答案:

没有答案