我有一个imageview,当我在屏幕上触摸时,我会选择点。但我不想把圈子放在imageview限制之外。图像是具有人体轮廓的SVG。如何定义图像的限制?
这是我的代码:
科特林:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var layout: RelativeLayout = findViewById(R.id.layout1)
layout.addView(CustomView(this))
var imageBody: ImageView = findViewById(R.id.body)
var mBitmap: Bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888)
}
class CustomView(context: Context) : View(context) {
val brush = Paint()
var coordX: Float = 0.0f
var coordY: Float = 0.0f
override fun onTouchEvent(event: MotionEvent?): Boolean {
var X = event!!.getX()
var Y = event.getY()
coordX = X
coordY = Y
invalidate()
var text :String = "X: " + coordX + "\nY: " + coordY
Toast.makeText(context, text, Toast.LENGTH_LONG).show()
return super.onTouchEvent(event)
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
/*Circle Color*/
brush.setARGB(255, 105, 103, 103)
/*Circle*/
canvas.drawCircle(coordX, coordY, 10f, brush)
}
}
答案 0 :(得分:0)
嗯,那个简单的数学。在onTouchEvent()
实施查询getMeasuredWidth()
和getMeasuredHeight()
中,根据您想要绘制的圆圈来确定触摸事件的坐标。
由于您的圈子的半径为10f
,coordX
为Math.max(10f, Math.min(X, getMeasuredWidth() - 10f))
,coordY
同样为Math.max(10f, Math.min(X, getMeasuredHeight() - 10f))
。