喜糖

移动开发工程师 。涉及 android、ios、jni

导航

扩大View的点击范围

Posted on 2014-04-01 19:51  喜糖  阅读(382)  评论(0编辑  收藏  举报

扩大View的点击范围本人知道的有两种方法,在不影响界面效果的前提下:

1、在View的外面添加一个透明容器

2、就是本文要说的,代码如下 :

public void addToParentArea(final View view) {

        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        final float density = metric.density;

        final View parent = (View) view.getParent();
        parent.post(new Runnable() {
            public void run() {
                // View的点击范围向四周扩大30个单位
                final Rect r = new Rect();
                view.getHitRect(r);
                r.right += 30 * density;
                r.left += 30 * density;
                r.bottom += 30 * density;
                r.top += 30 * density;
                parent.setTouchDelegate(new TouchDelegate(r, view));
            }
        });
    }

参考文章:http://developer.android.com/training/gestures/viewgroup.html

Android provides the TouchDelegate class to make it possible for a parent to extend the touchable area of a child view beyond the child's bounds. This is useful when the child has to be small, but should have a larger touch region. You can also use this approach to shrink the child's touch region if need be.

In the following example, an ImageButton is the "delegate view" (that is, the child whose touch area the parent will extend). Here is the layout file:

   1:  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   
   2:    android:id="@+id/parent_layout"   
   3:    android:layout_width="match_parent"     
   4:    android:layout_height="match_parent"    
   5:    tools:context=".MainActivity" >    
   6:     <ImageButton android:id="@+id/button"         
   7:       android:layout_width="wrap_content"       
   8:       android:layout_height="wrap_content"        
   9:       android:background="@null"          
  10:       android:src="@drawable/icon" />
  11:  </RelativeLayout>

The snippet below does the following:

  • Gets the parent view and posts a Runnable on the UI thread. This ensures that the parent lays out its children before calling the getHitRect() method. The getHitRect() method gets the child's hit rectangle (touchable area) in the parent's coordinates.
  • Finds the ImageButton child view and calls getHitRect() to get the bounds of the child's touchable area.
  • Extends the bounds of the ImageButton's hit rectangle.
  • Instantiates a TouchDelegate, passing in the expanded hit rectangle and the ImageButton child view as parameters.
  • Sets the TouchDelegate on the parent view, such that touches within the touch delegate bounds are routed to the child.
In its capacity as touch delegate for the ImageButton child view, the parent view will receive all touch events. If the touch event occurred within the child's hit rectangle, the parent will pass the touch event to the child for handling.
 
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Get the parent view
        View parentView = findViewById(R.id.parent_layout);
        
        parentView.post(new Runnable() {
            // Post in the parent's message queue to make sure the parent
            // lays out its children before you call getHitRect()
            @Override
            public void run() {
                // The bounds for the delegate view (an ImageButton
                // in this example)
                Rect delegateArea = new Rect();
                ImageButton myButton = (ImageButton) findViewById(R.id.button);
                myButton.setEnabled(true);
                myButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(MainActivity.this, 
                                "Touch occurred within ImageButton touch region.", 
                                Toast.LENGTH_SHORT).show();
                    }
                });
     
                // The hit rectangle for the ImageButton
                myButton.getHitRect(delegateArea);
            
                // Extend the touch area of the ImageButton beyond its bounds
                // on the right and bottom.
                delegateArea.right += 100;
                delegateArea.bottom += 100;
            
                // Instantiate a TouchDelegate.
                // "delegateArea" is the bounds in local coordinates of 
                // the containing view to be mapped to the delegate view.
                // "myButton" is the child view that should receive motion
                // events.
                TouchDelegate touchDelegate = new TouchDelegate(delegateArea, 
                        myButton);
     
                // Sets the TouchDelegate on the parent view, such that touches 
                // within the touch delegate bounds are routed to the child.
                if (View.class.isInstance(myButton.getParent())) {
                    ((View) myButton.getParent()).setTouchDelegate(touchDelegate);
                }
            }
        });
    }
}