您的位置:首页 > 其它

扩大View的点击范围

2014-04-01 19:51 447 查看
扩大View的点击范围本人知道的有两种方法,在不影响界面效果的前提下:

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));
}
});
}

参考文章:http://developer.android.com/training/gestures/viewgroup.html

Androidprovidesthe
TouchDelegate
classtomakeitpossibleforaparenttoextendthetouchableareaofachildviewbeyondthechild'sbounds.Thisisusefulwhenthechildhastobesmall,butshouldhavealargertouchregion.Youcanalsousethisapproachtoshrinkthechild'stouchregionifneedbe.

Inthefollowingexample,an
ImageButton
isthe"delegateview"(thatis,thechildwhosetouchareatheparentwillextend).Hereisthelayoutfile:

<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
Runnable
ontheUIthread.Thisensuresthattheparentlaysoutitschildrenbeforecallingthe
getHitRect()
method.The
getHitRect()
methodgetsthechild'shitrectangle(touchablearea)intheparent'scoordinates.
Findsthe
ImageButton
childviewandcalls
getHitRect()
togettheboundsofthechild'stouchablearea.
Extendstheboundsofthe
ImageButton
'shitrectangle.
Instantiatesa
TouchDelegate
,passingintheexpandedhitrectangleandthe
ImageButton
childviewasparameters.
Setsthe
TouchDelegate
ontheparentview,suchthattoucheswithinthetouchdelegateboundsareroutedtothechild.
Initscapacityastouchdelegateforthe
ImageButton
childview,theparentviewwillreceivealltouchevents.Ifthetoucheventoccurredwithinthechild'shitrectangle,theparentwillpassthetoucheventtothechildforhandling.


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);
}
}
});
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: