android 開源控件收集講解 (scrollLayout)


一.扯淡
一直以來都是在索取,而沒有貢獻,所以希望能夠也寫點東西做點總結。希望對大家有幫助,自己也是一個學習總結的過程.堅持堅持堅持。

今天給加大推薦一個好東西--ScrollLayout(滑動),他其實是一個左右滑動屏幕的控件,同樣來自oschina的項目.可能大家之前都用viewpage.或者說現在還有actionbar,
之所以推薦他,就是因為他真的是封裝的非常好.完全可以實現很友好的交互界面.

二.詳細

因為也是拿別人的東西講,所以我也不好去評論.但是我在做項目的時候就用上了.而且我覺得特別友好.用好了絕對是利器。

 1.怎么用 --xml中使用,代碼如下

  其中:net.oschina.app.widget.ScrollLayout是包名加類名,其實java很多新手特別是學生不知道面向對象是什么意思,我想說類的概念,面向對象的概念只有你拿着代碼去看

,去理解,完全可以理解這些。

  < net.oschina.app.widget.ScrollLayout   
      
android:id ="@+id/main_scrolllayout"     
      android:layout_width
="fill_parent"     
      android:layout_height
="fill_parent"
         android:layout_weight
="1" >   

  2.類的封裝

 ----------------------在這里你只需要新建一個包名,然后把這個類放進去。之后就是跟使用android本身的組件是一樣的了.很多人可能寫了這么久的代碼也都沒有嘗試過自定義過東西。我雖然這里也不敢說厲害.但是也還算是寫過.大家如果懶直接拿過去用就是了.但是我建議還是在項目不緊張的時候仔細看看別人的代碼,別人這么寫的原因是什么.之后肯定又是一大提升.什么時候自己自定義個。一定成就感十足.加油吧.大家可以看到他是繼承的ViewGroup.然后重寫了里面的方法.仔細看看他是怎么實現的.我怕我寫的不夠好,就不說了。仔細看看代碼應該就能夠明白.

/**
 * 左右滑動切換屏幕控件
 * 
@author  Yao.GUET date: 2011-05-04
 * @modify liux (
http://my.oschina.net/liux )
 
*/
public  class ScrollLayout  extends ViewGroup {
     private  static  final String TAG = "ScrollLayout";
     private Scroller mScroller;
     private VelocityTracker mVelocityTracker;
     private  int mCurScreen;
     private  int mDefaultScreen = 0;
     private  static  final  int TOUCH_STATE_REST = 0;
     private  static  final  int TOUCH_STATE_SCROLLING = 1;
     private  static  final  int SNAP_VELOCITY = 600;
     private  int mTouchState = TOUCH_STATE_REST;
     private  int mTouchSlop;
     private  float mLastMotionX;
     private  float mLastMotionY;
     private OnViewChangeListener mOnViewChangeListener;

     /**
     * 設置是否可左右滑動
     * 
@author  liux
     
*/
     private  boolean isScroll =  true;
     public  void setIsScroll( boolean b) {
         this.isScroll = b;
    }
    
     public ScrollLayout(Context context, AttributeSet attrs) {
         this(context, attrs, 0);
    }

     public ScrollLayout(Context context, AttributeSet attrs,  int defStyle) {
         super(context, attrs, defStyle);
        mScroller =  new Scroller(context);
        mCurScreen = mDefaultScreen;
        mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
    }

    @Override
     protected  void onLayout( boolean changed,  int l,  int t,  int r,  int b) {
         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) {
         // Log.e(TAG, "onMeasure");
         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
         final  int width = MeasureSpec.getSize(widthMeasureSpec);
         final  int widthMode = MeasureSpec.getMode(widthMeasureSpec);
         if (widthMode != MeasureSpec.EXACTLY) {
             throw  new IllegalStateException(
                    "ScrollLayout only canmCurScreen run at EXACTLY mode!");
        }
         final  int heightMode = MeasureSpec.getMode(heightMeasureSpec);
         if (heightMode != MeasureSpec.EXACTLY) {
             throw  new IllegalStateException(
                    "ScrollLayout only can run at EXACTLY mode!");
        }

         //  The children are given the same width and height as the scrollLayout
         final  int count = getChildCount();
         for ( int i = 0; i < count; i++) {
            getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
        }
         //  Log.e(TAG, "moving to screen "+mCurScreen);
        scrollTo(mCurScreen * width, 0);
    }

     /**
     * According to the position of current layout scroll to the destination
     * page.
     
*/
     public  void snapToDestination() {
         final  int screenWidth = getWidth();
         final  int destScreen = (getScrollX() + screenWidth / 2) / screenWidth;
        snapToScreen(destScreen);
    }

     public  void snapToScreen( int whichScreen) {
         // 是否可滑動
         if(!isScroll) {
             this.setToScreen(whichScreen);
             return;
        }
        
        scrollToScreen(whichScreen);
    }

