import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.Scroller;
public class SlideLinearLayout extends LinearLayout {
private boolean ishow = false;
private Scroller mScroller = null;
private int distance;
public SlideLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
mScroller = new Scroller(context);
// TODO Auto-generated constructor stub
}
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
if (ishow) {
scrollTo(mScroller.getCurrX(), 0);
} else {
scrollTo(mScroller.getCurrX() - distance, 0);
}
postInvalidate();
}
}
public void beginScroll(Boolean isShow, int odistance,int duration) {
distance=odistance;
if (!ishow) {
mScroller.startScroll(0, 0, -distance, 0, duration);
ishow = true;
} else {
mScroller.startScroll(0, 0, distance, 0, duration);
ishow = false;
}
invalidate();
}
public boolean Ishow() {
return ishow;
}
}
代碼依據:
一,屏幕的左上角是座標系統原點(0,0)
原點向右延伸是X軸正方向,原點向下延伸是Y軸正方向1,MotionEvent類中:
event.getRowX():觸摸點相對於屏幕原點的X座標
event.getX(): 觸摸點相對於其所在組件原點的X座標
event.getRowY():觸摸點相對於屏幕原點的Y座標
event.getY(): 觸摸點相對於其所在組件原點的Y座標
2,Scroller類中:
getScrollY():相對於“座標系統原點”(見上圖)在Y軸上的偏移量
getScrollX():相對於“座標系統原點”(見上圖)在X軸上的偏移量
3,View類中:
getScrollY():相對於“座標系統原點”(見上圖)在Y軸上的偏移量
getScrollX():相對於“座標系統原點”(見上圖)在X軸上的偏移量
在computeScroll()中獲取滾動情況,做出滾動的響應
computeScroll在父控件執行drawChild時調用
提供的起始點和將要滑動的距離開始滾動。滾動會使用缺省值250ms作爲持續時間。
參數
startX 水平方向滾動的偏移值,以像素爲單位。正值表明滾動將向左滾動
startY 垂直方向滾動的偏移值,以像素爲單位。正值表明滾動將向上滾動
dx 水平方向滑動的距離,正值會使滾動向左滾動
dy 垂直方向滑動的距離,正值會使滾動向上滾動