高仿launcher和墨迹左右拖动效果

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

       

            首先自定义一个 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.     } 
public class MyScrollLayout extends ViewGroup{

    private VelocityTracker mVelocityTracker;  			// 用于判断甩动手势    
    private static final int SNAP_VELOCITY = 600;        
    private Scroller  mScroller;						// 滑动控制器	
    private int mCurScreen;    						    
	private int mDefaultScreen = 0;    						 
    private float mLastMotionX;       
 //   private int mTouchSlop;							
    
   private OnViewChangeListener mOnViewChangeListener;	
 
	public MyScrollLayout(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init(context);
	}	
	public MyScrollLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init(context);
	}
	
	public MyScrollLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub		
		init(context);
	}
	
	private void init(Context context)
	{
		mCurScreen = mDefaultScreen;    	  
	 //   mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();    	        
	    mScroller = new Scroller(context); 
	    
	}

	@Override
	protected 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;    
	                }    
	            }    
	        }    
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.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, Math.abs(delta)*2);
	            
	            mCurScreen = whichScreen;    
	            invalidate();       // Redraw the layout    	            
	            if (mOnViewChangeListener != null)
	            {
	            	mOnViewChangeListener.OnViewChange(mCurScreen);
	            }
	        }    
	    }    

	@Override
	public void computeScroll() {
		// TODO Auto-generated method stub
		if (mScroller.computeScrollOffset()) {    
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());  
            postInvalidate();    
        }   
	}

	@Override
	public 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: 	        	
	        	  Log.i("", "onTouchEvent  ACTION_DOWN");	        	  
	        	if (mVelocityTracker == null) {    
			            mVelocityTracker = VelocityTracker.obtain();    
			            mVelocityTracker.addMovement(event); 
			    }        	 
	            if (!mScroller.isFinished()){    
	                mScroller.abortAnimation();    
	            }                
	            mLastMotionX = x;	           
	            break;    
	                
	        case MotionEvent.ACTION_MOVE:  
	           int deltaX = (int)(mLastMotionX - x);	           
        	   if (IsCanMove(deltaX))
        	   {
        		 if (mVelocityTracker != null)
  		         {
  		            	mVelocityTracker.addMovement(event); 
  		         }   
  	            mLastMotionX = x;     
  	            scrollBy(deltaX, 0);	
        	   }
         
	           break;    	                
	        case MotionEvent.ACTION_UP:       	        	
	        	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          
	                snapToScreen(mCurScreen - 1);       
	            } else if (velocityX < -SNAP_VELOCITY       
	                    && mCurScreen < getChildCount() - 1) {       
	                // Fling enough to move right          
	                snapToScreen(mCurScreen + 1);       
	            } else {       
	                snapToDestination();       
	            }      
	            	            
	            if (mVelocityTracker != null) {       
	                mVelocityTracker.recycle();       
	                mVelocityTracker = null;       
	            }       
	      //      mTouchState = TOUCH_STATE_REST;
	            break;      
	        }    	            
	        return true;    
	}

	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. public class SwitchViewDemoActivity extends Activity implements OnViewChangeListener, OnClickListener{ 
  2.     /** Called when the activity is first created. */ 
  3.  
  4.     private MyScrollLayout mScrollLayout;    
  5.     private ImageView[] mImageViews;     
  6.     private int mViewCount;  
  7.     private int mCurSel; 
  8.      
  9.     @Override 
  10.     public void onCreate(Bundle savedInstanceState) { 
  11.         super.onCreate(savedInstanceState); 
  12.         setContentView(R.layout.main);         
  13.         init(); 
  14.          
  15.     } 
  16.      
  17.     private void init() 
  18.     { 
  19.         mScrollLayout = (MyScrollLayout) findViewById(R.id.ScrollLayout);    
  20.         LinearLayout linearLayout = (LinearLayout) findViewById(R.id.llayout); 
  21.         //动态添加一个layout控件 
  22.         LinearLayout layout=new LinearLayout(this); 
  23.         layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); 
  24.         layout.setBackgroundResource(R.drawable.guide01); 
  25.         mScrollLayout.addView(layout); 
  26.         //动态添加一个imageView控件         
  27.         ImageView imageView=new ImageView(this); 
  28.         imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
  29.         imageView.setPadding(15, 15, 15, 15); 
  30.         imageView.setImageResource(R.drawable.guide_round); 
  31.         linearLayout.addView(imageView); 
  32.          
  33.         mViewCount = mScrollLayout.getChildCount(); 
  34.         mImageViews = new ImageView[mViewCount];     
  35.         for(int i = 0; i < mViewCount; i++)      { 
  36.             mImageViews[i] = (ImageView) linearLayout.getChildAt(i); 
  37.             mImageViews[i].setEnabled(true); 
  38.             mImageViews[i].setOnClickListener(this); 
  39.             mImageViews[i].setTag(i); 
  40.         }        
  41.         mCurSel = 0
  42.         mImageViews[mCurSel].setEnabled(false);      
  43.         mScrollLayout.SetOnViewChangeListener(this); 
  44.          
  45.     } 
  46.  
  47.     private void setCurPoint(int index) 
  48.     { 
  49.         if (index < 0 || index > mViewCount - 1 || mCurSel == index)      { 
  50.             return ; 
  51.         }        
  52.         mImageViews[mCurSel].setEnabled(true); 
  53.         mImageViews[index].setEnabled(false);        
  54.         mCurSel = index
  55.     } 
  56.  
  57.     @Override 
  58.     public void OnViewChange(int view) { 
  59.         // TODO Auto-generated method stub 
  60.         setCurPoint(view); 
  61.     } 
  62.  
  63.     @Override 
  64.     public void onClick(View v) { 
  65.         // TODO Auto-generated method stub 
  66.         int pos = (Integer)(v.getTag()); 
  67.         setCurPoint(pos); 
  68.         mScrollLayout.snapToScreen(pos); 
  69.     } 