     public  void scrollToScreen( 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) * 1); // 持續滾動時間 以毫秒為單位
            mCurScreen = whichScreen;
            invalidate();  //  Redraw the layout
            
             if (mOnViewChangeListener !=  null)
            {
                mOnViewChangeListener.OnViewChange(mCurScreen);
            }
        }
    }
    
     public  void setToScreen( int whichScreen) {
        whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));
        mCurScreen = whichScreen;
        scrollTo(whichScreen * getWidth(), 0);
        
         if (mOnViewChangeListener !=  null)
        {
            mOnViewChangeListener.OnViewChange(mCurScreen);
        }
    }

     public  int getCurScreen() {
         return mCurScreen;
    }

    @Override
     public  void computeScroll() {
         if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            postInvalidate();
        }
    }

    @Override
     public  boolean onTouchEvent(MotionEvent event) {
         // 是否可滑動
         if(!isScroll) {
             return  false;
        }
        
         if (mVelocityTracker ==  null) {
            mVelocityTracker = VelocityTracker.obtain();
        }
        mVelocityTracker.addMovement(event);
         final  int action = event.getAction();
         final  float x = event.getX();
         final  float y = event.getY();
         switch (action) {
         case MotionEvent.ACTION_DOWN:
             // Log.e(TAG, "event down!");
             if (!mScroller.isFinished()) {
                mScroller.abortAnimation();
            }
            mLastMotionX = x;
            
             // ---------------New Code----------------------
            mLastMotionY = y;
             // ---------------------------------------------
            
             break;
         case MotionEvent.ACTION_MOVE:
             int deltaX = ( int) (mLastMotionX - x);
            
             // ---------------New Code----------------------
             int deltaY = ( int) (mLastMotionY - y);
             if(Math.abs(deltaX) < 200 && Math.abs(deltaY) > 10)
                 break;
            mLastMotionY = y;
             // -------------------------------------
            
            mLastMotionX = x;
            scrollBy(deltaX, 0);
             break;
         case MotionEvent.ACTION_UP:
             // Log.e(TAG, "event : up");
            
//  if (mTouchState == TOUCH_STATE_SCROLLING) {
             final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000);
             int velocityX = ( int) velocityTracker.getXVelocity();
             // Log.e(TAG, "velocityX:" + velocityX);
             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;
         case MotionEvent.ACTION_CANCEL:
            mTouchState = TOUCH_STATE_REST;
             break;
        }
         return  true;
    }

    @Override
     public  boolean onInterceptTouchEvent(MotionEvent ev) {
         // Log.e(TAG, "onInterceptTouchEvent-slop:" + mTouchSlop);
         final  int action = ev.getAction();
         if ((action == MotionEvent.ACTION_MOVE)
                && (mTouchState != TOUCH_STATE_REST)) {
             return  true;
        }
         final  float x = ev.getX();
         final  float y = ev.getY();
         switch (action) {
         case MotionEvent.ACTION_MOVE:
             final  int xDiff = ( int) Math.abs(mLastMotionX - x);
             if (xDiff > mTouchSlop) {
                mTouchState = TOUCH_STATE_SCROLLING;
            }
             break;
         case MotionEvent.ACTION_DOWN:
            mLastMotionX = x;
            mLastMotionY = y;
            mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST
                    : TOUCH_STATE_SCROLLING;
             break;
         case MotionEvent.ACTION_CANCEL:
         case MotionEvent.ACTION_UP:
            mTouchState = TOUCH_STATE_REST;
             break;
        }
         return mTouchState != TOUCH_STATE_REST;
    }
    
     /**
     * 設置屏幕切換監聽器
     * 
@param  listener
     
*/
     public  void SetOnViewChangeListener(OnViewChangeListener listener)
    {
        mOnViewChangeListener = listener;
    }

     /**
     * 屏幕切換監聽器
     * 
@author  liux
     
*/
     public  interface OnViewChangeListener {
         public  void OnViewChange( int view);
    }
}

 3.方法

   在這個類里面我們可以看到幾個方法。看看我們怎么用他們,如下面的這個方法,就是要滑動到那個頁面。這個碉堡了.效果真的不錯.大家可以試試.

  

public  void snapToScreen( int whichScreen) {
         // 是否可滑動
         if(!isScroll) {
             this.setToScreen(whichScreen);
             return;
        }
        
        scrollToScreen(whichScreen);
    }

     public  void scrollToScreen( 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) * 1); // 持續滾動時間 以毫秒為單位
            mCurScreen = whichScreen;
            invalidate();  //  Redraw the layout
            
             if (mOnViewChangeListener !=  null)
            {
                mOnViewChangeListener.OnViewChange(mCurScreen);
            }
        }
    }

 

三.總結

          今天跟大家講了這個組件。也說了使用的方法,可能還是不夠詳細。所以我還是希望大家去下載源碼好好研究。我這也算是一種學習。大家有任何問題可以跟我交流.

我不是大牛。但是我希望我能夠堅持.-----

最后附上osa的源碼下載地址.有什么使用上的問題大家可以隨時交流--------

http://www.oschina.net/app

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM