標籤:
在android開發中,常常會要求IOS應用和android應用的體驗一致,所以對應android中開發時,很多控制項就需要開發人員自己定義,下面就為大家分享一個仿蘋果的彈性滑動ScrollView。
BounceScrollView源碼:
package com.joke.widget;import android.content.Context;import android.graphics.Rect;import android.os.Build;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.view.animation.TranslateAnimation;import android.widget.ScrollView;/** * ScrollView反彈效果的實現 */public class BounceScrollView extends ScrollView { private View inner;// 孩子View private float y;// 點擊時y座標 private Rect normal = new Rect();// 矩形(這裡只是個形式,只是用於判斷是否需要動畫.) private boolean isCount = false;// 是否開始計算 public BounceScrollView(Context context, AttributeSet attrs) { super(context, attrs); // 取消滑動到頂部或底部時邊緣的黃色或藍色底紋 if (Build.VERSION.SDK_INT >= 9) { this.setOverScrollMode(View.OVER_SCROLL_NEVER); } } /*** * 根據 XML 產生視圖工作完成.該函數在產生視圖的最後調用,在所有子視圖添加完之後. 即使子類覆蓋了 onFinishInflate * 方法,也應該調用父類的方法,使該方法得以執行. */ @Override protected void onFinishInflate() { if (getChildCount() > 0) { inner = getChildAt(0); } } /*** * 監聽touch */ @Override public boolean onTouchEvent(MotionEvent ev) { if (inner != null) { commOnTouchEvent(ev); } return super.onTouchEvent(ev); } /*** * 觸摸事件 * * @param ev */ public void commOnTouchEvent(MotionEvent ev) { int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_UP: // 手指鬆開. if (isNeedAnimation()) { animation(); isCount = false; } break; /*** * 排除出第一次移動計算,因為第一次無法得知y座標, 在MotionEvent.ACTION_DOWN中擷取不到, * 因為此時是MyScrollView的touch事件傳遞到到了LIstView的孩子item上面.所以從第二次計算開始. * 然而我們也要進行初始化,就是第一次移動的時候讓滑動距離歸0. 之後記錄準確了就正常執行. */ case MotionEvent.ACTION_MOVE: final float preY = y;// 按下時的y座標 float nowY = ev.getY();// 時時y座標 int deltaY = (int) (preY - nowY);// 滑動距離 if (!isCount) { deltaY = 0; // 在這裡要歸0. } y = nowY; // 當滾動到最上或者最下時就不會再滾動,這時移動布局 if (isNeedMove()) { // 初始化頭部矩形 if (normal.isEmpty()) { // 儲存正常的布局位置 normal.set(inner.getLeft(), inner.getTop(), inner.getRight(), inner.getBottom()); } // Log.e("jj", "矩形:" + inner.getLeft() + "," + inner.getTop() // + "," + inner.getRight() + "," + inner.getBottom()); // 移動布局 inner.layout(inner.getLeft(), inner.getTop() - deltaY / 2, inner.getRight(), inner.getBottom() - deltaY / 2); } isCount = true; break; default: break; } } /*** * 回縮動畫 */ public void animation() { // 開啟移動動畫 TranslateAnimation ta = new TranslateAnimation(0, 0, inner.getTop(), normal.top); ta.setDuration(200); inner.startAnimation(ta); // 設定回到正常的布局位置 inner.layout(normal.left, normal.top, normal.right, normal.bottom); // Log.e("jj", "迴歸:" + normal.left + "," + normal.top + "," + // normal.right // + "," + normal.bottom); normal.setEmpty(); } // 是否需要開啟動畫 public boolean isNeedAnimation() { return !normal.isEmpty(); } /*** * 是否需要移動布局 inner.getMeasuredHeight():擷取的是控制項的總高度 * * getHeight():擷取的是螢幕的高度 * * @return */ public boolean isNeedMove() { int offset = inner.getMeasuredHeight() - getHeight(); int scrollY = getScrollY(); // Log.e("jj", "scrolly=" + scrollY); // 0是頂部,後面那個是底部 if (scrollY == 0 || scrollY == offset) { return true; } return false; }}
使用說明:
1、直接將BounceScrollView放到*.widget包中
2、在布局檔案中直接使用BounceScrollView包裹其他布局
<com.joke.widget.BounceScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fadeScrollbars="false" android:fadingEdge="none" android:fadingEdgeLength="0dip" tools:context=".MainActivity" > <!-- android:background="@drawable/coversation_bg" --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FF0000" android:orientation="vertical" > ... </LinearLayout></com.joke.widget.BounceView>
值得注意的是:BounceScrollView是直接繼承自ScrollView的,那麼BounceScrollView也需要遵循ScrollView的約定,它內部只允許有一個子View,所以其他布局需要使用一個ViewGroup來包裹。
另外:還可以給BounceScrollView加上背景,使用:android:background="@drawable/coversation_bg"
例如:coversation_bg.xml還可以實現圖片的疊加效果
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item> <bitmap android:src="@drawable/cover_default_bg" android:tileMode="repeat" /> </item> <item android:top="10.0dip"> <bitmap android:gravity="top|center" android:src="@drawable/conversation_bg_logo" /> </item></layer-list>
仿蘋果彈性布局