不管是安卓系統還是IOS系統都會有果凍滑動效果給用戶展現,但是在Android中程序員要怎么實現呢?大家了解操作方法嗎?別著急,本文是武林技術頻道小編為大家帶來最專業的介紹,可別錯過了哦!
在微信是的處理方法是讓用戶滑動,但最終還是回滾到最初的地方,這樣的效果很生動(畢竟成功還是取決于細節)。那么在安卓我們要怎么弄呢。下面為大家介紹一下JellyScrollView,是我繼承ScrollView的一個有阻尼的效果的果凍滑動控件。
處理微信的方法是讓用戶滑動,但最終會回滾到原來的位置,非常生動(畢竟,成功取決于細節)。那么我們在android中要做什么呢?讓我們介紹一下jelly scrollview,一個我從scrollview繼承的jelly滑動控件。
下面話不多說了,先來看看效果圖

(在虛擬機或者真機跑起來是很流暢,可能是錄制視頻做成gif的時候有點卡頓。)
實現原理
其實只需要重寫下它的攔截方法的邏輯就好了,ScrollView的攔截方法onInterceptTouchEvent一般情況下都默認地返回false,表示不攔截該事件。我們只需要在用戶滑動了界面的情況下,攔截下來并移動布局就好了,當用戶松開手就恢復布局。很簡單
第一步:集成ScrollView重寫幾個構造方法;
第二步:重寫下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;//系統最少滑動距離 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(); } /*** * 根據 XML 生成視圖工作完成.該函數在生成視圖的最后調用,在所有子視圖添加完之后. 即使子類覆蓋了 onFinishInflate * 方法,也應該調用父類的方法,使該方法得以執行. */ @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. 之后記錄準確了就正常執行. */ 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
以上就是Android實現果凍滑動效果的控件,相信大家都了解了吧?更多的技術知識,請隨時關注武林技術頻道,我們一定會為大家竭誠服務。
新聞熱點
疑難解答