我仿照写了一个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代码很简单, 只是加载一下布局文件就可以了~
我们可以在右边布置一个菜单,这样很方便的划出菜单栏~