如何使用多个TouchDelegate

Jun*_*aid 10 layout android view imagebutton

我有两个ImageButtons,每个都在RelativeLayout中,这两个RelativeLayout在另一个RelativeLayout中,我想为每个ImageButton设置TouchDelegate.如果通常我将TouchDelegate添加到每个ImageButton并且它的父RelativeLayout然后只有一个ImageButton正常工作,另一个不扩展它的点击区域.因此,PLease帮助我了解如何在两个ImageButtons中使用TouchDelegate.如果不可能,那么扩展视图点击区域的有效方法是什么?提前致谢 ........

这是我的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/FrameContainer"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout android:layout_alignParentLeft="true"
    android:id="@+id/relativeLayout3" android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <RelativeLayout android:layout_alignParentLeft="true"
        android:id="@+id/relativeLayout1" android:layout_width="113dip"
        android:layout_height="25dip">
        <ImageButton android:id="@+id/tutorial1"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:background="@null" android:src="@drawable/tutorial" />
    </RelativeLayout>
    <RelativeLayout android:layout_alignParentLeft="true"
        android:id="@+id/relativeLayout2" android:layout_width="113dip"
        android:layout_height="25dip" android:layout_marginLeft="100dip">
        <ImageButton android:id="@+id/tutorial2"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:background="@null" android:src="@drawable/tutorial"
            android:layout_marginLeft="50dip" />
    </RelativeLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我的Activity类:

import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.TouchDelegate;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

public class TestTouchDelegate extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    View mParent1 = findViewById(R.id.relativeLayout1);
    mParent1.post(new Runnable() {
        @Override
        public void run() {
            Rect bounds1 = new Rect();
            ImageButton mTutorialButton1 = (ImageButton) findViewById(R.id.tutorial1);
            mTutorialButton1.setEnabled(true);
            mTutorialButton1.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    Toast.makeText(TestTouchDelegate.this, "Test TouchDelegate 1", Toast.LENGTH_SHORT).show();
                }
            });

            mTutorialButton1.getHitRect(bounds1);
            bounds1.right += 50;
            TouchDelegate touchDelegate1 = new TouchDelegate(bounds1, mTutorialButton1);

            if (View.class.isInstance(mTutorialButton1.getParent())) {
                ((View) mTutorialButton1.getParent()).setTouchDelegate(touchDelegate1);
            }
        }
    });

    //View mParent = findViewById(R.id.FrameContainer);
    View mParent2 = findViewById(R.id.relativeLayout2);
    mParent2.post(new Runnable() {
        @Override
        public void run() {
            Rect bounds2 = new Rect();
            ImageButton mTutorialButton2 = (ImageButton) findViewById(R.id.tutorial2);
            mTutorialButton2.setEnabled(true);
            mTutorialButton2.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    Toast.makeText(TestTouchDelegate.this, "Test TouchDelegate 2", Toast.LENGTH_SHORT).show();
                }
            });

            mTutorialButton2.getHitRect(bounds2);
            bounds2.left += 50;
            TouchDelegate touchDelegate2 = new TouchDelegate(bounds2, mTutorialButton2);

            if (View.class.isInstance(mTutorialButton2.getParent())) {
                ((View) mTutorialButton2.getParent()).setTouchDelegate(touchDelegate2);
            }
        }
    });

}
Run Code Online (Sandbox Code Playgroud)

}

nee*_*ars 21

您可以使用复合模式可以添加多个TouchDelegateView.脚步:

  1. 创建TouchDelegateComposite(无论您将作为参数传递给哪个视图,它仅用于获取上下文)
  2. 创建必要的TouchDelegates并将它们添加到复合
  3. 添加复合以查看他们建议在这里(通过view.post(new Runnable))

    public class TouchDelegateComposite extends TouchDelegate {
    
        private final List<TouchDelegate> delegates = new ArrayList<TouchDelegate>();
        private static final Rect emptyRect = new Rect();
    
        public TouchDelegateComposite(View view) {
            super(emptyRect, view);
        }
    
        public void addDelegate(TouchDelegate delegate) {
            if (delegate != null) {
                delegates.add(delegate);
            }
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            boolean res = false;
            float x = event.getX();
            float y = event.getY();
            for (TouchDelegate delegate : delegates) {
                event.setLocation(x, y);
                res = delegate.onTouchEvent(event) || res;
            }
            return res;
        }
    
    }
    
    Run Code Online (Sandbox Code Playgroud)

  • 我改编了这个解决方案,用同一个父版扩展了可点击的视图区域:[gist](https://gist.github.com/nikosk/3854f2effe65438cfa40)@AndroidNoob:包含一个易于使用的静态方法.像这样使用:TouchDelegateComposite.expandClickArea(buttonView,parentView,clickListener,paddingInDP); (2认同)

Bre*_*ein 5

每个视图应该只有一个触摸委托。View 类中 getTouchDelegate() 的文档如下:

“获取此视图的 TouchDelegate。”

只能有一个 TouchDelegate。要每个视图仅使用一个 TouchDelegate,您可以将每个可触摸视图包装在一个视图中,其尺寸反映您想要可触摸的内容。square 的一位 Android 开发人员给出了一个示例,说明如何仅使用一个静态方法对多个视图执行此操作 (http://www.youtube.com/watch?v=jF6Ad4GYjRU&t=37m4s):

  public static void expandTouchArea(final View bigView, final View smallView, final int extraPadding) {
bigView.post(new Runnable() {
    @Override
    public void run() {
        Rect rect = new Rect();
        smallView.getHitRect(rect);
        rect.top -= extraPadding;
        rect.left -= extraPadding;
        rect.right += extraPadding;
        rect.bottom += extraPadding;
        bigView.setTouchDelegate(new TouchDelegate(rect, smallView));
    }
});
Run Code Online (Sandbox Code Playgroud)

}

假设您不想让视图层次结构变得混乱。我还能想到另外两个选择。您可以定义可触摸视图内可触摸内容的边界,并确保将所有触摸事件从相应的父视图传递到该子视图。或者您可以覆盖可触摸视图的 getHitRect() 。前者会很快使您的代码变得混乱并使其难以理解,因此后者是更好的前进方向。您想要覆盖 getHitRect。

其中 mPadding 是您希望在视图周围可触摸的额外区域的数量,您可以使用如下所示的内容:

    @Override
public void getHitRect(Rect outRect) {
    outRect.set(getLeft() - mPadding, getTop() - mPadding, getRight() + mPadding, getTop() + mPadding);
}
Run Code Online (Sandbox Code Playgroud)

如果您使用上面这样的代码,您将必须考虑附近有哪些可触摸的视图。堆栈最高的视图的可触摸区域可能会重叠在另一个视图的顶部。

另一个类似的选项是仅更改可触摸视图的填充。我不喜欢将此作为解决方案,因为跟踪视图大小的调整方式可能会变得困难。

  • 事实证明我提供的解决方案在 ICS 中不起作用,这可能就是它不适合您的原因。我写了一篇关于两种不同的触摸代理策略的博客文章http://www.brendanweinstein.me/2012/06/26/touchdelegate-and-gethitrect-making-buttons-with-expanded-touch-areas/,您可以获取示例两种策略的代码位于 https://github.com/brendanw/Touch-Delegates/blob/master/src/com/brendan/touchdelegates/MyTouchDelegate.java (2认同)