转载:
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 对 Button 的三种监听 Android 对 Button 的三种监听 Android 对 Button 的三种监听 https://blog.csdn.net/nicepainkiller/article/details/85038886
android 自定义各种风格button
本文用两种方法BaseAdapter 和SimpleAdapter分别实现三种情况。listview中有button事件的时候,针对不同的需求,响应不同的事件。只实现响应listview的点击事件。只响应listview中的button事件。两个事件都需要。
本实例是初学Android入门Button实例,多个例子,归纳总结,新手必学,举一反三的例子.
使用新的方法实现Button效果,如上图一样,有时候我们需要一个Button,前面有一个小型的图标,有两种办法,可以自定义一个Button,其实,我们不必纠结于Button这个词,只要能获得点击事件,做出Button一样的效果就可以
本文实例讲述了Android自定义Button并设置不同背景图片的方法。分享给大家供大家参考,具体如下: 1、自定义MyButton类 public class MyButton extends Button { //This constructormust be public MyButton...
Android 点击button呈现水波状
android studio 自定义button边框,角度,渐变颜色按下时改变样式, 以及基础动画,为image view添加旋转,下移,渐变等样式,使用timer handler button进行控制。
android 自定义Button效果
最近在做android项目时 发现要在listView用到Button等各种点击事件 经过各种百度 发现有两个简单的方法实现 我把demo放上去 为了方便自己以后用到 也方便学习listview实现button监听的同学们 注意 我用最新sdk...
Android Button事件的实现,初学android的好帮手
android Button常用监听事件的几种类型
Android Button按钮及TextView实例介绍的源程序,一是介绍Android手机中button的用法,二是TextVeiw的介绍。并附上了源代码,是一份很不错的参考。
这是android_button的使用,为android基础篇
Android自定义Button的通用方法
Android 拖动 Button 显示效果,在Android测试机上是按住鼠标拖动按钮,和其它的拖动操作方法一样,不过在手机上运行的时候,用手触摸屏幕按钮住屏幕移动,就可移动按钮,代码注释英文,来自国外Android网站的源码。
Progress Button Android Android Button that morphs into a loading progress bar. Fully customizable in the XML Really simple to use. Makes your app looks cooler =D You can check how this library was...
Android 4.x SelectButtons 之使用範例 當 CheckBox、RadioButton 與 ToggleButton 等不同選項按鈕的選取狀態改變時,會取得該選項按鈕上面的文字,並以Toast訊息方塊呈現
Button设置背景很简单,只需简单的应用Background属性即可,而为其添加状态设计也是通过此。Android系统为开发者考虑了一套selector机制,可以根据控件的状态(点击、非点击、焦点)自动切换。下面用一个简单的例子来...
Android带动态效果的Button(按钮)