У меня есть пользовательский вид, который я добавил к макету фрагмента. Я хочу определить координаты контуров в моем пользовательском представлении на основе изображения, которое находится внутри макета моего фрагмента. Я пытался использовать findViewById и привязку данных, но когда я это сделал, фрагмент даже не появился в моем приложении.
См. Мой код CustomView:
class StarLineView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr) {
private var path = Path()
private var paint = Paint()
private val dashes = floatArrayOf(90f, 90f)
init {
paint = Paint().apply {
...
}
path = Path().apply {
moveTo(100f, 100f)
lineTo(100f, 0f)
}
val lineAnim = ValueAnimator.ofFloat(100f, 0f)
...
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
canvas!!.drawPath(path, paint)
}
}
И макет моего фрагмента:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradient">
<ImageView
android:id="@+id/starOffButton"
android:layout_width="100dp"
android:layout_height="100dp"
android:contentDescription="@string/starOffButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/star_off_intermediary" />
...
<com.example.android.myproject.customViews.StarLineView
android:id="@+id/starLineView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
У меня нет файла. xml только для моего customView, я не понимаю, почему, если у меня есть способ добавить непосредственно в фрагмент xml. Таким образом, позиция StarLineView должна основываться на значениях starOffButton, оба находятся в макете одного фрагмента. Как я могу сделать это возможным? (Я не создал. xml только для своего пользовательского представления)