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

思路:其實(shí)原理很簡單,實(shí)現(xiàn)一個(gè)自定義的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";
//移動(dòng)因子, 是一個(gè)百分比, 比如手指移動(dòng)了100px, 那么View就只移動(dòng)50px
//目的是達(dá)到一個(gè)延遲的效果
private static final float MOVE_FACTOR = 0.5f;
//松開手指后, 界面回到正常位置需要的動(dòng)畫時(shí)間
private static final int ANIM_TIME = 100;
//ScrollView的子View, 也是ScrollView的唯一一個(gè)子View
private View contentView;
//手指按下時(shí)的Y值, 用于在移動(dòng)時(shí)計(jì)算移動(dòng)距離
//如果按下時(shí)不能上拉和下拉, 會(huì)在手指移動(dòng)時(shí)更新為當(dāng)前手指的Y值
private float startY;
//用于記錄正常的布局位置
private Rect originalRect = new Rect();
//手指按下時(shí)記錄是否可以繼續(xù)下拉
private boolean canPullDown = false;
//手指按下時(shí)記錄是否可以繼續(xù)上拉
private boolean canPullUp = false;
//在手指滑動(dòng)的過程中記錄是否移動(dòng)了布局
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中的唯一子控件的位置信息, 這個(gè)位置信息在整個(gè)控件的生命周期中保持不變
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();
//記錄按下時(shí)的Y值
startY = ev.getY();
break;
case MotionEvent.ACTION_UP:
if(!isMoved) break; //如果沒有移動(dòng)布局, 則跳過執(zhí)行
// 開啟動(dòng)畫
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:
//在移動(dòng)的過程中, 既沒有滾動(dòng)到可以上拉的程度, 也沒有滾動(dòng)到可以下拉的程度
if(!canPullDown && !canPullUp) {
startY = ev.getY();
canPullDown = isCanPullDown();
canPullUp = isCanPullUp();
break;
}
//計(jì)算手指移動(dòng)的距離
float nowY = ev.getY();
int deltaY = (int) (nowY - startY);
//是否應(yīng)該移動(dòng)布局
boolean shouldMove =
(canPullDown && deltaY > 0) //可以下拉, 并且手指向下移動(dòng)
|| (canPullUp && deltaY< 0) //可以上拉, 并且手指向上移動(dòng)
|| (canPullUp && canPullDown); //既可以上拉也可以下拉(這種情況出現(xiàn)在ScrollView包裹的控件比ScrollView還?。?
if(shouldMove){
//計(jì)算偏移量
int offset = (int)(deltaY * MOVE_FACTOR);
//隨著手指的移動(dòng)而移動(dòng)布局
contentView.layout(originalRect.left, originalRect.top + offset,
originalRect.right, originalRect.bottom + offset);
isMoved = true; //記錄移動(dòng)了布局
}
break;
default:
break;
}
return super.dispatchTouchEvent(ev);
}
//判斷是否滾動(dòng)到頂部
private boolean isCanPullDown() {
return getScrollY() == 0 ||
contentView.getHeight() < getHeight() + getScrollY();
}
//判斷是否滾動(dòng)到底部
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>
這里直接在外層包裹實(shí)現(xiàn)。注意,因?yàn)镸yreboundscrollview是繼承自Scrollview,因此要遵循Scrollview的使用原則,里面只能包含一個(gè)LinearLayout,所以無論里面多門復(fù)雜的布局,最后我們都要將其包含在一個(gè)LinearLayout中。
ok,功能實(shí)現(xiàn),效果也演示,具體需要使用直接拿來用就可以。
以上這篇Android仿IOS上拉下拉彈性效果的實(shí)例代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
制作獨(dú)立的Android模擬器實(shí)現(xiàn)方法
本文主要介紹如何制作獨(dú)立的Android模擬器,這里給大家提供詳細(xì)的制作流程,有需要的小伙伴可以參考下2016-08-08
Android Location服務(wù)之LocationManager案例詳解
這篇文章主要介紹了Android Location服務(wù)之LocationManager案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
SDL2和OpenGL使用踩坑筆記經(jīng)驗(yàn)分享
今天小編就為大家分享一篇關(guān)于SDL2和OpenGL使用踩坑筆記經(jīng)驗(yàn)分享,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
Android中Spinner控件之鍵值對用法實(shí)例分析
這篇文章主要介紹了Android中Spinner控件之鍵值對用法,實(shí)例分析了Spinner控件控件的鍵值對實(shí)用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
Android UI設(shè)計(jì)與開發(fā)之實(shí)現(xiàn)應(yīng)用程序只啟動(dòng)一次引導(dǎo)界面
這篇文章主要為大家詳細(xì)介紹了Android UI設(shè)計(jì)與開發(fā)之實(shí)現(xiàn)應(yīng)用程序只啟動(dòng)一次引導(dǎo)界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android Studio 3.1.X中導(dǎo)入項(xiàng)目的正確方法分享
這篇文章主要給大家介紹了關(guān)于Android Studio 3.1.X中導(dǎo)入項(xiàng)目的正確方法,文中一步步將解決的方法以及可能遇到的問題介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
Flutter配置代理抓包實(shí)現(xiàn)過程詳解
這篇文章主要為大家介紹了Flutter配置代理抓包實(shí)現(xiàn)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Android 實(shí)現(xiàn)圓圈擴(kuò)散水波動(dòng)畫效果兩種方法
這篇文章主要介紹了Android 實(shí)現(xiàn)圓圈擴(kuò)散水波動(dòng)畫效果兩種方法,需要的朋友可以參考下2018-05-05
Android自定義TextBanner實(shí)現(xiàn)自動(dòng)滾動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android自定義TextBanner實(shí)現(xiàn)自動(dòng)滾動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07

