Android教程:高仿launcher和墨迹左右拖动效果


有这样的想法,做出一个模仿launcher的效果。自己也曾从网上搜过很多资料,也思考过怎么实现,最终还是参考了别人的资料实现了此效果,也解决了我这半个多月的冥思苦想,再次感谢,今天把代码贴出来供大家学习,因为这方面做得比较好的资料缺失比较少(因为本人搜了很多资料都不能达到效果),如果大家觉得还不错,请顶起。

更多Android相关信息见Android 专题页面 http://www.bkjia.com/topicnews.aspx?tid=11

首先自定义一个 ViewGroup:

  1. public class MyScrollLayout extends ViewGroup{  
  2.   
  3.     private VelocityTracker mVelocityTracker;           // 用于判断甩动手势      
  4.     private static final int SNAP_VELOCITY = 600;          
  5.     private Scroller  mScroller;                        // 滑动控制器      
  6.     private int mCurScreen;                               
  7.     private int mDefaultScreen = 0;                            
  8.     private float mLastMotionX;         
  9.  //   private int mTouchSlop;                             
  10.       
  11.    private OnViewChangeListener mOnViewChangeListener;    
  12.    
  13.     public MyScrollLayout(Context context) {  
  14.         super(context);  
  15.         // TODO Auto-generated constructor stub  
  16.         init(context);  
  17.     }     
  18.     public MyScrollLayout(Context context, AttributeSet attrs) {  
  19.         super(context, attrs);  
  20.         // TODO Auto-generated constructor stub  
  21.         init(context);  
  22.     }  
  23.       
  24.     public MyScrollLayout(Context context, AttributeSet attrs, int defStyle) {  
  25.         super(context, attrs, defStyle);  
  26.         // TODO Auto-generated constructor stub       
  27.         init(context);  
  28.     }  
  29.       
  30.     private void init(Context context)  
  31.     {  
  32.         mCurScreen = mDefaultScreen;            
  33.      //   mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();                  
  34.         mScroller = new Scroller(context);   
  35.           
  36.     }  
  37.   
  38.     @Override  
  39.     protected void onLayout(boolean changed, int l, int t, int r, int b) {  
  40.         // TODO Auto-generated method stub        
  41.          if (changed) {      
  42.                 int childLeft = 0;      
  43.                 final int childCount = getChildCount();                       
  44.                 for (int i=0; i<childCount; i++) {      
  45.                     final View childView = getChildAt(i);      
  46.                     if (childView.getVisibility() != View.GONE) {      
  47.                         final int childWidth = childView.getMeasuredWidth();      
  48.                         childView.layout(childLeft, 0,       
  49.                                 childLeft+childWidth, childView.getMeasuredHeight());      
  50.                         childLeft += childWidth;      
  51.                     }      
  52.                 }      
  53.             }      
  54.     }  
  55.   
  56.     @Override  
  57.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  58.         // TODO Auto-generated method stub  
  59.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);         
  60.         final int width = MeasureSpec.getSize(widthMeasureSpec);         
  61.         final int widthMode = MeasureSpec.getMode(widthMeasureSpec);        
  62.                   
  63.         final int count = getChildCount();         
  64.         for (int i = 0; i < count; i++) {         
  65.             getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);         
  66.         }                  
  67.         scrollTo(mCurScreen * width, 0);          
  68.     }  
  69.   
  70.      public void snapToDestination() {      
  71.             final int screenWidth = getWidth();      
  72.             final int destScreen = (getScrollX()+ screenWidth/2)/screenWidth;      
  73.             snapToScreen(destScreen);      
  74.      }    
  75.       
  76.      public void snapToScreen(int whichScreen) {          
  77.             // get the valid layout page      
  78.             whichScreen = Math.max(0, Math.min(whichScreen, getChildCount()-1));      
  79.             if (getScrollX() != (whichScreen*getWidth())) {                       
  80.                 final int delta = whichScreen*getWidth()-getScrollX();      
  81.                         mScroller.startScroll(getScrollX(), 0,       
  82.                         delta, 0, Math.abs(delta)*2);  
  83.                   
  84.                 mCurScreen = whichScreen;      
  85.                 invalidate();       // Redraw the layout                      
  86.                 if (mOnViewChangeListener != null)  
  87.                 {  
  88.                     mOnViewChangeListener.OnViewChange(mCurScreen);  
  89.                 }  
  90.             }      
  91.         }      
  92.   
  93.     @Override  
  94.     public void computeScroll() {  
  95.         // TODO Auto-generated method stub  
  96.         if (mScroller.computeScrollOffset()) {      
  97.             scrollTo(mScroller.getCurrX(), mScroller.getCurrY());    
  98.             postInvalidate();      
  99.         }     
  100.     }  
  101.   
  102.     @Override  
  103.     public boolean onTouchEvent(MotionEvent event) {  
  104.         // TODO Auto-generated method stub                            
  105.             final int action = event.getAction();      
  106.             final float x = event.getX();      
  107.             final float y = event.getY();      
  108.                   
  109.             switch (action) {      
  110.             case MotionEvent.ACTION_DOWN:                 
  111.                   Log.i("", "onTouchEvent  ACTION_DOWN");                   
  112.                 if (mVelocityTracker == null) {      
  113.                         mVelocityTracker = VelocityTracker.obtain();      
  114.                         mVelocityTracker.addMovement(event);   
  115.                 }              
  116.                 if (!mScroller.isFinished()){      
  117.                     mScroller.abortAnimation();      
  118.                 }                  
  119.                 mLastMotionX = x;                
  120.                 break;      
  121.                       
  122.             case MotionEvent.ACTION_MOVE:    
  123.                int deltaX = (int)(mLastMotionX - x);                 
  124.                if (IsCanMove(deltaX))  
  125.                {  
  126.                  if (mVelocityTracker != null)  
  127.                  {  
  128.                         mVelocityTracker.addMovement(event);   
  129.                  }     
  130.                 mLastMotionX = x;       
  131.                 scrollBy(deltaX, 0);      
  132.                }  
  133.            
  134.                break;                         
  135.             case MotionEvent.ACTION_UP:                       
  136.                 int velocityX = 0;  
  137.                 if (mVelocityTracker != null)  
  138.                 {  
  139.                     mVelocityTracker.addMovement(event);   
  140.                     mVelocityTracker.computeCurrentVelocity(1000);    
  141.                     velocityX = (int) mVelocityTracker.getXVelocity();  
  142.                 }                                     
  143.                 if (velocityX > SNAP_VELOCITY && mCurScreen > 0) {         
  144.                     // Fling enough to move left            
  145.                     snapToScreen(mCurScreen - 1);         
  146.                 } else if (velocityX < -SNAP_VELOCITY         
  147.                         && mCurScreen < getChildCount() - 1) {         
  148.                     // Fling enough to move right            
  149.                     snapToScreen(mCurScreen + 1);         
  150.                 } else {         
  151.                     snapToDestination();         
  152.                 }        
  153.                                   
  154.                 if (mVelocityTracker != null) {         
  155.                     mVelocityTracker.recycle();         
  156.                     mVelocityTracker = null;         
  157.                 }         
  158.           //      mTouchState = TOUCH_STATE_REST;  
  159.                 break;        
  160.             }                     
  161.             return true;      
  162.     }  
  163.   
  164.     private boolean IsCanMove(int deltaX)  
  165.     {  
  166.         if (getScrollX() <= 0 && deltaX < 0 ){  
  167.             return false;  
  168.         }     
  169.         if  (getScrollX() >=  (getChildCount() - 1) * getWidth() && deltaX > 0){  
  170.             return false;  
  171.         }         
  172.         return true;  
  173.     }  
  174.       
  175.     public void SetOnViewChangeListener(OnViewChangeListener listener)  
  176.     {  
  177.         mOnViewChangeListener = listener;  
  178.     }  
  179. }  
  • 1
  • 2
  • 3
  • 下一页

相关内容