Android 高仿QQ HD mini左右滑动菜单栏,动画逼真。首先来看看几张效果图:
1.进入程序时,首先显示中间菜单,即QQHD mini的最近会话列表。
2.向左滑动,或者点击左上角图标,可以展开左边菜单栏,即QQ HD mini的好友列表,有动画效果。
3.向右滑动或者点击右上角图标,可以展开右边菜单,即QQ HD mini的设置之类的布局,这里我放了一个自定义view,作为例子。
下面让我们来看看最重要的那个自定义view的源码:
/**
* 可左右切换菜单栏的LinearLayout
*
* @author way
*
*/
public class CenterLayout extends LinearLayout {
private final static String TAG = "CenterLayout";
public static final int LEFT = 0x001;// 当前显示左菜单栏
public static final int RIGHT = 0x002;// 当前显示右菜单栏
public static final int MIDDLE = 0x000;// 当前显示中间主界面
private int mCurState = MIDDLE;// 当前显示的view
public final int MENU_border_Width = 50;// 边栏宽度
private Scroller mScroller;
private LinearLayout leftLayout, rightLayout, childLayout;
private Context context;
private boolean fling;
private boolean mIsBeingDragged = false;
private int mTouchSlop;
/**
* Position of the last motion event.
*/
private float mLastMotionX, mLastMotionY;
/**
* ID of the active pointer. This is used to retain consistency during
* drags/flings if multiple pointers are used.
*/
private int mActivePointerId = INVALID_POINTER;
/**
* Sentinel value for no current active pointer. Used by
* {@link #mActivePointerId}.
*/
private static final int INVALID_POINTER = -1;
int menuWidth = 0;
int moveWidth = 0;
// 3个构造器
public CenterLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public CenterLayout(Context context) {
super(context);
initView(context);
}
public Scroller getScroller() {
return mScroller;
}
// 初始化view
public void initView(Context context) {
this.context = context;
this.menuWidth = MENU_border_Width;
this.mScroller = new Scroller(context, AnimationUtils.loadInterpolator(
context, android.R.anim.overshoot_interpolator));
final ViewConfiguration configuration = ViewConfiguration.get(context);
mTouchSlop = configuration.getScaledTouchSlop();
mCurState = MIDDLE;// 初始化默认状态
}
/**
* 获取屏幕宽度
*
* @param context
* @return
*/
private int getViewWidthInPix(Context context) {
int viewWidthInPix = -1;
if (viewWidthInPix == -1) {
WindowManager manager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
viewWidthInPix = manager.getDefaultDisplay().getWidth();
}
return viewWidthInPix;
}
@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
super.onLayout(changed, left, top, right, bottom);
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.layout(child.getLeft() + moveWidth, child.getTop(),
child.getRight() + moveWidth, child.getBottom());
}
}
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
scrollTo(mScroller.getCurrX(), 0);
postInvalidate();
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
// Log.i(TAG, "onInterceptTouchEvent------>" + ev.getAction());
final int action = ev.getAction();
if ((action == MotionEvent.ACTION_MOVE) && (mIsBeingDragged)) {
return true;// 拦截不传递给child view
}
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
final float x = ev.getX();
final float y = ev.getY();
if (!inChild((int) x, (int) y)) {
mIsBeingDragged = false;
break;
// 超出边界,return false传递给子view处理
}
/*
* Remember location of down touch. ACTION_DOWN always refers to
* pointer index 0.
*/
mLastMotionX = x;
mLastMotionY = y;
mActivePointerId = ev.getPointerId(0);