`
guozhenqian
  • 浏览: 151012 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Android增大button响应区域

阅读更多

转载:

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"?>
<FrameLayout android:id="@+id/FrameContainer"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
            <ImageButton android:id="@+id/tutorial"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:background="@null" android:src="@drawable/tutorial" />
</FrameLayout>



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

 

 

 

 

 

分享到:
评论

相关推荐

    Android开发的各种demo

    通过分析不同的demo,初学者可以学习到如何使用LinearLayout、RelativeLayout、ConstraintLayout等布局管理器,以及如何添加和定制控件如Button、TextView、EditText等。 3. **Activity与Intent**:Activity是...

    Android实现音量调节的方法

    2. `Button`(@+id/btnUpper):用于增大音量。 3. `Button`(@+id/btnLower):用于减小音量。 这些组件的布局和属性设置使得用户可以通过点击按钮直观地控制音量。 接下来,我们需要创建一个Activity类,如`...

    Android源码——按钮点击WIN8 磁贴效果.rar

    - 注意按钮的触摸区域,确保在动画过程中仍能响应点击事件。 总的来说,这个压缩包提供的源码和资源可以帮助开发者理解如何在Android中实现Windows 8磁贴效果的按钮点击动画,通过学习和实践,开发者可以进一步...

    Android+UI+问题集注+第二期

    #### 一、Android文本增大时背景的改变 当在Android应用中遇到文本变化导致背景形状发生改变的问题时,通常是指文本尺寸变化后,原本设计为圆角矩形的背景变为直角矩形的情况。这种现象可能会破坏应用界面的一致性...

    Android编码规范

    10. **第三方库使用**:谨慎引入第三方库,避免增大应用体积。 11. **错误处理**:全局异常捕获,通过邮件通知服务端,便于问题定位。 12. **.9图处理**:充分利用.9图特性,优化图片拉伸效果。 13. **静态变量...

    安卓动画效果相关-浮动菜单按钮的展开与收缩动画。.rar

    标题提到的“浮动菜单按钮的展开与收缩动画”是指在Android应用中,一个常见的交互设计——浮动操作按钮(Floating Action Button, FAB)如何通过动画来展示其扩展和收起的过程。这种动画效果常见于许多应用,特别是...

    毕业设计,实现一个类似美颜相机(美图秀秀应用)的 Android app.zip

    2. **Android布局设计**:使用XML来创建用户界面,包括各种布局如线性布局、相对布局、网格布局等,以及控件如ImageView、Button、EditText等。此外,还会用到ConstraintLayout来实现更复杂的界面约束和响应式设计。...

    android研发规范

    - **第三方库引用**:谨慎选择和使用第三方库,避免增大应用包大小。 - **异常处理**:全局异常捕获,发送错误报告至服务端,便于问题排查。 - **图片处理**:注意图片的压缩和缓存策略,提高加载速度。 - **...

    Android_Tilt-MAME:因为触摸屏控件很烂-开源

    “返回”是“插入硬币”和“游戏开始”菜单的默认按钮,“音量增大”和“向下”是Button1和Button2 Lousy触摸屏的默认映射,仅此而已。 也许有些出乎意料的是,倾斜传感器变得更加准确和响应Swift,这使得它在某些...

    Material风格的登陆注册页面MaterialLogin.zip

    5. **响应式设计**:为了适应不同屏幕尺寸,MaterialLogin应该会实现响应式布局,确保在手机和平板等设备上都能良好显示。 6. **触摸反馈**:遵循Material Design,项目中的按钮和其他可点击元素应提供触觉反馈,如...

    uni-app入门

    uni-app 是一款基于 Vue.js 的跨平台开发框架,它允许开发者编写一次代码,即可将应用程序发布到iOS、Android、H5以及微信、支付宝、百度、头条、QQ、钉钉等各类小程序平台上。这极大地提高了开发效率,减少了多平台...

Global site tag (gtag.js) - Google Analytics