Android TouchDelegate

原文 : http://blog.csdn.net/a220315410/article/details/9141265


TouchDelegate是個挺有意思的小玩意,它可以幫助我們讓某個控件處理比它實際佔用空間更大的觸摸消息。


  之前,我曾經看到過一個app,上面有一個小圖標共用戶拖動操作,因爲圖標太小,經常點不到,當時我想到的處理方案是把圖標改大,這樣多少會導致UI發生變化。現在藉助TouchDelegate我可以更方便的修正這個bug,並且無需改動UI。

   使用TouchDelegate的方法是

   1. 構造TouchDelegate實例delegate,參數爲需要修改作用範圍的控件view1和增大後的rect。

   2. 在view1的祖先控件view2上設定delegate。

   需要注意的是,如果touch事件被view2或者view2的某個子控件消耗掉了,那麼delegate就無法起效了。原因的話,描述起來篇幅過長,請自行參考Android的消息分派機制(google可以找到很多資料)。

   使用demo如下:

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:id="@+id/parent">
    <LinearLayout
        android:id="@+id/child"
        android:layout_height="200dp"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:background="#00ff00"></LinearLayout>
</RelativeLayout>


MainActivity.java:


package com.example.touchdelegatepro;
import android.graphics.Rect;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.TouchDelegate;
import android.view.View;
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final View child = findViewById(R.id.child);
        final View parent = findViewById(R.id.parent);
        parent.post(new Runnable() {
            @Override
            public void run() {
                Rect rc = new Rect();
                child.getHitRect(rc); //如果直接在oncreate函數中執行本函數,會獲取rect失敗,因爲此時UI界面尚未開始繪製,無法獲得正確的座標
                rc.bottom += 150;
                parent.setTouchDelegate(new TouchDelegate(rc, child));
            }
        });
        child.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Log.i("ray", String.format("action=%d, x:%f,y=%f", motionEvent.getAction(), motionEvent.getX(), motionEvent.getY()));
                if(motionEvent.getAction()== MotionEvent.ACTION_DOWN){
                    return true;
                }else{
                    return false;
                }
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
        
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章