Android仿IOS上拉下拉彈性效果的實例代碼
用過iphone的朋友相信都體驗過頁面上拉下拉有一個彈性的效果,使用起來用戶體驗很好;Android并沒有給我們封裝這樣一個效果,我們來看下在Android里如何實現(xiàn)這個效果。先看效果,感覺有些時候還是蠻實用的。

思路:其實原理很簡單,實現(xiàn)一個自定義的Scrollview方法(來自網(wǎng)上大神),然后在布局文件中使用自定義方法Scrollview就可以了。
代碼:
自定義View,繼承自Scrollview。MyReboundScrollView類
package com.wj.myreboundscrollview.customview;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ScrollView;
//仿ios可上提下拉的ScrollView
public class MyReboundScrollView extends ScrollView {
private static final String TAG = "ElasticScrollView";
//移動因子, 是一個百分比, 比如手指移動了100px, 那么View就只移動50px
//目的是達(dá)到一個延遲的效果
private static final float MOVE_FACTOR = 0.5f;
//松開手指后, 界面回到正常位置需要的動畫時間
private static final int ANIM_TIME = 100;
//ScrollView的子View, 也是ScrollView的唯一一個子View
private View contentView;
//手指按下時的Y值, 用于在移動時計算移動距離
//如果按下時不能上拉和下拉, 會在手指移動時更新為當(dāng)前手指的Y值
private float startY;
//用于記錄正常的布局位置
private Rect originalRect = new Rect();
//手指按下時記錄是否可以繼續(xù)下拉
private boolean canPullDown = false;
//手指按下時記錄是否可以繼續(xù)上拉
private boolean canPullUp = false;
//在手指滑動的過程中記錄是否移動了布局
private boolean isMoved = false;
public MyReboundScrollView(Context context) {
super(context);
}
public MyReboundScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onFinishInflate() {
if (getChildCount() > 0) {
contentView = getChildAt(0);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if(contentView == null) return;
//ScrollView中的唯一子控件的位置信息, 這個位置信息在整個控件的生命周期中保持不變
originalRect.set(contentView.getLeft(), contentView.getTop(), contentView
.getRight(), contentView.getBottom());
}
//在觸摸事件中, 處理上拉和下拉的邏輯
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (contentView == null) {
return super.dispatchTouchEvent(ev);
}
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
//判斷是否可以上拉和下拉
canPullDown = isCanPullDown();
canPullUp = isCanPullUp();
//記錄按下時的Y值
startY = ev.getY();
break;
case MotionEvent.ACTION_UP:
if(!isMoved) break; //如果沒有移動布局, 則跳過執(zhí)行
// 開啟動畫
TranslateAnimation anim = new TranslateAnimation(0, 0, contentView.getTop(),
originalRect.top);
anim.setDuration(ANIM_TIME);
contentView.startAnimation(anim);
// 設(shè)置回到正常的布局位置
contentView.layout(originalRect.left, originalRect.top,
originalRect.right, originalRect.bottom);
//將標(biāo)志位設(shè)回false
canPullDown = false;
canPullUp = false;
isMoved = false;
break;
case MotionEvent.ACTION_MOVE:
//在移動的過程中, 既沒有滾動到可以上拉的程度, 也沒有滾動到可以下拉的程度
if(!canPullDown && !canPullUp) {
startY = ev.getY();
canPullDown = isCanPullDown();
canPullUp = isCanPullUp();
break;
}
//計算手指移動的距離
float nowY = ev.getY();
int deltaY = (int) (nowY - startY);
//是否應(yīng)該移動布局
boolean shouldMove =
(canPullDown && deltaY > 0) //可以下拉, 并且手指向下移動
|| (canPullUp && deltaY< 0) //可以上拉, 并且手指向上移動
|| (canPullUp && canPullDown); //既可以上拉也可以下拉(這種情況出現(xiàn)在ScrollView包裹的控件比ScrollView還?。?
if(shouldMove){
//計算偏移量
int offset = (int)(deltaY * MOVE_FACTOR);
//隨著手指的移動而移動布局
contentView.layout(originalRect.left, originalRect.top + offset,
originalRect.right, originalRect.bottom + offset);
isMoved = true; //記錄移動了布局
}
break;
default:
break;
}
return super.dispatchTouchEvent(ev);
}
//判斷是否滾動到頂部
private boolean isCanPullDown() {
return getScrollY() == 0 ||
contentView.getHeight() < getHeight() + getScrollY();
}
//判斷是否滾動到底部
private boolean isCanPullUp() {
return contentView.getHeight() <= getHeight() + getScrollY();
}
}
代碼注釋非常清楚。
布局文件直接使用
<com.wj.myreboundscrollview.customview.MyReboundScrollView 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" tools:context=".MainActivity" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <EditText android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="12dip" android:hint="Your Name"/> <EditText android:id="@+id/et_feedback" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="32dip" android:hint="Your Feedback" android:lines="5"/> <Button android:id="@+id/btn_submit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="42dip" android:text="Submit" android:onClick="submit"/> </LinearLayout> </com.wj.myreboundscrollview.customview.MyReboundScrollView>
這里直接在外層包裹實現(xiàn)。注意,因為Myreboundscrollview是繼承自Scrollview,因此要遵循Scrollview的使用原則,里面只能包含一個LinearLayout,所以無論里面多門復(fù)雜的布局,最后我們都要將其包含在一個LinearLayout中。
ok,功能實現(xiàn),效果也演示,具體需要使用直接拿來用就可以。
以上這篇Android仿IOS上拉下拉彈性效果的實例代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Android Location服務(wù)之LocationManager案例詳解
這篇文章主要介紹了Android Location服務(wù)之LocationManager案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Android UI設(shè)計與開發(fā)之實現(xiàn)應(yīng)用程序只啟動一次引導(dǎo)界面
這篇文章主要為大家詳細(xì)介紹了Android UI設(shè)計與開發(fā)之實現(xiàn)應(yīng)用程序只啟動一次引導(dǎo)界面,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
Android Studio 3.1.X中導(dǎo)入項目的正確方法分享
這篇文章主要給大家介紹了關(guān)于Android Studio 3.1.X中導(dǎo)入項目的正確方法,文中一步步將解決的方法以及可能遇到的問題介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
Android 實現(xiàn)圓圈擴(kuò)散水波動畫效果兩種方法
這篇文章主要介紹了Android 實現(xiàn)圓圈擴(kuò)散水波動畫效果兩種方法,需要的朋友可以參考下2018-05-05
Android自定義TextBanner實現(xiàn)自動滾動
這篇文章主要為大家詳細(xì)介紹了Android自定義TextBanner實現(xiàn)自動滾動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-07-07

