歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android教程:高仿launcher和墨跡左右拖動效果

有這樣的想法,做出一個模仿launcher的效果。自己也曾從網上搜過很多資料,也思考過怎麼實現,最終還是參考了別人的資料實現了此效果,也解決了我這半個多月的冥思苦想,再次感謝,今天把代碼貼出來供大家學習,因為這方面做得比較好的資料缺失比較少(因為本人搜了很多資料都不能達到效果),如果大家覺得還不錯,請頂起。

更多Android相關信息見Android 專題頁面 http://www.linuxidc.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. }  
Copyright © Linux教程網 All Rights Reserved