Android左滑菜单

首先要介绍一下Scroller 对象。

参考:http://blog.csdn.net/bigconvience/article/details/26697645

Scroller 启不到对View滑动的作动,View的滑动效果要由View自己的scrollTo()、scrollBy()、computeScroll() 等方法完成!通常是用mScroller记录/计算View滚动的位置,再重写View的computeScroll(),完成实际的滚动。 

 

注意:调用View的scrollTo()和scrollBy()是用于滑动View中的内容,而不是把某个View的位置进行改变。如果想改变莫个View在屏幕中的位置,可以使用如下的方法。

调用public void offsetLeftAndRight(int offset)用于左右移动方法或

       public void offsetTopAndBottom(int offset)用于上下移动。

如:button.offsetLeftAndRignt(300)表示将button控件向左移动300个像素。

scrollTo(int x, int y) 是将View中内容滑动到相应的位置,参考的坐标系原点为parent View的左上角。

(这一点很重要,我们的移动只是对View的内容进行移动!)

 

getScrollX()和getScrollY()是View类中专门用于记录滑动位置的方法!

 

下面是我写的一个常用的左滑菜单的例子!

public class ScrollLinearLayout extends LinearLayout {

	public static final String tag = "scroll";

	private Context mContext;

	// 这个类封装了滚动操作。滚动的持续时间可以通过构造函数传递,并且可以指定滚动动作的持续的最长时间。
	// 经过这段时间,滚动会自动定位到最终位置,并且通过computeScrollOffset()会得到的返回值为false,表明滚动动作已经结束。
	private Scroller mScroller;

	private int mLastX = 0;
	private int mLastY = 0;

	private int mHolderWidth = 120;

	public ScrollLinearLayout(Context context) {
		super(context);
		initWidgets(context);
	}

	public ScrollLinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		initWidgets(context);
	}

	private void initWidgets(Context context) {
		mContext = context;
		mScroller = new Scroller(mContext);

	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		return super.dispatchTouchEvent(ev);
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		// return super.onInterceptTouchEvent(ev);
		return true;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int x = (int) event.getX();// 当前动作相对View的X坐标
		int y = (int) event.getY();// 当前动作相对View的Y坐标

		int currentScrollX = getScrollX();// 当前View在X方向上的偏移量

		Log.e(tag, "x== " + x + " y== " + y + " scrollX==" + currentScrollX);

		if (MotionEvent.ACTION_DOWN == event.getAction()) {
			Log.e(tag, "MotionEvent.ACTION_DOWN");

			if (!mScroller.isFinished()) {// 上一次的动作还未结束,则滚动到最终x与y位置时中止动画
				mScroller.abortAnimation();// 停止动画。与forceFinished(boolean)相反,Scroller滚动到最终x与y位置时中止动画。
			}

			return true;
		} else if (MotionEvent.ACTION_UP == event.getAction()) {
			Log.e(tag, "MotionEvent.ACTION_UP");
			int newScrollX = 0;
			if (currentScrollX - mHolderWidth * 0.4 > 0) {// 向上动作时若已向右滑超过了4/10就直接向右滑100%
				newScrollX = mHolderWidth;
			}

			this.smoothScrollTo(newScrollX, 0);

		} else if (MotionEvent.ACTION_MOVE == event.getAction()) {
			Log.e(tag, "MotionEvent.ACTION_MOVE");
			int deltaX = x - mLastX;// 与上一个动作的偏移量(X方向)
			int deltaY = y - mLastY;// 与上一个动作的偏移量(Y方向)

			Log.e(tag, "deltaX== " + deltaX + " deltaY== " + deltaY);

			if (Math.abs(deltaX) > Math.abs(deltaY) * 2) {
				if (0 != deltaX) {
					int newScrollX = currentScrollX - deltaX;// 计算最新的X方向上的偏移量
					if (newScrollX < 0) {
						newScrollX = 0;
					} else if (newScrollX > mHolderWidth) {
						newScrollX = mHolderWidth;
					}
					this.scrollTo(newScrollX, 0);
				}
			}
		}
		mLastX = x;
		mLastY = y;
		return super.onTouchEvent(event);
	}

	// 缓慢滚动到指定位置
	private void smoothScrollTo(int destX, int destY) {
		int scrollX = getScrollX();
		int delta = destX - scrollX;
		mScroller.startScroll(scrollX, 0, delta, 0, Math.abs(delta) * 3);
		invalidate();
	}

	@Override
	public void computeScroll() {
		Log.e(tag, "computeScroll getCurrX==" + mScroller.getCurrX()
				+ " getCurrY==" + mScroller.getCurrY());
		if (mScroller.computeScrollOffset()) {
			scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
			postInvalidate();
		}
	}

}

 用法:

<com.study.widgets.ScrollLinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#000000"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="#096722" />

        <RelativeLayout
            android:layout_width="100dp"
            android:layout_height="150dp"
            android:background="#903109" >
        </RelativeLayout>
    </com.study.widgets.ScrollLinearLayout>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值