Android实现滑动的几种方法
scrollTo,Scroller,LayoutParams,layout,offsetLeftAndRight2016-06-13
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.scu.lly.dragviewtest.view.DragView
android:layout_width="100dp"
android:layout_height="100dp" />
</LinearLayout>public class DragView extends View{
private int mLastX;
private int mLastY;
public DragView(Context context) {
super(context);
init();
}
public DragView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DragView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(){
setBackgroundColor(Color.BLUE);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
int x = (int) ev.getX();
int y = (int) ev.getY();
switch (ev.getAction()){
case MotionEvent.ACTION_DOWN:
mLastX = x;
mLastY = y;
break;
case MotionEvent.ACTION_MOVE:
int offsetX = x - mLastX;
int offsetY = y - mLastY;
//调整layout的四个坐标
layout(getLeft() + offsetX, getTop() + offsetY, getRight() + offsetX, getBottom() + offsetY);
break;
}
return true;
}
}<span style="white-space:pre"> </span>case MotionEvent.ACTION_MOVE:
int offsetX = x - mLastX;
int offsetY = y - mLastY;
//调整layout的四个坐标
//layout(getLeft() + offsetX, getTop() + offsetY, getRight() + offsetX, getBottom() + offsetY);
//使用简写形式
offsetLeftAndRight(offsetX);
offsetTopAndBottom(offsetY);
break;case MotionEvent.ACTION_MOVE:
int offsetX = x - mLastX;
int offsetY = y - mLastY;
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
lp.leftMargin = getLeft() + offsetX;
lp.topMargin = getTop() + offsetY;
setLayoutParams(lp);
break;case MotionEvent.ACTION_MOVE:
int offsetX = x - mLastX;
int offsetY = y - mLastY;
//LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) getLayoutParams();
lp.leftMargin = getLeft() + offsetX;
lp.topMargin = getTop() + offsetY;
setLayoutParams(lp);
break;case MotionEvent.ACTION_MOVE:
//int offsetX = x - mLastX;
//int offsetY = y - mLastY;
//此时,计算坐标是相反的
int offsetX = mLastX - x;
int offsetY = mLastY - y;
//让View所在的ViewGroup进行移动
((View)getParent()).scrollBy(offsetX,offsetY);
break;public class DragView extends View{
private int mLastX;
private int mLastY;
private Scroller mScroller;
public DragView(Context context) {
super(context);
init(context);
}
public DragView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public DragView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context){
setBackgroundColor(Color.BLUE);
mScroller = new Scroller(context);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
int x = (int) ev.getX();
int y = (int) ev.getY();
switch (ev.getAction()){
case MotionEvent.ACTION_DOWN:
mLastX = x;
mLastY = y;
break;
case MotionEvent.ACTION_MOVE:
//int offsetX = x - mLastX;
//int offsetY = y - mLastY;
//此时,计算坐标是相反的
int offsetX = mLastX - x;
int offsetY = mLastY - y;
//让View所在的ViewGroup进行移动
((View)getParent()).scrollBy(offsetX,offsetY);
break;
case MotionEvent.ACTION_UP:
View viewGroup = (View) getParent();
mScroller.startScroll(viewGroup.getScrollX(),viewGroup.getScrollY(),-viewGroup.getScrollX(),-viewGroup.getScrollY());
//记住需要invalidate
invalidate();
break;
}
return true;
}
@Override
public void computeScroll() {
super.computeScroll();
if(mScroller.computeScrollOffset()){
((View)getParent()).scrollTo(mScroller.getCurrX(),mScroller.getCurrY());
//记住,需要不断调用invalidate进行重绘
invalidate();
}
}
}