Android實現(xiàn)果凍滑動效果的控件
前言
在微信是的處理方法是讓用戶滑動,但最終還是回滾到最初的地方,這樣的效果很生動(畢竟成功還是取決于細節(jié))。那么在安卓我們要怎么弄呢。下面為大家介紹一下JellyScrollView,是我繼承ScrollView的一個有阻尼的效果的果凍滑動控件。
下面話不多說了,先來看看效果圖
(在虛擬機或者真機跑起來是很流暢,可能是錄制視頻做成gif的時候有點卡頓。)
實現(xiàn)原理
其實只需要重寫下它的攔截方法的邏輯就好了,ScrollView的攔截方法onInterceptTouchEvent一般情況下都默認地返回false,表示不攔截該事件。我們只需要在用戶滑動了界面的情況下,攔截下來并移動布局就好了,當用戶松開手就恢復布局。很簡單
第一步:集成ScrollView重寫幾個構(gòu)造方法;
第二步:重寫下onFinishInflate方法,獲得第一個子view;
第三步:在攔截方法里面處理上面所說的邏輯;
public class JellyScrollView extends ScrollView { private View inner;// 子View private float y;// 點擊時y坐標 private Rect normal = new Rect();// 矩形(這里只是個形式,只是用于判斷是否需要動畫.) private boolean isCount = false;// 是否開始計算 private boolean isMoving = false;// 是否開始移動. private int top;// 拖動時時高度。 private int mTouchSlop;//系統(tǒng)最少滑動距離 public JellyScrollView(Context context) { super(context); } public JellyScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public JellyScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); } /*** * 根據(jù) XML 生成視圖工作完成.該函數(shù)在生成視圖的最后調(diào)用,在所有子視圖添加完之后. 即使子類覆蓋了 onFinishInflate * 方法,也應該調(diào)用父類的方法,使該方法得以執(zhí)行. */ @Override protected void onFinishInflate() { if (getChildCount() > 0) { inner = getChildAt(0); } } /**攔截事件*/ @Override public boolean onInterceptTouchEvent(MotionEvent ev) { if (inner != null) { int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: y = ev.getY(); top = 0; break; case MotionEvent.ACTION_UP: // 手指松開. isMoving = false; if (isNeedAnimation()) { animation(); } break; /*** * 排除出第一次移動計算,因為第一次無法得知y坐標, 在MotionEvent.ACTION_DOWN中獲取不到, * 因為此時是ScrollView的touch事件傳遞到到了ListView的子item上面.所以從第二次計算開始. * 然而我們也要進行初始化,就是第一次移動的時候讓滑動距離歸0. 之后記錄準確了就正常執(zhí)行. */ case MotionEvent.ACTION_MOVE: final float preY = y;// 按下時的y坐標 float nowY = ev.getY();// 每時刻y坐標 int deltaY = (int) (nowY - preY);// 滑動距離 if (!isCount) { deltaY = 0; // 在這里要歸0. } if (Math.abs(deltaY) < mTouchSlop && top <= 0) return true; // 當滾動到最上或者最下時就不會再滾動,這時移動布局 isNeedMove(); if (isMoving) { // 初始化頭部矩形 if (normal.isEmpty()) { // 保存正常的布局位置 normal.set(inner.getLeft(), inner.getTop(), inner.getRight(), inner.getBottom()); } // 移動布局 inner.layout(inner.getLeft(), inner.getTop() + deltaY / 3, inner.getRight(), inner.getBottom() + deltaY / 3); top += (deltaY / 6); } isCount = true; y = nowY; break; } } return super.onInterceptTouchEvent(ev); } /*** * 回縮動畫 */ 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); normal.setEmpty(); // 手指松開要歸0. isCount = false; y = 0; } // 是否需要開啟動畫 public boolean isNeedAnimation() { return !normal.isEmpty(); } /*** * 是否需要移動布局 * inner.getMeasuredHeight():獲取的是控件的總高度 * getHeight():獲取的是屏幕的高度 * * @return */ public void isNeedMove() { int offset = inner.getMeasuredHeight() - getHeight(); int scrollY = getScrollY(); // scrollY == 0是頂部 // scrollY == offset是底部 if (scrollY == 0 || scrollY == offset) { isMoving = true; } } }
然后在布局里面在最外層就使用我們的JellyScrollView
(為了方便展示,我只是大概寫了一部分布局代碼)
<?xml version="1.0" encoding="utf-8"?><cn.ichengxi.fang.view.JellyScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/bg" android:scrollbars="none"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/personal_setting_txt" android:layout_width="match_parent" android:layout_height="wrap_content" android:alpha="0.8" android:gravity="center_vertical" android:text="設置" android:textColor="@android:color/black" /> </LinearLayout></cn.ichengxi.fang.view.JellyScrollView>12345678910111213141516171819202122231234567891011121314151617181920212223
總結(jié)
好了,這樣就可以很優(yōu)雅的實現(xiàn)了果凍控件的效果啦。以上就是本文的全部內(nèi)容了,希望這篇文章的內(nèi)容對大家能有所幫助,如果有疑問大家可以留言交流。
相關(guān)文章
Android中加載網(wǎng)絡資源時的優(yōu)化可使用(線程+緩存)解決
Android 中加載網(wǎng)絡資源時的優(yōu)化;基本的思路是線程+緩存來解決,具體解決思路如下,有類似情況的朋友可以參考下哈2013-06-06Android ViewPager實現(xiàn)輪播圖效果
這篇文章主要為大家詳細介紹了Android ViewPager實現(xiàn)輪播圖效果的相關(guān)資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02Android中使用include標簽和merge標簽重復使用布局
這篇文章主要介紹了Android中使用include標簽和merge標簽重復使用布局,文中講解了創(chuàng)建可復用布局的例子以及include標簽和merge標簽使用例子,需要的朋友可以參考下2014-06-06Android中AlarmManager+Notification實現(xiàn)定時通知提醒功能
本篇文章主要介紹了Android中AlarmManager+Notification實現(xiàn)定時通知提醒功能,非常具有實用價值,需要的朋友可以參考下2017-10-10Android EditText限制輸入字符類型的方法總結(jié)
這篇文章主要介紹了Android EditText限制輸入字符類型的方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-03-03