Android中TouchDelegate的用法淺析

來源:互聯網
上載者:User

一般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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.