public class SwitchViewDemoActivity extends Activity implements OnViewChangeListener, OnClickListener{
    /** Called when the activity is first created. */

	private MyScrollLayout mScrollLayout;	
	private ImageView[] mImageViews;	
	private int mViewCount;	
	private int mCurSel;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
        init();
        
    }
    
    private void init()
    {
    	mScrollLayout = (MyScrollLayout) findViewById(R.id.ScrollLayout); 	
    	LinearLayout linearLayout = (LinearLayout) findViewById(R.id.llayout);
    	//动态添加一个layout控件
    	LinearLayout layout=new LinearLayout(this);
    	layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
    	layout.setBackgroundResource(R.drawable.guide01);
    	mScrollLayout.addView(layout);
    	//动态添加一个imageView控件        
    	ImageView imageView=new ImageView(this);
    	imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    	imageView.setPadding(15, 15, 15, 15);
    	imageView.setImageResource(R.drawable.guide_round);
    	linearLayout.addView(imageView);
    	
    	mViewCount = mScrollLayout.getChildCount();
    	mImageViews = new ImageView[mViewCount];   	
    	for(int i = 0; i < mViewCount; i++)    	{
    		mImageViews[i] = (ImageView) linearLayout.getChildAt(i);
    		mImageViews[i].setEnabled(true);
    		mImageViews[i].setOnClickListener(this);
    		mImageViews[i].setTag(i);
    	}    	
    	mCurSel = 0;
    	mImageViews[mCurSel].setEnabled(false);    	
    	mScrollLayout.SetOnViewChangeListener(this);
    	
    }

    private void setCurPoint(int index)
    {
    	if (index < 0 || index > mViewCount - 1 || mCurSel == index)    	{
    		return ;
    	}    	
    	mImageViews[mCurSel].setEnabled(true);
    	mImageViews[index].setEnabled(false);    	
    	mCurSel = index;
    }

    @Override
	public void OnViewChange(int view) {
		// TODO Auto-generated method stub
		setCurPoint(view);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int pos = (Integer)(v.getTag());
		setCurPoint(pos);
		mScrollLayout.snapToScreen(pos);
	}
}

