扩大View的点击范围
2014-04-01 19:51
447 查看
扩大View的点击范围本人知道的有两种方法,在不影响界面效果的前提下:
1、在View的外面添加一个透明容器
2、就是本文要说的,代码如下:
参考文章:http://developer.android.com/training/gestures/viewgroup.html
Androidprovidesthe
Inthefollowingexample,an
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
Thesnippetbelowdoesthefollowing:
Getstheparentviewandpostsa
Findsthe
Extendstheboundsofthe
Instantiatesa
Setsthe
Initscapacityastouchdelegateforthe
1、在View的外面添加一个透明容器
2、就是本文要说的,代码如下:
publicvoidaddToParentArea(finalViewview){ DisplayMetricsmetric=newDisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metric); finalfloatdensity=metric.density; finalViewparent=(View)view.getParent(); parent.post(newRunnable(){ publicvoidrun(){ //View的点击范围向四周扩大30个单位 finalRectr=newRect(); view.getHitRect(r); r.right+=30*density; r.left+=30*density; r.bottom+=30*density; r.top+=30*density; parent.setTouchDelegate(newTouchDelegate(r,view)); } }); }
参考文章:
Androidprovidesthe
classtomakeitpossibleforaparenttoextendthetouchableareaofachildviewbeyondthechild'sbounds.Thisisusefulwhenthechildhastobesmall,butshouldhavealargertouchregion.Youcanalsousethisapproachtoshrinkthechild'stouchregionifneedbe.TouchDelegate
Inthefollowingexample,an
isthe"delegateview"(thatis,thechildwhosetouchareatheparentwillextend).Hereisthelayoutfile:ImageButton
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageButtonandroid:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:src="@drawable/icon"/>
</RelativeLayout>
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
Thesnippetbelowdoesthefollowing:
Getstheparentviewandpostsa
ontheUIthread.ThisensuresthattheparentlaysoutitschildrenbeforecallingtheRunnable
method.ThegetHitRect()
methodgetsthechild'shitrectangle(touchablearea)intheparent'scoordinates.getHitRect()
Findsthe
childviewandcallsImageButton
togettheboundsofthechild'stouchablearea.getHitRect()
Extendstheboundsofthe
'shitrectangle.ImageButton
Instantiatesa
,passingintheexpandedhitrectangleandtheTouchDelegate
childviewasparameters.ImageButton
Setsthe
ontheparentview,suchthattoucheswithinthetouchdelegateboundsareroutedtothechild.TouchDelegate
Initscapacityastouchdelegateforthe
childview,theparentviewwillreceivealltouchevents.Ifthetoucheventoccurredwithinthechild'shitrectangle,theparentwillpassthetoucheventtothechildforhandling.ImageButton
publicclassMainActivityextendsActivity{
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Gettheparentview
ViewparentView=findViewById(R.id.parent_layout);
parentView.post(newRunnable(){
//Postintheparent'smessagequeuetomakesuretheparent
//laysoutitschildrenbeforeyoucallgetHitRect()
@Override
publicvoidrun(){
//Theboundsforthedelegateview(anImageButton
//inthisexample)
RectdelegateArea=newRect();
ImageButtonmyButton=(ImageButton)findViewById(R.id.button);
myButton.setEnabled(true);
myButton.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewview){
Toast.makeText(MainActivity.this,
"TouchoccurredwithinImageButtontouchregion.",
Toast.LENGTH_SHORT).show();
}
});
//ThehitrectanglefortheImageButton
myButton.getHitRect(delegateArea);
//ExtendthetouchareaoftheImageButtonbeyonditsbounds
//ontherightandbottom.
delegateArea.right+=100;
delegateArea.bottom+=100;
//InstantiateaTouchDelegate.
//"delegateArea"istheboundsinlocalcoordinatesof
//thecontainingviewtobemappedtothedelegateview.
//"myButton"isthechildviewthatshouldreceivemotion
//events.
TouchDelegatetouchDelegate=newTouchDelegate(delegateArea,
myButton);
//SetstheTouchDelegateontheparentview,suchthattouches
//withinthetouchdelegateboundsareroutedtothechild.
if(View.class.isInstance(myButton.getParent())){
((View)myButton.getParent()).setTouchDelegate(touchDelegate);
}
}
});
}
}
相关文章推荐
- Android 扩大view点击范围
- Android 扩大view点击范围
- Android 扩大view点击范围
- Android 扩大view点击范围
- 一种扩大View点击范围的方法
- Android view扩大点击范围
- Android 完美扩大ImageView的点击范围方案
- Android View分区域点击实现方案——1.根据坐标范围
- iOS runtime扩大UIButton点击范围-b
- Android 扩大ImageView的点击区域
- 超链接点击范围扩大到外层包含元素
- ImageView 加大点击范围
- Android 扩大 ImageView 点击区域
- 扩大Button的点击范围,setImage与setBackgroundImage区别
- 使用android.view.TouchDelegate扩大View的触摸点击区域
- UIButton超出父view的显示范围后,不响应点击事件
- 不改变button大小,扩大点击范围
- 扩大Button点击范围
- iOS runtime扩大UIButton点击范围
- 扩大UIButton的点击范围