仿微信5.2界面解析

来源:互联网 发布:三国志13隐藏数据 编辑:程序博客网 时间:2024/05/05 15:46

     微信5.2内测版本,完全颠覆了之前版本的微信,虽然在功能上未做大的改动,但是界面却换了一套布局,更加注重细节和清新,让人用起来的比较心情愉悦。以下是微信5.2的页面<<5>>

<<1>>      <<2>>

     自己用了几天的5.2的版本,很喜欢这界面风格,如是自己写了一个仿微信界面。自我感觉还蛮像的。下面是我的界面

     <<3>>      <<4>>       <<5>>         <<5>>       <<5>>

     就那整体布局而言,之前的微信版本菜单栏在下面,5.2的则是在上面。我也不知道5.2版本的整体布局是如何实现。说说我这个仿界面是如何布局的。

     顶部是一个RelativeLayout.当然最重要的就是下面主体的布局咯。

     主体部分自定义MyScrollLayout继承自ViewGroup。里面放3个FrameLayout,分别对应聊天记录,发现,联系人,左右滑动来回切换。在每一个FrameLayout中会有一个ListView,这样一来ListView的上下滑动事件就会被自定义的MyScrollLayout的滑动事件截获掉,而只会触发左右滑动事件。而在自定义的MyScrollLayout中主要是对它的滑动事件进行分发和处理。

      在android的group中处理触摸事件的回调方法有3个,分别是

     1.dispatchTouchEvent,最先触发,这个回调事件主要是用来分发当前触摸事件到底给谁处理

     2.onInterceptTouchEvent,其次触发,这个回调事件是来判断是否要截取当前触摸事件,是否把事件传入子层。

     3.onTouchEvent,最后触发,用来处理逻辑功能

    