最后是布局文件 :

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent" > 
  5.  
  6.     <cn.com.karl.scroll.MyScrollLayout 
  7.         xmlns:android="http://schemas.android.com/apk/res/android" 
  8.         android:id="@+id/ScrollLayout" 
  9.         android:layout_width="fill_parent" 
  10.         android:layout_height="fill_parent" > 
  11.  
  12.         <LinearLayout 
  13.             android:layout_width="fill_parent" 
  14.             android:layout_height="fill_parent" 
  15.             android:background="@drawable/guide01" > 
  16.         </LinearLayout> 
  17.  
  18.         <LinearLayout 
  19.             android:layout_width="fill_parent" 
  20.             android:layout_height="fill_parent" 
  21.             android:background="@drawable/guide02" > 
  22.         </LinearLayout> 
  23.  
  24.         <LinearLayout 
  25.             android:layout_width="fill_parent" 
  26.             android:layout_height="fill_parent" 
  27.             android:background="@drawable/guide03" > 
  28.         </LinearLayout> 
  29.  
  30.         <LinearLayout 
  31.             android:layout_width="fill_parent" 
  32.             android:layout_height="fill_parent" 
  33.             android:background="@drawable/guide04" > 
  34.         </LinearLayout> 
  35.  
  36.         <LinearLayout 
  37.             android:layout_width="fill_parent" 
  38.             android:layout_height="fill_parent" 
  39.             android:background="@drawable/guide05" > 
  40.         </LinearLayout> 
  41.     </cn.com.karl.scroll.MyScrollLayout> 
  42.  
  43.     <LinearLayout 
  44.         android:id="@+id/llayout" 
  45.         android:layout_width="wrap_content" 
  46.         android:layout_height="wrap_content" 
  47.         android:layout_alignParentBottom="true" 
  48.         android:layout_centerHorizontal="true" 
  49.         android:layout_marginBottom="24.0dip" 
  50.         android:orientation="horizontal" > 
  51.  
  52.         <ImageView 
  53.             android:layout_width="wrap_content" 
  54.             android:layout_height="wrap_content" 
  55.             android:layout_gravity="center_vertical" 
  56.             android:clickable="true" 
  57.             android:padding="15.0dip" 
  58.             android:src="@drawable/guide_round" /> 
  59.  
  60.         <ImageView 
  61.             android:layout_width="wrap_content" 
  62.             android:layout_height="wrap_content" 
  63.             android:layout_gravity="center_vertical" 
  64.             android:clickable="true" 
  65.             android:padding="15.0dip" 
  66.             android:src="@drawable/guide_round" /> 
  67.  
  68.         <ImageView 
  69.             android:layout_width="wrap_content" 
  70.             android:layout_height="wrap_content" 
  71.             android:layout_gravity="center_vertical" 
  72.             android:clickable="true" 
  73.             android:padding="15.0dip" 
  74.             android:src="@drawable/guide_round" /> 
  75.  
  76.         <ImageView 
  77.             android:layout_width="wrap_content" 
  78.             android:layout_height="wrap_content" 
  79.             android:layout_gravity="center_vertical" 
  80.             android:clickable="true" 
  81.             android:padding="15.0dip" 
  82.             android:src="@drawable/guide_round" /> 
  83.  
  84.         <ImageView 
  85.             android:layout_width="wrap_content" 
  86.             android:layout_height="wrap_content" 
  87.             android:layout_gravity="center_vertical" 
  88.             android:clickable="true" 
  89.             android:padding="15.0dip" 
  90.             android:src="@drawable/guide_round" /> 
  91.     </LinearLayout> 
  92.  
  93. </RelativeLayout> 

 

原文:http://my.oschina.net/chen106106/blog/50248

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值