Android橫向滾動屏幕特效分析
今天教大家寫一個類似于Android桌面的launcher效果的自定義控件,在開始寫之前大家需要熟悉幾個類和它們的方法,下面我分別列出來:
一.VelocityTracker 速度追蹤器
顧名思義這個類的作用主要是追蹤用戶手指在屏幕上的滑動速度。當你要跟蹤一個touch事件的時候,使用obtain()方法得到這個類的實 例,然后 用addMovement(MotionEvent)函數(shù)將你接受到的motion event加入到VelocityTracker類實例中。當你使用到速率時,使用computeCurrentVelocity(int)初始化速率的 單位,并獲得當前的事件的速率,然后使用getXVelocity() 或getXVelocity()獲得橫向和豎向的速率。
二.ViewConfiguration
這個類里面定義了android的許多標準的常量(UI的超時、大小和距離等)。
三.GestureDetector 手勢識別器
這個類主要是追蹤用戶手指在屏幕上的滑動方向,這個類在我們馬上要實現(xiàn)的類中沒有使用,但是使用的原理和它差不多,所以順便提一下,而且在以后的開發(fā)中,這個類也是經常使用的。
四.Scroller
這個類主要是支持view控件滑動,其實android很多可滑動的控件里面默認隱藏的就是這個類。而且這個類沒有進行實際的視圖移動,當調用它的 startScroll()方法實際上只是為了在父類調用computeScroll()方法前開始動畫,也就是說這個類實際上就是相當于一個代理,值是 為了給后面視圖移動添加一些動畫效果。所以單獨調用startScroll()而不重寫computeScroll()方法是不會看到任何效果的。這兩者 必須配合使用,才能有移動的時候的動畫效果。
其中Scroller.computeScrollOffset()方法是判斷scroller的移動動畫是否完成,當你調用startScroll()方法的時候這個方法返回的值一直都為true,如果采用其它方式移動視圖比如:scrollTo()或 scrollBy時那么這個方法返回false。
現(xiàn)在來講講startScroll(int startX, int startY, int dx, int dy, int duration)方法的四個參數(shù)的意思:
- startX表示當前視圖的x坐標值
- startY表示當前視圖的y坐標值
- dx表示在當前視圖的x坐標基礎上橫向移動的距離
- dy表示在當前視圖的y坐標基礎上縱向移動的距離
- duration表示視圖移動的操作在多少時間內執(zhí)行完場,也就是動畫的持續(xù)時間(單位:毫秒)
五.ViewGroup
這是個特殊的View,它繼承于Android.view.View,它的功能就是裝載和管理下一層的View對象或ViewGroup對象,也就說他是一個容納其它元素的的容器。
下面我們來分別分析我們要使用這5個類的那些方法,首先我們來看ViewGroup類,因為我們自定義的控件就是繼承至這個類,我們會重寫這個類中的5個方法如下:
1.onLayout(boolean changed, int l, int t, int r, int b)
這個方法是在onMeasure()方法執(zhí)行后調用,作用是父類為子類在屏幕上分配實際的寬度和高度。里面的四個參數(shù)分別表示,布局是否發(fā)生改變,布局左 上右下的邊距。
2.onMeasure(int widthMeasureSpec, int heightMeasureSpec)
這個方法在控件的父元素正要放置它的子控件時調用。然后傳入兩個參數(shù)——widthMeasureSpec和 heightMeasureSpec。它們指明控件可獲得的空間以及關于這個空間描述的元數(shù)據(jù)。比返回一個結果要好的方法是你傳遞View的高度和寬度到 setMeasuredDimension方法里。widthMeasureSpec和heightMeasureSpec參數(shù)在它們使用之前,首先要做 的是使用MeasureSpec類的靜態(tài)方法getMode和getSize來譯解。一個MeasureSpec包含一個尺寸和模式。
有三種可能的模式:
- UNSPECIFIED:父布局沒有給子布局任何限制,子布局可以任意大小。
- EXACTLY:父布局決定子布局的確切大小。不論子布局多大,它都必須限制在這個界限里。(當布局定義為一個固定像素或者fill_parent時就是EXACTLY模式)
- AT_MOST:子布局可以根據(jù)自己的大小選擇任意大小。(當布局定義為wrap_content時就是AT_MOST模式)
3.computeScroll()
這個方法主要是父類要求它的子類滾動的時候調用。在這個方法里,我們可以實現(xiàn) view的滾動操作,這里滾動并不是view的滾動而是布局的滾動。當調用scroller的startScroll()方法后父類就會調用這個方法實現(xiàn) 滾動視圖滾動操作。
4.onTouchEvent(MotionEvent event)
處理傳遞到view 的手勢事件。手勢事件類型包括ACTION_DOWN,ACTION_MOVE,ACTION_UP,ACTION_CANCEL等事件。Layout里 的onTouch默認返回值是false, View里的onTouch默認返回值是true,當我們手指點擊屏幕時候,先調用ACTION_DOWN事件,當onTouch里返回值是true的時 候,onTouch回繼續(xù)調用ACTION_UP事件,如果onTouch里返回值是false,那么onTouch只會調用ACTION_DOWN而不調用ACTION_UP.
5.onInterceptTouchEvent(MotionEvent ev)
用于攔截手勢事件的,每個手勢事件都會先調用這個方法。Layout里的onInterceptTouchEvent默認返回值是false,這樣touch事件會傳遞到View控件。
下面再將幾個大家可能比較混亂的方法說明一下:
Invalidate()和PostInvalidate(),這兩個方法作用都一樣,就是呼叫ui線程重新繪制 界面也就是刷新界面。那為什么要兩個方法呢,這是因為android是多線程應用,大家應該都知道在非UI線程中是不能直接操作界面控件的,所以第2個方 法就幫助大家在子線程中刷行界面,***個方法則是在UI線程中刷新界面。
getX()和getRawX()這兩個方法的左右都是獲取當前點在屏幕上的坐標,getX()是獲取當前點相對于當前視圖左上角的坐標,getRawX()則是獲取當前點相對于手機屏幕左上角的坐標。
上面已經把我們要用到的類和方法做了詳細描述,下面就是實現(xiàn)的源碼:
- import android.content.Context;
- import android.util.AttributeSet;
- import android.view.MotionEvent;
- import android.view.VelocityTracker;
- import android.view.View;
- import android.view.ViewConfiguration;
- import android.view.ViewGroup;
- import android.widget.Scroller;
- /**
- * @author
- */
- public class ScrollLayout extends ViewGroup {
- private Scroller mScroller;
- private VelocityTracker mVelocityTracker;
- /**
- * 當前的屏幕位置
- */
- private int mCurScreen;
- /**
- * 設置默認屏幕的屬性,0表示***個屏幕
- */
- private int mDefaultScreen = 0;
- /**
- * 標識滾動操作已結束
- */
- private static final int TOUCH_STATE_REST = 0;
- /**
- * 標識正在執(zhí)行滑動操作
- */
- private static final int TOUCH_STATE_SCROLLING = 1;
- /**
- * 標識滑動速率
- */
- private static final int SNAP_VELOCITY = 600;
- /**
- * 當前滑動狀態(tài)
- */
- private int mTouchState = TOUCH_STATE_REST;
- /**
- * 在用戶觸發(fā)ontouch事件之前,我們認為用戶能夠使view滑動的距離(像素)
- */
- private int mTouchSlop;
- /**
- * 手指觸碰屏幕的***一次x坐標
- */
- private float mLastMotionX;
- /**
- * 手指觸碰屏幕的***一次y坐標
- */
- @SuppressWarnings("unused")
- private float mLastMotionY;
- public ScrollLayout(Context context) {
- super(context);
- mScroller = new Scroller(context);
- mCurScreen = mDefaultScreen;
- mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
- }
- public ScrollLayout(Context context, AttributeSet attrs) {
- super(context, attrs);
- mScroller = new Scroller(context);
- mCurScreen = mDefaultScreen;
- mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
- }
- 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) {
- 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) {
- 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!");
- }
- final int count = getChildCount();
- for (int i = 0; i < count; i++) {
- getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
- }
- // 初始化視圖的位置
- scrollTo(mCurScreen * width, 0);
- }
- /**
- * 根據(jù)滑動的距離判斷移動到第幾個視圖
- */
- public void snapToDestination() {
- final int screenWidth = getWidth();
- final int destScreen = (getScrollX() + screenWidth / 2) / screenWidth;
- snapToScreen(destScreen);
- }
- /**
- * 滾動到制定的視圖
- *
- * @param whichScreen
- * 視圖下標
- */
- public void snapToScreen(int whichScreen) {
- 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, 1000);
- mCurScreen = whichScreen;
- invalidate();
- }
- }
- public void setToScreen(int whichScreen) {
- whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));
- mCurScreen = whichScreen;
- scrollTo(whichScreen * getWidth(), 0);
- }
- 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 (mVelocityTracker == null) {
- mVelocityTracker = VelocityTracker.obtain();
- }
- mVelocityTracker.addMovement(event);
- final int action = event.getAction();
- final float x = event.getX();
- switch (action) {
- case MotionEvent.ACTION_DOWN:
- if (!mScroller.isFinished()) {
- mScroller.abortAnimation();
- }
- mLastMotionX = x;
- break;
- case MotionEvent.ACTION_MOVE:
- int deltaX = (int) (mLastMotionX - x);
- mLastMotionX = x;
- scrollBy(deltaX, 0);
- break;
- case MotionEvent.ACTION_UP:
- final VelocityTracker velocityTracker = mVelocityTracker;
- velocityTracker.computeCurrentVelocity(1000);
- int velocityX = (int) velocityTracker.getXVelocity();
- if (velocityX > SNAP_VELOCITY && mCurScreen > 0) {
- // 向左移動
- snapToScreen(mCurScreen - 1);
- } else if (velocityX < -SNAP_VELOCITY
- && mCurScreen < getChildCount() - 1) {
- // 向右移動
- 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) {
- 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;
- }
- }