左右滑动控件

时间:2016-07-28 11:36:32
【文件属性】:

文件名称:左右滑动控件

文件大小:1.01MB

文件格式:ZIP

更新时间:2016-07-28 11:36:32

左右滑动控件

public class SlidingMenu extends RelativeLayout { private View mSlidingView; private View mMenuView; private View mDetailView; private RelativeLayout bgShade; private int screenWidth; private int screenHeight; 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; private boolean tCanSlideLeft = true; private boolean tCanSlideRight = false; private boolean hasClickLeft = false; private boolean hasClickRight = false; public SlidingMenu(Context context) { super(context); init(context); } private void init(Context context) { mContext = context; bgShade = new RelativeLayout(context); mScroller = new Scroller(getContext()); mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop(); WindowManager windowManager = ((Activity) context).getWindow() .getWindowManager(); Display display = windowManager.getDefaultDisplay(); screenWidth = display.getWidth(); screenHeight = display.getHeight(); LayoutParams bgParams = new LayoutParams(screenWidth, screenHeight); bgParams.addRule(RelativeLayout.CENTER_IN_PARENT); bgShade.setLayoutParams(bgParams); } public SlidingMenu(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public SlidingMenu(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context); } public void addViews(View left, View center, View right) { setLeftView(left); setRightView(right); setCenterView(center); } public void setLeftView(View view) { LayoutParams behindParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT); addView(view, behindParams); mMenuView = view; } public void setRightView(View view) { LayoutParams behindParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT); behindParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); addView(view, behindParams); mDetailView = view; } public void setCenterView(View view) { LayoutParams aboveParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); LayoutParams bgParams = new LayoutParams(screenWidth, screenHeight); bgParams.addRule(RelativeLayout.CENTER_IN_PARENT); View bgShadeContent = new View(mContext); bgShadeContent.setBackgroundDrawable(getResources().getDrawable( R.drawable.shade_bg)); bgShade.addView(bgShadeContent, bgParams); addView(bgShade, bgParams); addView(view, aboveParams); mSlidingView = view; mSlidingView.bringToFront(); } @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); if (x < 0) bgShade.scrollTo(x + 20, y); else bgShade.scrollTo(x - 20, y); } } invalidate(); } } } private boolean canSlideLeft = true; private boolean canSlideRight = false; public void setCanSliding(boolean left, boolean right) { canSlideLeft = left; canSlideRight = right; } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { final int action = ev.getAction(); final float x = ev.getX(); final float y = ev.getY(); switch (action) { case MotionEvent.ACTION_DOWN: mLastMotionX = x; mLastMotionY = y; mIsBeingDragged = false; if (canSlideLeft) { mMenuView.setVisibility(View.VISIBLE); mDetailView.setVisibility(View.INVISIBLE); } if (canSlideRight) { mMenuView.setVisibility(View.INVISIBLE); mDetailView.setVisibility(View.VISIBLE); } break; case MotionEvent.ACTION_MOVE: final float dx = x - mLastMotionX; final float xDiff = Math.abs(dx); final float yDiff = Math.abs(y - mLastMotionY); if (xDiff > mTouchSlop && xDiff > yDiff) { if (canSlideLeft) { float oldScrollX = mSlidingView.getScrollX(); if (oldScrollX < 0) { mIsBeingDragged = true; mLastMotionX = x; } else { if (dx > 0) { mIsBeingDragged = true; mLastMotionX = x; } } } else if (canSlideRight) { float oldScrollX = mSlidingView.getScrollX(); if (oldScrollX > 0) { mIsBeingDragged = true; mLastMotionX = x; } else { if (dx < 0) { mIsBeingDragged = true; mLastMotionX = x; } } } } break; } return mIsBeingDragged; } @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(); switch (action) { case MotionEvent.ACTION_DOWN: if (!mScroller.isFinished()) { mScroller.abortAnimation(); } mLastMotionX = x; mLastMotionY = y; if (mSlidingView.getScrollX() == -getMenuViewWidth() && mLastMotionX < getMenuViewWidth()) { return false; } if (mSlidingView.getScrollX() == getDetailViewWidth() && mLastMotionX > getMenuViewWidth()) { return false; } break; case MotionEvent.ACTION_MOVE: if (mIsBeingDragged) { final float deltaX = mLastMotionX - x; mLastMotionX = x; float oldScrollX = mSlidingView.getScrollX(); float scrollX = oldScrollX + deltaX; if (canSlideLeft) { if (scrollX > 0) scrollX = 0; } if (canSlideRight) { if (scrollX < 0) scrollX = 0; } if (deltaX < 0 && oldScrollX < 0) { // left view final float leftBound = 0; final float rightBound = -getMenuViewWidth(); if (scrollX > leftBound) { scrollX = leftBound; } else if (scrollX < rightBound) { scrollX = rightBound; } } else if (deltaX > 0 && oldScrollX > 0) { // right view final float rightBound = getDetailViewWidth(); final float leftBound = 0; if (scrollX < leftBound) { scrollX = leftBound; } else if (scrollX > rightBound) { scrollX = rightBound; } } if (mSlidingView != null) { mSlidingView.scrollTo((int) scrollX, mSlidingView.getScrollY()); if (scrollX < 0) bgShade.scrollTo((int) scrollX + 20, mSlidingView.getScrollY()); else bgShade.scrollTo((int) scrollX - 20, mSlidingView.getScrollY()); } } break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: if (mIsBeingDragged) { final VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(100); float xVelocity = velocityTracker.getXVelocity(); int oldScrollX = mSlidingView.getScrollX(); int dx = 0; if (oldScrollX <= 0 && canSlideLeft) {// left view if (xVelocity > VELOCITY) { dx = -getMenuViewWidth() - oldScrollX; } else if (xVelocity < -VELOCITY) { dx = -oldScrollX; if (hasClickLeft) { hasClickLeft = false; setCanSliding(tCanSlideLeft, tCanSlideRight); } } else if (oldScrollX < -getMenuViewWidth() / 2) { dx = -getMenuViewWidth() - oldScrollX; } else if (oldScrollX >= -getMenuViewWidth() / 2) { dx = -oldScrollX; if (hasClickLeft) { hasClickLeft = false; setCanSliding(tCanSlideLeft, tCanSlideRight); } } } if (oldScrollX >= 0 && canSlideRight) { if (xVelocity < -VELOCITY) { dx = getDetailViewWidth() - oldScrollX; } else if (xVelocity > VELOCITY) { dx = -oldScrollX; if (hasClickRight) { hasClickRight = false; setCanSliding(tCanSlideLeft, tCanSlideRight); } } else if (oldScrollX > getDetailViewWidth() / 2) { dx = getDetailViewWidth() - oldScrollX; } else if (oldScrollX <= getDetailViewWidth() / 2) { dx = -oldScrollX; if (hasClickRight) { hasClickRight = false; setCanSliding(tCanSlideLeft, tCanSlideRight); } } } smoothScrollTo(dx); } break; } return true; } private int getMenuViewWidth() { if (mMenuView == null) { return 0; } return mMenuView.getWidth(); } private int getDetailViewWidth() { if (mDetailView == null) { return 0; } return mDetailView.getWidth(); } void smoothScrollTo(int dx) { int duration = 500; int oldScrollX = mSlidingView.getScrollX(); mScroller.startScroll(oldScrollX, mSlidingView.getScrollY(), dx, mSlidingView.getScrollY(), duration); invalidate(); } public void showLeftView() { int menuWidth = mMenuView.getWidth(); int oldScrollX = mSlidingView.getScrollX(); if (oldScrollX == 0) { mMenuView.setVisibility(View.VISIBLE); mDetailView.setVisibility(View.INVISIBLE); smoothScrollTo(-menuWidth); tCanSlideLeft = canSlideLeft; tCanSlideRight = canSlideRight; hasClickLeft = true; setCanSliding(true, false); } else if (oldScrollX == -menuWidth) { smoothScrollTo(menuWidth); if (hasClickLeft) { hasClickLeft = false; setCanSliding(tCanSlideLeft, tCanSlideRight); } } } public void showRightView() { int menuWidth = mDetailView.getWidth(); int oldScrollX = mSlidingView.getScrollX(); if (oldScrollX == 0) { mMenuView.setVisibility(View.INVISIBLE); mDetailView.setVisibility(View.VISIBLE); smoothScrollTo(menuWidth); tCanSlideLeft = canSlideLeft; tCanSlideRight = canSlideRight; hasClickRight = true; setCanSliding(false, true); } else if (oldScrollX == menuWidth) { smoothScrollTo(-menuWidth); if (hasClickRight) { hasClickRight = false; setCanSliding(tCanSlideLeft, tCanSlideRight); } } } }


【文件预览】:
WyFrame
----.project(843B)
----project.properties(562B)
----src()
--------com()
----AndroidManifest.xml(977B)
----res()
--------drawable-ldpi()
--------drawable-hdpi()
--------drawable()
--------drawable-mdpi()
--------layout()
--------drawable-xhdpi()
--------values()
----.settings()
--------org.eclipse.jdt.core.prefs(177B)
----assets()
----README.md(858B)
----gen()
--------com()
----libs()
--------android-support-v4.jar(341KB)
----.classpath(459B)
----proguard-project.txt(781B)
----ic_launcher-web.png(54KB)
----bin()
--------resources.ap_(67KB)
--------classes()
--------AndroidManifest.xml(977B)
--------dexedLibs()
--------jarlist.cache(120B)
--------WyFrame.apk(210KB)
--------res()
--------classes.dex(528KB)

网友评论

  • 标记不太明白