Android 在画布下画圆圈
你好! 我遇到了这样一个问题,我需要用手指画一个圆圈 我尝试使用以下代码:Android 在画布下画圆圈,android,canvas,bitmap,position,draw,Android,Canvas,Bitmap,Position,Draw,你好! 我遇到了这样一个问题,我需要用手指画一个圆圈 我尝试使用以下代码: private fun pressFinger() { mFirst.setOnTouchListener(View.OnTouchListener { v, event -> val x = event.x val y = event.y when (event.action) { MotionEvent.ACTIO
private fun pressFinger() {
mFirst.setOnTouchListener(View.OnTouchListener { v, event ->
val x = event.x
val y = event.y
when (event.action) {
MotionEvent.ACTION_DOWN -> {
drawCircle(x, y)
}
}
return@OnTouchListener true
})
}
fun drawCircle(x: Float, y: Float) {
val width: Int = x.toInt()
val height: Int = y.toInt()
val bitmap = (mFirst.getDrawable() as BitmapDrawable).bitmap
val workingBitmap: Bitmap = Bitmap.createBitmap(bitmap)
val mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true)
val canvasBitmap = Canvas(mutableBitmap)
val paint = Paint(Paint.ANTI_ALIAS_FLAG)
paint.style = Paint.Style.FILL
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR)
canvasBitmap.drawCircle(width.toFloat(), height.toFloat(), 180F, paint)
mFirst.setImageBitmap(mutableBitmap)
drawUtils.draw()
}
问题是圆圈不是在手指下面画的,而是在侧面画的
我还要注意,放在ImageView中的图像的大小更大,我使用scaleType=“cropCenter”。
事实证明,我需要在ImageView中放置的图像上准确地获得手指的位置,而不仅仅是在屏幕上
如果有人知道问题的正确答案,我将不胜感激。画布位图.drawCircle(width.toFloat()-180F/2,height.toFloat()-180F/2,180F,paint)
试试看。这是一个非常糟糕的代码。为什么要在视图中编辑背景图像?更简单的方法是在onTouch函数中保存坐标,然后使视图无效。然后在视图的onDraw中,在该位置绘制一个圆。这样你就避免了所有的缩放问题,而且你不必复制两份!!!位图的使用效率非常低。不需要。根据绘制圆的文件,坐标是圆的中心。如果它位于右下角,则这是正确的。