package com.example.isweixin;import android.R.bool;import android.content.Context;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.view.VelocityTracker;import android.view.View;import android.view.ViewConfiguration;import android.view.ViewGroup;import android.view.View.MeasureSpec;import android.widget.Scroller;public class MyScrollLayout extends ViewGroup{    private static final String TAG = "ScrollLayout";          private VelocityTracker mVelocityTracker;  // 鐢ㄤ簬鍒ゆ柇鐢╁姩鎵嬪娍        private static final int SNAP_VELOCITY = 400;            private Scroller  mScroller;// 婊戝姩鎺у埗鍣�    private int mCurScreen;        private int mDefaultScreen = 0;         private float mLastMotionX;           private float mLastMotionY;               private boolean isPass = false; //   private int mTouchSlop;    //    private static final int TOUCH_STATE_REST = 0;//    private static final int TOUCH_STATE_SCROLLING = 1;//    private int mTouchState = TOUCH_STATE_REST;        private OnViewChangeListener mOnViewChangeListener; public MyScrollLayout(Context context) {super(context);// TODO Auto-generated constructor stubinit(context);}public MyScrollLayout(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stubinit(context);}public MyScrollLayout(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stubinit(context);}private void init(Context context){mCurScreen = mDefaultScreen;       //   mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();                mScroller = new Scroller(context);     }@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {// TODO Auto-generated method stub if (changed) {                int childLeft = 0;                final int childCount = getChildCount();                                for (int i=0; i<childCount; i++) {                    final View childView = getChildAt(i);                    if (childView.getVisibility() != View.GONE) {                        final int childWidth = childView.getMeasuredWidth();                        childView.layout(childLeft, 0,                                 childLeft+childWidth, childView.getMeasuredHeight());                        childLeft += childWidth;                    }                }            }    }@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {// TODO Auto-generated method stubsuper.onMeasure(widthMeasureSpec, heightMeasureSpec);final int width = MeasureSpec.getSize(widthMeasureSpec);           final int widthMode = MeasureSpec.getMode(widthMeasureSpec);          final int count = getChildCount();               for (int i = 0; i < count; i++) {                   getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);               }                        scrollTo(mCurScreen * width, 0);} public void snapToDestination() {            final int screenWidth = getWidth();            final int destScreen = (getScrollX()+ screenWidth/2)/screenWidth;            snapToScreen(destScreen);     }   public void snapToScreen(int whichScreen) {            // get the valid layout page            whichScreen = Math.max(0, Math.min(whichScreen, getChildCount()-1));            if (getScrollX() != (whichScreen*getWidth())) {                                final int delta = whichScreen*getWidth()-getScrollX();                      mScroller.startScroll(getScrollX(), 0,                         delta, 0, 300);                        mCurScreen = whichScreen;                invalidate();       // Redraw the layout                            if (mOnViewChangeListener != null)            {            mOnViewChangeListener.OnViewChange(mCurScreen);            }        }        }    @Overridepublic void computeScroll() {// TODO Auto-generated method stubif (mScroller.computeScrollOffset()) {                scrollTo(mScroller.getCurrX(), mScroller.getCurrY());              postInvalidate();            }   }@Overridepublic boolean onTouchEvent(MotionEvent event) {// TODO Auto-generated method stub                               final int action = event.getAction();            final float x = event.getX();            final float y = event.getY();                        switch (action) {            case MotionEvent.ACTION_DOWN:         System.out.println("父类点击onTouchEvent");          Log.i("", "onTouchEvent  ACTION_DOWN");                  if (mVelocityTracker == null) {                mVelocityTracker = VelocityTracker.obtain();                mVelocityTracker.addMovement(event);     }                     if (!mScroller.isFinished()){                    mScroller.abortAnimation();                }                            mLastMotionX = x;                       mLastMotionY = y;                       break;                            case MotionEvent.ACTION_MOVE:          System.out.println("父类滑动onTouchEvent");           int deltaX = (int)(mLastMotionX - x);                      if (IsCanMove(deltaX))           {         if (mVelocityTracker != null)           {              mVelocityTracker.addMovement(event);            }                 mLastMotionX = x;                   scrollBy(deltaX, 0);           }                    break;                            case MotionEvent.ACTION_UP:               System.out.println("父类放开onTouchEvent");        int velocityX = 0;            if (mVelocityTracker != null)            {            mVelocityTracker.addMovement(event);             mVelocityTracker.computeCurrentVelocity(1000);              velocityX = (int) mVelocityTracker.getXVelocity();            }                                           if (velocityX > SNAP_VELOCITY && mCurScreen > 0) {                       // Fling enough to move left                       Log.e(TAG, "snap left");                    snapToScreen(mCurScreen - 1);                   } else if (velocityX < -SNAP_VELOCITY                           && mCurScreen < getChildCount() - 1) {                       // Fling enough to move right                       Log.e(TAG, "snap right");                    snapToScreen(mCurScreen + 1);                   } else {                       snapToDestination();                   }                                          if (mVelocityTracker != null) {                       mVelocityTracker.recycle();                       mVelocityTracker = null;                   }             //      mTouchState = TOUCH_STATE_REST;            break;              }                        return true;    }@Overridepublic boolean onInterceptTouchEvent(MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN: System.out.println("父类点击onInterceptTouchEvent");if(isPass){return true;}break;case MotionEvent.ACTION_MOVE: System.out.println("父类滑动onInterceptTouchEvent");if(isPass){return true;}break;case MotionEvent.ACTION_UP:System.out.println("父类放开onInterceptTouchEvent");break;}return super.onInterceptTouchEvent(event);}@Overridepublic boolean dispatchTouchEvent(MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN: mLastMotionX = event.getX();                       mLastMotionY = event.getY();System.out.println("父类点击dispatchTouchEvent");break;case MotionEvent.ACTION_MOVE: System.out.println(Math.abs(event.getX()- mLastMotionX));System.out.println(Math.abs(event.getY()- mLastMotionY));double tanNum = Math.atan(Math.abs(event.getY()-mLastMotionY)/Math.abs(event.getX()- mLastMotionX));double retote = tanNum/3.14*180;System.out.println("角度:"+retote);if (retote<45) {System.out.println("---------父类滑动dispatchTouchEvent");isPass= true;}else{isPass = false;}onInterceptTouchEvent(event);System.out.println("***************"+isPass);break;case MotionEvent.ACTION_UP:System.out.println("父类放开dispatchTouchEvent");break;}return super.dispatchTouchEvent(event);}private boolean IsCanMove(int deltaX){if (getScrollX() <= 0 && deltaX < 0 ){return false;}if  (getScrollX() >=  (getChildCount() - 1) * getWidth() && deltaX > 0){return false;}return true;}public void SetOnViewChangeListener(OnViewChangeListener listener){mOnViewChangeListener = listener;}}



1 0