今天教大家寫一個類似於android桌面的launcher效果的自定義控件,在開始寫之前大家需要熟悉幾個類和它們的方法,下面我分別列出來:
1.VelocityTracker 速度追蹤器,顧名思義這個累的作用主要是追蹤用戶手指在屏幕上的滑動速度。當你要跟蹤一個touch事件的時候,使用obtain()方法得到這個類的實例,然後 用addMovement(MotionEvent)函數將你接受到的motion event加入到VelocityTracker類實例中。當你使用到速率時,使用computeCurrentVelocity(int)初始化速率的單位,並獲得當前的事件的速率,然後使用getXVelocity() 或getXVelocity()獲得橫向和豎向的速率。
2.ViewConfiguration 這個類裏面 定義了android的許多標準的常量(UI的超時、大小和距離等)。
3.GestureDetector 手勢識別器,這個類主要是追蹤用戶手指在屏幕上的滑動方向,這個類在我們馬上要實現的類中沒有使用,但是使用的原理和它差不多,所以順便提一下,而且在以後的開發中,這個類也是經常使用的。
4.Scroller 這個類主要是支持view控件滑動,其實android很多可滑動的控件裏面默認隱藏的就是這個類。而且這個類沒有進行實際的視圖移動,當調用它的startScroll()方法實際上只是爲了在父類調用computeScroll()方法前開始動畫,也就是說這個類實際上就是相當於一個代理,值是爲了給後面視圖移動添加一些動畫效果。所以單獨調用startScroll()而不重寫computeScroll()方法是不會看到任何效果的。這兩者必須配合使用,纔能有移動的時候的動畫效果。
其中Scroller.computeScrollOffset()方法是判斷scroller的移動動畫是否完成,當你調用startScroll()方法的時候這個方法返回的值一直都爲true,如果採用其它方式移動視圖比如:scrollTo()或scrollBy時那麼這個方法返回false。
現在來講講startScroll(int startX, int startY, int dx, int dy, int duration)方法的四個參數的意思
startX表示當前視圖的x座標值
startY表示當前視圖的y座標值
dx表示在當前視圖的x座標基礎上橫向移動的距離
dy表示在當前視圖的y座標基礎上縱向移動的距離
duration表示視圖移動的操作在多少時間內執行完場,也就是動畫的持續時間(單位:毫秒)
5.ViewGroup 這是個特殊的View,它繼承於Android.view.View,它的功能就是裝載和管理下一層的View對象或ViewGroup對象,也就說他是一個容納其它元素的的容器。
下面我們來分別分析我們要使用這5個類的那些方法,首先我們來看ViewGroup類,因爲我們自定義的控件就是繼承至這個類,我們會重寫這個類中的5個方法如下:
1.onLayout(boolean changed, int l, int t, int r, int b) 這個方法是在onMeasure()方法執行後調用,作用是父類爲子類在屏幕上分配實際的寬度和高度。裏面的四個參數分別表示,佈局是否發生改變,佈局左上右下的邊距。
2.onMeasure(int widthMeasureSpec, int heightMeasureSpec)這個方法在控件的父元素正要放置它的子控件時調用。然後傳入兩個參數——widthMeasureSpec和heightMeasureSpec。它們指明控件可獲得的空間以及關於這個空間描述的元數據。比返回一個結果要好的方法是你傳遞View的高度和寬度到setMeasuredDimension方法裏。widthMeasureSpec和heightMeasureSpec參數在它們使用之前,首先要做的是使用MeasureSpec類的靜態方法getMode和getSize來譯解。一個MeasureSpec包含一個尺寸和模式。有三種可能的模式:
UNSPECIFIED:父佈局沒有給子佈局任何限制,子佈局可以任意大小。
EXACTLY:父佈局決定子佈局的確切大小。不論子佈局多大,它都必須限制在這個界限裏。(當佈局定義爲一個固定像素或者fill_parent時就是EXACTLY模式)
AT_MOST:子佈局可以根據自己的大小選擇任意大小。(當佈局定義爲wrap_content時就是AT_MOST模式)
3.computeScroll()這個方法主要是父類要求它的子類滾動的時候調用。在這個方法裏,我們可以實現view的滾動操作,這裏滾動並不是view的滾動而是佈局的滾動。當調用scroller的startScroll()方法後父類就會調用這個方法實現滾動視圖滾動操作。
4.onTouchEvent(MotionEvent event) 處理傳遞到view 的手勢事件。手勢事件類型包括ACTION_DOWN,ACTION_MOVE,ACTION_UP,ACTION_CANCEL等事件。Layout裏的onTouch默認返回值是false, View裏的onTouch默認返回值是true,當我們手指點擊屏幕時候,先調用ACTION_DOWN事件,當onTouch裏返回值是true的時候,onTouch回繼續調用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()則是獲取當前點相對於手機屏幕左上角的座標。
上面已經把我們要用到的類和方法做了詳細描述下面就是實現的源碼:
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;
/**
* 標識正在執行滑動操作
*/
private static final int TOUCH_STATE_SCROLLING = 1;
/**
* 標識滑動速率
*/
private static final int SNAP_VELOCITY = 600;
/**
* 當前滑動狀態
*/
private int mTouchState = TOUCH_STATE_REST;
/**
* 在用戶觸發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);
}
/**
* 根據滑動的距離判斷移動到第幾個視圖
*/
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;
}
}