Android 每次我触摸屏幕时画一个圆圈
我的目标是每次触摸屏幕时画一个圆圈,而如果我想再触摸一次,上一个圆圈就会消失 现在发生的事情是,前面的圆圈并没有消失,所以每次我触摸屏幕时,它们就加起来了 这是我的密码: 科特林:Android 每次我触摸屏幕时画一个圆圈,android,draw,ontouchlistener,Android,Draw,Ontouchlistener,我的目标是每次触摸屏幕时画一个圆圈,而如果我想再触摸一次,上一个圆圈就会消失 现在发生的事情是,前面的圆圈并没有消失,所以每次我触摸屏幕时,它们就加起来了 这是我的密码: 科特林: override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) var imageBod
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var imageBody: ImageView = findViewById(R.id.imageViewBody)
imageBody.isDrawingCacheEnabled = true
imageBody.buildDrawingCache(true)
imageBody.setOnTouchListener(OnTouchListener { v, event ->
if(event != null){
if (event.action == MotionEvent.ACTION_DOWN) {
val bitmap: Bitmap = imageBody.drawingCache
val pixel: Int = bitmap.getPixel(event.getX().toInt(), event.getY().toInt())
coordX = event.getX()
coordY = event.getY()
val Drawable = imageBody.drawable
val ImageBounds = Drawable.bounds
val scaledHeight = ImageBounds.height()
val scaledWidth = ImageBounds.width()
OrigX = coordX / scaledHeight
OrigY = coordY / scaledWidth
when (pixel) {
Color.rgb(241,241,241) -> {
val canvas = Canvas(bitmap)
val paint = Paint()
paint.color = Color.rgb(255,128,0)
canvas.drawCircle(coordX, coordY, 15F, paint) /**DRAW CIRCLE*/
imageBody.setImageBitmap(bitmap)
imageBody.Invalidate()
}
}
}
}
false
})
}
试试这个。清除操作
action\u UP
中imageView的位图。下面是示例代码:
首先,将位图置于onTouchListener
之外,如下所示:
private var bitmap: Bitmap? = null
然后,您的onTouchListener
将如下所示:
imageBody.setOnTouchListener({ v, event ->
if(event != null){
if (event.action == MotionEvent.ACTION_DOWN) {
bitmap = Bitmap.createBitmap(imageBody.drawingCache)
val pixel: Int = bitmap.getPixel(event.getX().toInt(), event.getY().toInt())
coordX = event.getX()
coordY = event.getY()
val Drawable = imageBody.drawable
val ImageBounds = Drawable.bounds
val scaledHeight = ImageBounds.height()
val scaledWidth = ImageBounds.width()
OrigX = coordX / scaledHeight
OrigY = coordY / scaledWidth
when (pixel) {
Color.rgb(241,241,241) -> {
val canvas = Canvas(bitmap)
val paint = Paint()
paint.color = Color.rgb(255,128,0)
canvas.drawCircle(coordX, coordY, 15F, paint) /**DRAW CIRCLE*/
imageBody.setImageBitmap(bitmap)
imageBody.Invalidate()
}
}
}
if (event.action == MotionEvent.ACTION_UP) {
if (bitmap != null && !bitmap!!.isRecycled) {
bitmap?.recycle()
bitmap = null
}
}
}
true
})
在绘制圆之前,请尝试清除onclick中的imageBody位图。每次触摸并向上移动指针时,都必须绘制一个新视图action@Tepits我尝试了“imageBody.ClearFindViewByDCache()”,但它不起作用。@Dr.aNdRO你能帮我吗?请查看关于如何在画布上绘制的链接非常感谢,你的解决方案解决了我的问题。欢迎你。请将此标记为正确答案,以便每个人都知道您的问题已解决。
imageBody.setOnTouchListener({ v, event ->
if(event != null){
if (event.action == MotionEvent.ACTION_DOWN) {
bitmap = Bitmap.createBitmap(imageBody.drawingCache)
val pixel: Int = bitmap.getPixel(event.getX().toInt(), event.getY().toInt())
coordX = event.getX()
coordY = event.getY()
val Drawable = imageBody.drawable
val ImageBounds = Drawable.bounds
val scaledHeight = ImageBounds.height()
val scaledWidth = ImageBounds.width()
OrigX = coordX / scaledHeight
OrigY = coordY / scaledWidth
when (pixel) {
Color.rgb(241,241,241) -> {
val canvas = Canvas(bitmap)
val paint = Paint()
paint.color = Color.rgb(255,128,0)
canvas.drawCircle(coordX, coordY, 15F, paint) /**DRAW CIRCLE*/
imageBody.setImageBitmap(bitmap)
imageBody.Invalidate()
}
}
}
if (event.action == MotionEvent.ACTION_UP) {
if (bitmap != null && !bitmap!!.isRecycled) {
bitmap?.recycle()
bitmap = null
}
}
}
true
})