Android 實現View滑動功能

2021-09-26 18:57:04 字數 1269 閱讀 9760

1.固定區域內滑動

實現view滑動的方式

layout()

offsetleftandright()和offsettopandbottom()

layoutparams

scrollby()和 scrollto()

setx()和sety()

直接上**,使用setx和sety。

public class scrollviewframelayout extends framelayout 

public scrollviewframelayout(@nonnull context context, @nullable attributeset attrs)

public scrollviewframelayout(@nonnull context context, @nullable attributeset attrs, int defstyleattr)

private void init()

@override

public boolean ontouchevent(motionevent event)

return true;

}private void updateviewposition(motionevent event)

// 限制不可超出螢幕寬度(右)

if (desx + getwidth() >= mscreenwidth)

setx(desx);

// 限制不可超出螢幕高度

float desy = moriginaly + event.getrawy() - moriginalrawy;

if (desy <= screenutils.dptopx(margin_edge_v))

if (desy > mscreenheight - getheight() - screenutils.dptopx(margin_edge_v))

sety(desy);

}/**

* 得到螢幕寬度

** @return

*/private int getscreenwidth()

/*** 得到螢幕高度

** @return

*/private int getscreenheight()

}

ps:待續。

Scoller實現view的滑動

scoller實際上只是記錄了view要滑動到的位置 之後還是呼叫view的scollto或scollby方法進行實際上的view滾動mscroller new scroller context public void startmoveanim int starty,int dy,int dura...

Android自定義彈性滑動View

1.記錄一下自己自定義view 2.直接子view只能是乙個 public class slideview extends viewgroup public slideview context context,nullable attributeset attrs public slideview ...

View的滑動衝突

外部滑動方向和內部滑動方向一致 上面兩種情況的巢狀 父容器決定事件是否攔截 偽 public boolean onintercepthoverevent motionevent event else break case motionevent.action up 必須返回false,因為actio...