阅读更多

转载:

Android: TouchDelegate tutorialI’ve been doing quite a lot of Android development lately. Most of the time the API Specificationand Dev Guide are quite complete, but sometimes information is difficult to find. An example of this is the TouchDelegate class. According to the Specification TouchDelegate is a “Helper class to handle situations where you want a view to have a larger touch area than its actual view bounds.”. That’s exactly what it does and most application developers are likely to feel the need to use it.
Since I couldn’t find an example on how to actually use it, I wrote this small tutorial. It’s rather easy, but does expect you have some very basic Android knowledge (eg. how to build and use an Activity).
First up: the simplest layout file in history: (eg. tutorial.xml)

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
                            android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:background="@null" android:src="@drawable/tutorial" />




Put this file in res/layout/. Make sure you have a drawable (an image), called tutorial.png (or any other supported format) located in res/drawable/
The layout file only contains a FrameLayout with an ImageButton in it, which is all we need for this tutorial. I choose an ImageButton , but any View will do.
If you have questions regarding the layout file, visit the UI guide.
First thing to do, is to reference our layout file in the Activity, using

1 setContentView(R.layout.tutorial);



In order to have a larger touch area than the actual view bounds, we need to get a hold of the parent of our ImageButton: the FrameLayout  called FrameContainer. We post a small Runnable on the UI thread. In that Runnable we instantiate a Rect that specifies the bounds for our TouchDelegate. We then get the hit rectangle of the tutorialButton. The coordinates of the Rect are public, so we change them to what we want them to be. In the example I made the touch area of the tutorial ImageButton larger on the right side. After we have our Rect, we build a TouchDelegate with it and we set that TouchDelegate to the parent of our tutorialButton. I added a random message in onClick for testing purposes.
Code snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
View mParent = findViewById(R.id.FrameContainer);
mParent.post(new Runnable() {
    @Override
    public void run() {
        Rect bounds = new Rect();
        ImageButton mTutorialButton = (ImageButton) findViewById(R.id.tutorial);
        mTutorialButton.setEnabled(true);
        mTutorialButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Toast.makeText(TouchDelegateActivity.this, "Test TouchDelegate", Toast.LENGTH_SHORT).show();
            }
        });

        mTutorialButton.getHitRect(bounds);
        bounds.right += 50;
        TouchDelegate touchDelegate = new TouchDelegate(bounds, mTutorialButton);

        if (View.class.isInstance(mTutorialButton.getParent())) {
            ((View) mTutorialButton.getParent()).setTouchDelegate(touchDelegate);
        }
    }
});



That’s it. The actual bounds of your Rect might require some experimenting when used in an actual layout. Feel free to post remarks and questions.

 

 

 

 

 

 

I was recently asked about how to use a TouchDelegate. I was a bit 
rusty myself on this and I couldn't find any good documentation on it. 
Here's the code I wrote after a little trial and error. 
touch_delegate_view is a simple RelativeLayout with the id 
touch_delegate_root. I defined with a single, child of the layout, the 
button delegated_button. In this example I expand the clickable area 
of the button to 200 pixels above the top of my button. 

public class TouchDelegateSample extends Activity {

  Button mButton; 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.touch_delegate_view); 
    mButton = (Button)findViewById(R.id.delegated_button); 
    View parent = findViewById(R.id.touch_delegate_root);

    // post a runnable to the parent view's message queue so its run 
after 
    // the view is drawn 
    parent.post(new Runnable() { 
      @Override 
      public void run() { 
        Rect delegateArea = new Rect(); 
        Button delegate = TouchDelegateSample.this.mButton; 
        delegate.getHitRect(delegateArea); 
        delegateArea.top -= 200; 
        TouchDelegate expandedArea = new TouchDelegate(delegateArea, 
delegate); 
        // give the delegate to an ancestor of the view we're 
delegating the 
        // area to 
        if (View.class.isInstance(delegate.getParent())) { 
          ((View)delegate.getParent()).setTouchDelegate(expandedArea); 
        } 
      } 
    }); 
  }

 



Cheers, 
Justin 
Android Team @ Google

 

 

 

 

 

更多相关文章

  1. Android ListView列表 刷新和加载更多
  2. Jquery点击加载更多
  3. 《锋利的jQuery》读书笔记 第5章 jQuery对表单、表格的操作及更
  4. 关于jQuery Themeroller主题,我在哪里可以找到更多?
  5. DataTables警告:table id = DataTables_Table_0 - Ajax错误。有
  6. 总结一些更多的针对webkit的HTML, CSS和Javascript方面的特性.
  7. Joomla 3.x“错误检出失败,出现以下错误:”没有更多信息
  8. 如何使用jQuery将输入值插入更多填充的字段?
  9. 隐藏加载更多记录按钮ASP.net MVC Ajax Javascript

随机推荐

  1. Android(安卓)RecyclerView刷新和加载
  2. ContentProvider讲解与实例应用
  3. Android版本更新时对SQLite数据库升级或
  4. android -------- RecyclerView的可(多个I
  5. Android中的DatePickerDiaolog的使用
  6. aapt dump badging 找不到'android:versi
  7. Android全局变量的定义与使用
  8. 50个安卓开发者应该熟悉的Android(安卓)S
  9. linux下android模块开发部署
  10. [Android] [SystemUI] Recent -- 最近任