Android: How to reduce the viewing area?

I have two images whose transparent area overlaps with each other. When I click on one image, the onclicklistener called on another image. In any case, to reduce the area with an ImageView click.

+6
source share
3 answers

You create TouchDelegate :

 final View parent = (View) findViewById(R.id.touch_delegate_root); parent.post( new Runnable() { // Post in the parent message queue to make sure the parent // lays out its children before we call getHitRect() public void run() { final Rect rect = new Rect(); Button delegate = YourActivityClass.this.mButton; delegate.getHitRect(rect); rect.top -= 20; rect.bottom += 12; // etc parent.setTouchDelegate( new TouchDelegate( rect , delegate)); } }); 

refers to here

+2
source

You can solve it only with xml. Just frame your image and place another transparent view that you skip to click events on top of it. Adjust the size and position with the layout options:

 <FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="your_view" android:clickable="false" <!-- your other attributes --> <!-- ... --> /> <ImageView android:id="the_clickable_view" android:src="@null" <!-- set desired size of clickable area --> <!-- align it inside a frame using: android:gravity and android:margins --> /> </FrameLayout> 
+1
source

Do not use OnClickListener, but OnTouchListener and handle the click area yourself.

For example, by scaling a touch rectangle and moving it to the center of the view. You can also use radius or manual offsets.

 imageView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { final Rect rect = new Rect(); v.getHitRect(rect); float scale = 0.5f; final float x = event.getX(); final float y = event.getY(); final float minX = v.getWidth() * 0.5f * (1.0f - scale); final float maxX = v.getWidth() * 0.5f * (1.0f + scale); final float minY = v.getHeight() * 0.5f * (1.0f - scale); final float maxY = v.getHeight() * 0.5f * (1.0f + scale); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if (x > minX && x < maxX && y > minY && y < maxY) { Log.d("TOUCH", String.valueOf(x) + " " + String.valueOf(y)); } break; } return true; } }); 
0
source

Source: https://habr.com/ru/post/902093/


All Articles