一般TouchDelegate是在View類中的。
View類中出現TouchDelegate的地方如下:
public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource {// ... /** * The delegate to handle touch events that are physically in this view * but should be handled by another view. */ private TouchDelegate mTouchDelegate = null;// ... /** * Sets the TouchDelegate for this View. */ public void setTouchDelegate(TouchDelegate delegate) { mTouchDelegate = delegate; } /** * Gets the TouchDelegate for this View. */ public TouchDelegate getTouchDelegate() { return mTouchDelegate; }// ... public boolean onTouchEvent(MotionEvent event) {// ... if (mTouchDelegate != null) { if (mTouchDelegate.onTouchEvent(event)) { return true; } }// ...}// ...}
先放在這裡,下面我們看看TouchDelegate給我們的提示:
/**
* Helper class to handle situations where you want a view to have a larger touch area than its
* actual view bounds. The view whose touch area is changed is called the delegate view. This
* class should be used by an ancestor of the delegate. To use a TouchDelegate, first create an
* instance that specifies the bounds that should be mapped to the delegate and the delegate
* view itself.
* <p>
* The ancestor should then forward all of its touch events received in its
* {@link android.view.View#onTouchEvent(MotionEvent)} to {@link #onTouchEvent(MotionEvent)}.
* </p>
*
靠,一大段E文,讓人頭大,不過很簡單。
我用一幅圖來表示上面的含義:
簡單點說,就是一般我們只能Click這個View2的時候,View2才響應,但是我們想要Click這個View1的Bounds內,這個View2也要響應。
就是這麼簡單。。。
所以我們使用時候有:
TouchDelegate td = new TouchDelegate(bounds, view2); view1.setTouchDelegate(td);
這樣就OK了。。。
So, easy~~
當然你還可以自己派生TouchDelegate類。。
這裡帖出代碼:
// TestActivity.javapublic class TestActivity extends Activity {private LinearLayout mView1, mViewBounds;private Button mView2;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.a_testactivity);mView1 = (LinearLayout) findViewById(R.id.view1);mViewBounds = (LinearLayout) findViewById(R.id.viewBounds);mView2 = (Button) findViewById(R.id.view2);int v1Top = mView1.getTop();int v1Bottom = mView1.getBottom();ViewGroup.LayoutParams lp1 = mView1.getLayoutParams();ViewGroup.LayoutParams lpBounds = mViewBounds.getLayoutParams();Rect bounds = new Rect();bounds.left = 0;bounds.top = (lpBounds.height - lp1.height)/2;bounds.right = bounds.left + lpBounds.width;bounds.bottom = bounds.top + lpBounds.height;int a=2;int b=a;TouchDelegate td = new TouchDelegate(bounds, mView2);mView1.setTouchDelegate(td);}}
// a_testactivity.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/view1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="300dp" android:layout_height="200dp" android:background="@color/white" android:orientation="horizontal" > <LinearLayout android:id="@+id/viewBounds" android:layout_width="200dp" android:layout_height="100dp" android:layout_gravity="center" android:background="@color/black" android:orientation="horizontal" > <hh.test.MyButton android:id="@+id/view2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="View2" /> </LinearLayout> </LinearLayout></LinearLayout>