Custom views in android come in all sizes, shapes and forms. Besides, complex views would like to notify of a click only when a part of the view is clicked. This could specially be a challenge for custom views based on View. This could be achieved via Android's Region API. The logic is to create a region corresponding to the desired section of the view and check if the touch coordinates notified via onTouch(...) falls in the specific region.
final Region region = new Region( left, top, right, bottom );
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_UP: {
if (region.contains((int) x, (int) y)) {
notifyOnClickListeners();
}
}
break;
}
return super.onTouchEvent(event);
}
final Region region = new Region( left, top, right, bottom );
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_UP: {
if (region.contains((int) x, (int) y)) {
notifyOnClickListeners();
}
}
break;
}
return super.onTouchEvent(event);
}
No comments:
Post a Comment