I have created a custom view inside which I have drawn multiple arcs of different colors.
On touching how can I get the color of the touched point?
I have created a custom view inside which I have drawn multiple arcs of different colors.
On touching how can I get the color of the touched point?
In Java:
final Bitmap bitmap = Bitmap.createBitmap(customView.getWidth(), customView.getHeight(), Bitmap.Config.ARGB_8888);
customView.draw(new Canvas(bitmap));
customView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int color = bitmap.getPixel((int) event.getX(), (int) event.getY());
return true;
}
});
In Kotlin:
val bitmap = Bitmap.createBitmap(customView.getWidth(), customView.getHeight(), Bitmap.Config.ARGB_8888)
customView.draw(Canvas(bitmap))
customView.setOnTouchListener(View.OnTouchListener { _, event ->
val color = bitmap.getPixel(event.x.toInt(), event.y.toInt())
true
})
Solution to above has two step's
Step 1: Getting the bitmap for your View as canvas is nothing more than a container which holds drawing calls to manipulate a bitmap
.As view can update itself based on user event's or some other case then bitmap need's to be updated on onDraw call.
refer here how to do it.
Step 2: And once you get hold of bitmap get the x and y position from event of view and get specific color of pixel.
refer here how to do it.