问题描述
我正在创建一个有 4 个角点的边界框。如果移动一个角点,则计算每个角点坐标。拖动每个角点时,边界框可以旋转以及放大/缩小
标记的角点坐标是计算出来的,不与间距对齐。任何想法将不胜感激
dragHandle1!!.setonTouchListener(object : View.OnTouchListener {
@SuppressLint("ClickableViewAccessibility")
override fun onTouch(v: View,e: MotionEvent): Boolean {
Log.i("dh","1")
if (e.action === MotionEvent.ACTION_DOWN) {
centerX = ((imageView!!.left + imageView!!.right) / 2f.toInt()).toFloat()
centerY = ((imageView!!.top + imageView!!.bottom) / 2f.toInt()).toFloat()
startX = e.rawX - dragHandle1!!.x + centerX
startY = e.rawY - dragHandle1!!.y + centerY
startR = Math.hypot((e.rawX - startX).todouble(),(e.rawY - startY).todouble())
.toFloat()
startA = Math.todegrees(
Math.atan2(
(e.rawY - startY).todouble(),(e.rawX - startX).todouble()
)
).toFloat()
startScale = imageView!!.scaleX
startRotation = imageView!!.rotation
Log.i("cor1","startX-${startX},startY-${startY}")
} else if (e.action === MotionEvent.ACTION_MOVE) {
val newR = hypot((e.rawX - startX),(e.rawY - startY))
val newA = Math.todegrees(
Math.atan2(
(e.rawY - startY).todouble(),(e.rawX - startX).todouble()
)
).toFloat()
val newScale: Float = newR / startR * startScale
val newRotation: Float = newA - startA + startRotation
Log.i("rot",newRotation.toString())
imageView!!.scaleX = newScale
imageView!!.scaleY = newScale
imageView!!.rotation = newRotation
dragHandle1!!.x = getCornerX(centerX,newScale,newRotation)
dragHandle1!!.y = getCornerY(centerY,newRotation)
Log.i("cor1","dh1x-${dragHandle1!!.x},dh2x-${dragHandle1!!.y}")
Log.i("cor_img","left-${imageView!!.left},right-${imageView!!.right}")
//-----------------------------------------------------------
dragHandle1!!.pivotX = 0f
dragHandle1!!.pivotY = 0f
dragHandle1!!.rotation = newRotation
// ----- this part takes some effort to understand... ------
var a = Rect()
a = imageView!!.drawable.bounds
val drawLeft: Int = a.left
val drawTop: Int = a.top
val drawRight: Int = a.right
val drawBottom: Int = a.bottom
dragHandle2!!.x = getCornerX(centerX,newRotation + 95)
dragHandle2!!.y = getCornerY(centerY,newRotation + 95)
dragHandle2!!.pivotX = 0f
dragHandle2!!.pivotY = 0f
dragHandle2!!.rotation = newRotation
Log.i("cor2","dh2x-${dragHandle2!!.x},dh2y-${dragHandle2!!.y}")
//
dragHandle3!!.x = getCornerX(centerX,newRotation + 180)
dragHandle3!!.y = getCornerY(centerY,newRotation + 180)
dragHandle3!!.pivotX = 0f
dragHandle3!!.pivotY = 0f
dragHandle3!!.rotation = newRotation
//
dragHandle4!!.x = getCornerX(centerX,newRotation + 265)
dragHandle4!!.y = getCornerY(centerY,newRotation + 265)
Log.i("shape_centerX","cx-${centerX.toString()},ns-${newScale},nr${newRotation+175}")
Log.i("shape_centerY",centerY.toString())
Log.i("shape_dg","dgx-${dragHandle4!!.x},dgy-${dragHandle4!!.y}")
dragHandle4!!.pivotX = 0f
dragHandle4!!.pivotY = 0f
dragHandle4!!.rotation = newRotation
} else if (e.action === MotionEvent.ACTION_UP) {
}
return true
}
})
fun getCornerX(centerX: Float,newScale: Float,newRotation: Float): Float {
val x = (centerX + (Math.hypot(
imageView!!.width.todouble(),imageView!!.height.todouble()
) / 2f * newScale
* Math.cos(
Math.toradians(newRotation.todouble()) + Math.atan2(
imageView!!.height.todouble(),imageView!!.width.todouble()
)
))).toFloat()
return x
}
fun getCornerY(centerY: Float,newRotation: Float): Float{
val y = (centerY + (Math.hypot(
imageView!!.width.todouble(),imageView!!.height.todouble()
) / 2f * newScale
* Math.sin(
Math.toradians(newRotation.todouble()) + Math.atan2(
imageView!!.height.todouble(),imageView!!.width.todouble()
)
))).toFloat()
return y
}
角点坐标需要调整间距。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"/>
<RelativeLayout
android:id="@+id/relativeLayout2"
android:background="@color/teal_200"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="@color/white_transparent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/jellyfish" />
<ImageView
android:id="@+id/dh1"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_below="@+id/imageView1"
android:layout_toRightOf="@+id/imageView1"
android:src="@drawable/gray_circle" />
<ImageView
android:id="@+id/dh2"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_below="@+id/imageView1"
android:layout_toLeftOf="@+id/imageView1"
android:background="@drawable/gray_circle" />
<ImageView
android:id="@+id/dh3"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_above="@+id/imageView1"
android:layout_toLeftOf="@+id/imageView1"
android:src="@drawable/gray_circle" />
<ImageView
android:id="@+id/dh4"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_above="@+id/imageView1"
android:layout_toRightOf="@+id/imageView1"
android:src="@drawable/gray_circle" />
</RelativeLayout>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)