android向左滑动显示第二屏

我仿照写了一个android向左滑动时显示另一页菜单的代码,下面是效果图和代码

                                                            

            没划动之前 向左滑动效果

代码部分:

首先继承linearlayout类

public class MyView extends LinearLayout {

	Scroller mScroller;

	public MyView(Context context) {
		this(context, null);
	}

	public MyView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mScroller = new Scroller(context);
	}

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

	int preX = 0;// = (int) event.getX();
	int preY = 0;
	int nowX = 0;
	int nowY = 0;
	int preCurrentX = 0;

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		int currentx = getScrollX();
		nowX = (int) event.getX();
		nowY = (int) event.getY();

		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			nowX = (int) event.getX();
			nowY = (int) event.getY();
			preX = (int) event.getX();
			preY = (int) event.getY();
			if (mScroller.isFinished()) {
				mScroller.abortAnimation();
			}
			return true;
		} else if (event.getAction() == MotionEvent.ACTION_UP) {
			int newX = -currentx + preCurrentX;
			System.out.println("current x = " + currentx + " pre = "
					+ preCurrentX + " new x = " + newX);
			if (newX != 0) {
				if (newX < -5) {
					newX = -400;
				}
				if (newX > 15) {
					newX = 0;
				}

				this.scrollTo(-newX, 0);
			}
			preCurrentX = currentx;
		} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
			// System.out.println("prex = " + preX + " now x = " + nowX);
			int deltaX = nowX - preX;
			int deltaY = nowY - preY;
			int newX = currentx - deltaX;
			if (Math.abs(deltaX) > Math.abs(deltaY) * 1.5) {
				newX = (-currentx + deltaX);
				System.out.println("current x = " + currentx + " now x = "
						+ nowX + " delda x = " + deltaX + " new x = " + newX);
				if (newX > 0)
					newX = 0;
				if (newX < -400)
					newX = -200;
				this.scrollTo(-newX, 0);
			}
		}
		preX = nowX;
		preY = nowY;
		return super.onTouchEvent(event);
	}

	private void smoothScrollto(int x, int y, int delta) {
		int scrollX = getScrollX();
		System.out.println("x = " + scrollX);
		mScroller.startScroll(scrollX, 0, x - scrollX, 0, Math.abs(delta) * 3);
		invalidate();
	}

	@Override
	public void computeScroll() {
		System.out.println();
		// TODO Auto-generated method stub
		if (mScroller.computeScrollOffset()) {
			System.out.println("compute scroll ...... 现在滚动完毕");
			scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
			System.out.println("getCurrx = " + mScroller.getCurrX()
					+ " getCurry = " + mScroller.getCurrY());
			postInvalidate();
		}
		// super.computeScroll();
	}

}

下面是layout文件

<com.example.scrolllayout.MyView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="horizontal" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:background="#00FF00" >
    </FrameLayout>

    <FrameLayout
        android:layout_width="200dp"
        android:layout_height="fill_parent"
        android:background="#ff0000" />

</com.example.scrolllayout.MyView>

activity代码很简单, 只是加载一下布局文件就可以了~

我们可以在右边布置一个菜单,这样很方便的划出菜单栏~


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值