网上的上滑太少了,今天来了上滑的实现,并给出代码
public class MyView extends LinearLayout {
private int screenHeight;
private int screenWidth;
WindowManager mWM;
MoveRelativeLayout mSlidingView;
private Context mContext;
private Scroller mScroller;
private VelocityTracker mVelocityTracker;
private int mTouchSlop;
private float mLastMotionX;
private float mLastMotionY;
private static final int VELOCITY = 50;
private boolean mIsBeingDragged = true;
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
mWM = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
screenWidth = mWM.getDefaultDisplay().getWidth();
screenHeight = mWM.getDefaultDisplay().getHeight();
// BounceInterpolator polator = new BounceInterpolator();
// mScroller = new Scroller(context, polator);
mScroller = new Scroller(context);
}
@Override
protected void onFinishInflate() {
// TODO Auto-generated method stub
super.onFinishInflate();
mSlidingView = (MoveRelativeLayout) findViewById(R.id.mylayout);
}
/*public boolean onTouchEvent(MotionEvent event) {
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
final int action = event.getAction();
final int x = (int) event.getX();
final int y = (int) event.getY();
Log.d("0107", "x = " + x);
Log.d("0107", "y = " + y);
int imageViewHeight = 0;
int deltaY = 0;
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.d("0107", "ACTION_DOWN");
mrl.closeScroll();
mLastX = x;
mLastY = y;
break;
case MotionEvent.ACTION_MOVE:
Log.d("0107", "ACTION_MOVE");
mVelocityTracker.addMovement(event);
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(50);
int velocityY = (int) velocityTracker.getYVelocity();
Log.d("0107", "ACTION_MOVE-----------------------velocityY = "
+ velocityY);
imageViewHeight = findViewById(R.id.iv).getHeight();
int scrolledY = findViewById(R.id.mylayout).getScrollY();
// velocityY < 0 shang hua
// velocityY > 0 xia hua
// if(velocityY < -SNAP_VELOCITY){
// if(scrolledY > 299){
// mrl.startScroll(0, mrl.getScrollY(),
// 0,findViewById(R.id.iv).getHeight() - mrl.getScrollY()-1 , 1000);
// return true;
// }else{
// mrl.startScroll(0, 0, 0,mrl.getScrollY() , 1000);
// }
//
// }
int deltaX = mLastX - x;
deltaY = mLastY - y; // > 0 up <0 down
totalMoveDistant += deltaY;
// 移动视图到制定位置
if (-velocityY < SNAP_VELOCITY) {
flg = false;
if (totalMoveDistant >= 299) {
Log.d("0107", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
findViewById(R.id.mylayout).scrollTo(0,
findViewById(R.id.iv).getHeight() - 1);
} else {
Log.d("0107", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
findViewById(R.id.mylayout).scrollBy(0, deltaY);
}
}else{
flg = true;
mrl.startScroll(0, mrl.getScrollY(), 0, findViewById(R.id.iv)
.getHeight() - mrl.getScrollY() - 1, 1000);
totalMoveDistant = 299;
}
mLastX = x;
mLastY = y;
break;
case MotionEvent.ACTION_UP:
Log.d("0107", "ACTION_UP");
Log.d("0107",
"ACTION_UP---findViewById(R.id.mylayout).getScrollY() = "
+ findViewById(R.id.mylayout).getScrollY());
Log.d("0107", "ACTION_UP---imageViewHeight/2 = "
+ (imageViewHeight / 2));
mVelocityTracker.addMovement(event);
final VelocityTracker velocityTracker2 = mVelocityTracker;
velocityTracker2.computeCurrentVelocity(50);
int velocityY2 = (int) velocityTracker2.getYVelocity();
Log.d("0107", "ACTION_UP-----------------------velocityY2 = "
+ velocityY2);
if(!flg){
flg = false;
if (findViewById(R.id.mylayout).getScrollY() > (findViewById(
R.id.iv).getHeight() / 2)) {
// findViewById(R.id.mylayout).scrollTo(0,
// findViewById(R.id.iv).getHeight()-1);
mrl.startScroll(0, mrl.getScrollY(), 0, findViewById(R.id.iv)
.getHeight() - mrl.getScrollY() - 1, 500);
totalMoveDistant = 299;
} else {
// findViewById(R.id.mylayout).scrollTo(0, 0);
mrl.startScroll(0, mrl.getScrollY(), 0, -mrl.getScrollY(), 1000);
totalMoveDistant = 0;
}
}
// if(deltaY > 0){
// if(deltaY > (imageViewHeight/2)){
// startBounceAnim(this.getScrollY(), imageViewHeight, 450);
// }else{
// startBounceAnim(this.getScrollY(), 0, 450);
// }
// }else{
// startBounceAnim(this.getScrollY(), 0, 450);
// }
// startBounceAnim(findViewById(R.id.mylayout).getScrollY(),
// imageViewHeight, 450);
break;
}
// 不再传递Touch事件
return true;
}*/
/*
* @Override protected void onLayout(boolean changed, int l, int t, int r,
* int b) { super.onLayout(changed, l, t, r, b);
*
* Log.e("0107", "onLayout"); for(int i = 0; i< getChildCount(); i++){ View
* child = getChildAt(i); if(child.getId() == R.id.iv){ Log.e("0107",
* "child.getId() == R.id.iv"); child.layout(0, phoneHeight-300, phoneWidth,
* phoneHeight); } } }
*/
// @Override
// protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//
// Log.e("0107", "onMeasure");
// }
// public void startBounceAnim(int startY, int dy, int duration) {
// mScroller.startScroll(0, startY, 0, dy, duration);
// invalidate();
// }
@Override
public void scrollTo(int x, int y) {
super.scrollTo(x, y);
postInvalidate();
}
@Override
public void computeScroll() {
if (!mScroller.isFinished()) {
if (mScroller.computeScrollOffset()) {
int oldX = mSlidingView.getScrollX();
int oldY = mSlidingView.getScrollY();
int x = mScroller.getCurrX();
int y = mScroller.getCurrY();
if (oldX != x || oldY != y) {
if (mSlidingView != null) {
mSlidingView.scrollTo(x, y);
}
}
invalidate();
}
}
}
private boolean canSlideLeft = false;
private boolean canSlideRight = true;
//
// public void setCanSliding(boolean left, boolean right) {
// canSlideLeft = left;
// canSlideRight = right;
// }
// 拦截touch事件
/* @Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
final float x = ev.getX();
final float y = ev.getY();
Log.d("0320", "x = " + x);
Log.d("0320", "y = " + y);
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.d("0320", "onInterceptTouchEvent---ACTION_DOWN");
mLastMotionX = x;
mLastMotionY = y;
Log.d("0320", "onInterceptTouchEvent---ACTION_DOWN---mLastMotionX = " + mLastMotionX);
Log.d("0320", "onInterceptTouchEvent---ACTION_DOWN---mLastMotionY = " + mLastMotionY);
mIsBeingDragged = false;
break;
case MotionEvent.ACTION_MOVE:
final float dx = x - mLastMotionX;
final float dy = y - mLastMotionY;
final float xDiff = Math.abs(dx);
final float yDiff = Math.abs(y - mLastMotionY);
Log.d("0320", "onInterceptTouchEvent---ACTION_MOVE---dx = " + dx);
Log.d("0320", "onInterceptTouchEvent---ACTION_MOVE---dy = " + dy);
Log.d("0320", "onInterceptTouchEvent---ACTION_MOVE---xDiff = " + xDiff);
Log.d("0320", "onInterceptTouchEvent---ACTION_MOVE---yDiff = " + yDiff);
if (yDiff > mTouchSlop && yDiff > xDiff) {
if (canSlideLeft) {
float oldScrollY = mSlidingView.getScrollY();
Log.d("0320", "onInterceptTouchEvent---ACTION_MOVE---aaa---oldScrollY = " + oldScrollY);
if (oldScrollY < 0) {
Log.d("0320", "onInterceptTouchEvent---ACTION_MOVE---aaa---oldScrollY < 0");
mIsBeingDragged = true;
mLastMotionY = y;
} else {
if (dy > 0) {
Log.d("0320", "onInterceptTouchEvent---ACTION_MOVE---aaa---dy > 0");
mIsBeingDragged = true;
mLastMotionY = y;
}
}
} else if (canSlideRight) {
float oldScrollY = mSlidingView.getScrollY();
Log.d("0320", "onInterceptTouchEvent---ACTION_MOVE---bbb---oldScrollY = " + oldScrollY);
if (oldScrollY > 0) {
Log.d("0320", "onInterceptTouchEvent---ACTION_MOVE---bbb---oldScrollY > 0");
mIsBeingDragged = true;
mLastMotionY = y;
} else {
if (dy < 0) {
Log.d("0320", "onInterceptTouchEvent---ACTION_MOVE---bbb---dy < 0");
mIsBeingDragged = true;
mLastMotionY = y;
}
}
}
}
break;
}
return mIsBeingDragged;
}*/
// 处理拦截后的touch事件
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(ev);
final int action = ev.getAction();
final float x = ev.getX();
final float y = ev.getY();
Log.d("0320", "onTouchEvent---x = " + x);
Log.d("0320", "onTouchEvent---y = " + y);
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.d("0320", "onTouchEvent---ACTION_DOWN");
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
}
mLastMotionX = x;
mLastMotionY = y;
if (mSlidingView.getScrollY() == -getMenuViewWidth()
&& mLastMotionY < getMenuViewWidth()) {
return false;
}
// if (mSlidingView.getScrollY() == getMenuViewWidth()
// && mLastMotionY > getMenuViewWidth()) {
// return false;
// }
break;
case MotionEvent.ACTION_MOVE:
Log.d("0320", "onTouchEvent---ACTION_MOVE");
if (mIsBeingDragged) {
final float deltaY = mLastMotionY - y;
Log.d("0320", "onTouchEvent---ACTION_MOVE---deltaY = " + deltaY);
mLastMotionY = y;
Log.d("0320", "onTouchEvent---ACTION_MOVE---mLastMotionY = " + mLastMotionY);
float oldScrollY = mSlidingView.getScrollY();
Log.d("0320", "onTouchEvent---ACTION_MOVE---oldScrollY = " + oldScrollY);
float scrollY = oldScrollY + deltaY;
Log.d("0320", "onTouchEvent---ACTION_MOVE---scrollY = " + scrollY);
if (deltaY < 0 && oldScrollY < 0) { // left view
Log.d("0320", "onTouchEvent---ACTION_MOVE---deltaY < 0 && oldScrollY < 0");
final float leftBound = 0;
final float rightBound = -getMenuViewWidth();
if (scrollY > leftBound) {
scrollY = leftBound;
Log.d("0320", "onTouchEvent---ACTION_MOVE---scrollY = leftBound is " + scrollY);
} else if (scrollY < rightBound) {
scrollY = rightBound;
Log.d("0320", "onTouchEvent---ACTION_MOVE---scrollY = rightBound is " + scrollY);
}
}
else if (deltaY > 0 && oldScrollY > 0) { // right view
final float rightBound = getMenuViewWidth();
final float leftBound = 0;
if (scrollY < leftBound) {
scrollY = leftBound;
} else if (scrollY > rightBound) {
scrollY = rightBound;
}
}
if (mSlidingView != null) {
Log.d("0320", "onTouchEvent---ACTION_MOVE---mSlidingView.getScrollX,(int) scrollY" + mSlidingView.getScrollX() + ", " + ((int) scrollY));
mSlidingView.scrollTo(mSlidingView.getScrollX(),
(int) scrollY);
}
}
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
Log.d("0320", "onTouchEvent---ACTION_UP");
if (mIsBeingDragged) {
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(100);
float yVelocity = velocityTracker.getYVelocity();// 滑动的速度
Log.d("0320", "onTouchEvent---ACTION_UP--yVelocity = " + yVelocity);
int oldScrollY = mSlidingView.getScrollY();
Log.d("0320", "onTouchEvent---ACTION_UP--oldScrollY = " + oldScrollY);
int dy = 0;
/*if (oldScrollY <= 0 && canSlideLeft) {// left view
Log.d("0320", "onTouchEvent---ACTION_UP--oldScrollY <= 0 && canSlideLeft");
if (yVelocity > VELOCITY) {
Log.d("0320", "onTouchEvent---ACTION_UP--yVelocity > VELOCITY");
dy = -getMenuViewWidth() - oldScrollY;
Log.d("0320", "onTouchEvent---ACTION_UP---yVelocity > VELOCITY---dy = " + dy);
} else if (yVelocity < -VELOCITY) {
dy = -oldScrollY;
// if (hasClickLeft) {
// hasClickLeft = false;
// setCanSliding(tCanSlideLeft, tCanSlideRight);
// }
Log.d("0320", "onTouchEvent---ACTION_UP---yVelocity < -VELOCITY---dy = " + dy);
} else if (oldScrollY < -getMenuViewWidth() / 2) {
dy = -getMenuViewWidth() - oldScrollY;
Log.d("0320", "onTouchEvent---ACTION_UP---oldScrollY < -getMenuViewWidth() / 2---dy = " + dy);
} else if (oldScrollY >= -getMenuViewWidth() / 2) {
dy = -oldScrollY;
Log.d("0320", "onTouchEvent---ACTION_UP---oldScrollY > -getMenuViewWidth() / 2---dy = " + dy);
// if (hasClickLeft) {
// hasClickLeft = false;
// setCanSliding(tCanSlideLeft, tCanSlideRight);
// }
}
}*/
if (oldScrollY >= 0 && canSlideRight) {
if (yVelocity < -VELOCITY) {
dy = getMenuViewWidth() - oldScrollY;
} else if (yVelocity > VELOCITY) {
dy = -oldScrollY;
// if (hasClickRight) {
// hasClickRight = false;
// setCanSliding(tCanSlideLeft, tCanSlideRight);
// }
} else if (oldScrollY > getMenuViewWidth() / 2) {
dy = getMenuViewWidth() - oldScrollY;
} else if (oldScrollY <= getMenuViewWidth() / 2) {
dy = -oldScrollY;
// if (hasClickRight) {
// hasClickRight = false;
// setCanSliding(tCanSlideLeft, tCanSlideRight);
// }
}
}
Log.d("0320", "onTouchEvent---smoothScrollTo");
smoothScrollTo(dy);
}
break;
}
return true;
}
private int getMenuViewWidth() {
if (mSlidingView == null) {
return 0;
}
return 300;
}
// private int getDetailViewWidth() {
// if (mDetailView == null) {
// return 0;
// }
// return mDetailView.getWidth();
// }
void smoothScrollTo(int dy) {
Log.d("0320", "onTouchEvent---smoothScrollTo");
int duration = 500;
int oldScrollY = mSlidingView.getScrollY();
mScroller.startScroll(mSlidingView.getScrollX(), oldScrollY, mSlidingView.getScrollX(),
dy, duration);
invalidate();
}
}
android demo http://download.csdn.net/detail/tyh123456789/7360265