在android开发中,经常会遇到一个view需要它能够支持滑动的需求。下面通过本篇文章给大家介绍android view移动的六种方法。
layout()
如果你将滑动后的目标位置的座标传递给layout(),这样子就会把view的位置给重新布置了一下,在视觉上就是view的一个滑动的效果。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
public class
DragView extends View{
private int
lastX;
private int
lastY;
public DragView(Context
context, AttributeSet attrs) {
super (context,
attrs);
}
public boolean
onTouchEvent(MotionEvent event) {
int x
= ( int )
event.getX();
int y
= ( int )
event.getY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
lastX
= x;
lastY
= y;
break ;
case MotionEvent.ACTION_MOVE:
int offX
= x - lastX;
int offY
= y - lastY;
layout(getLeft()+offX,
getTop()+offY,
getRight()+offX
, getBottom()+offY);
break ;
}
return true ;
}
}
|
offsetLeftAndRight() offsetTopAndBottom()
其实这两个方法分别是对左右移动和上下移动的封装,传入的就是偏移量。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public boolean
onTouchEvent(MotionEvent event) {
int x
= ( int )
event.getX();
int y
= ( int )
event.getY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
lastX
= x;
lastY
= y;
break ;
case MotionEvent.ACTION_MOVE:
int offX
= x - lastX;
int offY
= y - lastY;
offsetLeftAndRight(offX);
offsetTopAndBottom(offY);
break ;
}
return true ;
}
|
LayoutParams
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public boolean
onTouchEvent(MotionEvent event) {
int x
= ( int )
event.getX();
int y
= ( int )
event.getY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
lastX
= x;
lastY
= y;
break ;
case MotionEvent.ACTION_MOVE:
int offX
= x - lastX;
int offY
= y - lastY;
ViewGroup.MarginLayoutParams
mlp =
(MarginLayoutParams)
getLayoutParams();
mlp.leftMargin
= getLeft()+offX;
mlp.topMargin
= getTop()+offY;
setLayoutParams(mlp);
break ;
}
return true ;
}
|
scrollTo() scrollBy()
sceollTo(x,y)传入的应该是移动的终点座标
scrollBy(dx,dy)传入的是移动的增量。
通过scrollBy传入的值应该是你需要的那个增量的相反数!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public boolean
onTouchEvent(MotionEvent event) {
int x
= ( int )
event.getX();
int y
= ( int )
event.getY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
lastX
= x;
lastY
= y;
break ;
case MotionEvent.ACTION_MOVE:
int offX
= x - lastX;
int offY
= y - lastY;
((View)
getParent()).scrollBy(-offX,- offY);
break ;
}
return true ;
}
|
Scroller
步骤一:
初始化Scroller对象,即mScroller = new Scroller(context)
步骤二:
重写computeScroll()方法,实现模拟滑动。可以复制下面的末模板代码:
1
2
3
4
5
6
7
|
public void
computeScroll() {
super .computeScroll();
if (mScroller.computeScrollOffset()){
((View)getParent()).scrollTo(mScroller.getCurrX(),mScroller.getCurrY());
}
invalidate();
}
|
步骤三:
开启模拟过程,在合适的地方(一般都在move中)startScroll方法。它有两个重载方法如下:
1
2
|
startScroll( int startX, int startY,
int dx, int dy, int duration)
startScroll( int startX, int startY, int dx, int dy)
|
需要说明的是:
1.computeScrollOffset方法用来判断是否完成了整个滑动,返回为true,则说明没有完成,否则则完成滑动。
2.getCurrY()以及getCurrX()获得的是当前的滑动座标。
3.最后必须要用invalidate方法来刷新。因为computeScroll方法不会自动调用,是在draw方法中被调用的。所以必须使用invalidate刷新,就会调用draw方法,自然就会调用computeScroll方法了。这样子就会实现循环调用。
4.在startScroll中,偏移量跟使用scrollBy方法中的偏移量用法是一样的,即也必须填写你实际想要移动距离的相反数。也就是你实际想让它偏移一个正值,这里就填写它相应的负值,如果想偏移一个负值,这里就填写相应的正值!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
public class
DragView extends View{
private int
lastX;
private int
lastY;
private Scroller
mScroller;
public DragView(Context
context, AttributeSet attrs) {
super (context,
attrs);
mScroller
= new Scroller(context);
}
public boolean
onTouchEvent(MotionEvent event) {
int x
= ( int )
event.getX();
int y
= ( int )
event.getY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
lastX
= x;
lastY
= y;
break ;
case MotionEvent.ACTION_MOVE:
int offX
= x - lastX;
int offY
= y - lastY;
View
viewGroup = (View) getParent();
((View)
getParent()).scrollBy(-offX,- offY);
break ;
case MotionEvent.ACTION_UP:
View
viewGroup = (View) getParent();
mScroller.startScroll(viewGroup.getScrollX(),
viewGroup.getScrollY(),
-viewGroup.getScrollX()
,-viewGroup.getScrollY());
break ;
}
return true ;
}
public void
computeScroll() {
super .computeScroll();
if (mScroller.computeScrollOffset())
{
((View)getParent()).scrollTo(mScroller.getCurrX(),
mScroller.getCurrY());
}
invalidate();
}
}
|