android学习之-----使用TouchDelegate增大触摸面积
2016-10-13 15:06
441 查看
需求:
有时候由于界面上图标过小 ,导致点击触摸的区域太小不好操作,或者有时候会发生误操作。解决办法:
如果是图片设置src属性,设置padding ,或者外层嵌套一层布局 通过外层布局的点击事件来触发响应操作。总的来说主要还是需要增大可触摸的区域。
TouchDelegate
官方解释:https://developer.android.com/reference/android/view/TouchDelegate.html
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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.mytestapp.MainActivity" > <LinearLayout android:id="@+id/llayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/img" android:layout_width="10dp" android:layout_height="10dp" android:src="@drawable/ic_launcher" /> </LinearLayout> </RelativeLayout>
可以看见上面我们只设置了 图片的大小为10dp 应该是很小吧。
java代码设置
public class MainActivity extends Activity { private LinearLayout llayout; ImageView img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); img = (ImageView) findViewById(R.id.img); llayout = (LinearLayout) findViewById(R.id.llayout); llayout.post(new Runnable() { @Override public void run() { llayout.setTouchDelegate(new TouchDelegate( new Rect(0, 0, llayout.getMeasuredWidth()/2, llayout.getMeasuredHeight()), img)); } }); img.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "1111", 0).show(); } }); } }
注意事项:外层需要有一个容器承载需要扩展的那个View 用外层的容器去调用setTouchDelegate方法 为子View扩展可以点击的范围大小。
相关文章推荐
- 使用android.view.TouchDelegate扩大View的触摸点击区域
- 使用TouchDelegate增大控件的触摸范围
- Android学习之 扩大控件点击触摸区域及TouchDelegate的介绍
- 使用android.view.TouchDelegate扩大View的触摸点击区域
- Android使用TouchDelegate增加View的触摸范围
- 使用android.view.TouchDelegate扩大View的触摸点击区域
- Android使用TouchDelegate增加View的触摸范围
- 使用android.view.TouchDelegate扩大View的触摸点击区域
- Android使用TouchDelegate增加View的触摸范围
- Android事件分发08——TouchDelegate的使用的一个示例与问题分析
- Android事件分发07——TouchDelegate的使用与解析
- (OnTouchListener) android中的触摸事件的使用(绘图)
- 第17天 Android Touch事件学习 4 获取手指触摸位置
- Android 多点触摸 multi-touch
- APIDemo学习笔记——Android上几种简单的Animation使用方法(二)
- Android学习之七:使用Container
- android学习小结5-各种控件使用方式DEMO
- APIDemo学习笔记——Android上几种简单的Animation使用方法(三)——3D Transition
- android学习进行时----1.1.2UI部分Button使用-事件监听
- Mars Android视频学习笔记——01_16_SQLite使用方法