Android實(shí)現(xiàn)回彈ScrollView的原理
本文實(shí)例為大家分享了Android實(shí)現(xiàn)回彈ScrollView的原理,供大家參考,具體內(nèi)容如下
回彈的ScrollView
網(wǎng)上看到的通常是ElasticScrollView,
有一個(gè)Bug:點(diǎn)擊子控件滑動(dòng)時(shí),滑動(dòng)無(wú)效,
所以針對(duì)此問題,我對(duì)ElasticScrollView做了改進(jìn)。
原理圖


代碼
我在注釋中做了詳細(xì)的說明
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ScrollView;
/**
?* Created by Tindle Wei.
?*/
public class ElasticScrollView extends ScrollView {
? ? /**
? ? ?* 手指抖動(dòng)誤差
? ? ?*/
? ? private static final int SHAKE_MOVE_VALUE = 8;
? ? /**
? ? ?* Scrollview內(nèi)部的view
? ? ?*/
? ? private View innerView;
? ? /**
? ? ?* 記錄innerView最初的Y位置
? ? ?*/
? ? private float startY;
? ? /**
? ? ?* 記錄原始innerView的大小位置
? ? ?*/
? ? private Rect outRect = new Rect();
? ? private boolean animationFinish = true;
? ? public ElasticScrollView(Context context) {
? ? ? ? super(context);
? ? }
? ? public ElasticScrollView(Context context, AttributeSet attrs) {
? ? ? ? super(context, attrs);
? ? }
? ? /**
? ? ?* 繼承自View
? ? ?* 在xml的所有布局加載完之后執(zhí)行
? ? ?*/
? ? @Override
? ? protected void onFinishInflate() {
? ? ? ? if (getChildCount() > 0) {
? ? ? ? ? ? innerView = getChildAt(0);
? ? ? ? }
? ? }
? ? /**
? ? ?* 繼承自ViewGroup
? ? ?* 返回true, 截取觸摸事件
? ? ?* 返回false, 將事件傳遞給onTouchEvent()和子控件的dispatchTouchEvent()
? ? ?*/
? ? @Override
? ? public boolean onInterceptTouchEvent(MotionEvent ev) {
? ? ? ? // 判斷 點(diǎn)擊子控件 or 按住子控件滑動(dòng)
? ? ? ? // 如果點(diǎn)擊子控件,則返回 false, 子控件響應(yīng)點(diǎn)擊事件
? ? ? ? // 如果按住子控件滑動(dòng),則返回 true, 滑動(dòng)布局
? ? ? ? switch (ev.getAction()) {
? ? ? ? ? ? case MotionEvent.ACTION_DOWN: {
? ? ? ? ? ? ? ? startY = ev.getY();
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? case MotionEvent.ACTION_MOVE: {
? ? ? ? ? ? ? ? float currentY = ev.getY();
? ? ? ? ? ? ? ? float scrollY = currentY - startY;
? ? ? ? ? ? ? ? // 是否返回 true
? ? ? ? ? ? ? ? return Math.abs(scrollY) > SHAKE_MOVE_VALUE;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? // 默認(rèn)返回 false
? ? ? ? return super.onInterceptTouchEvent(ev);
? ? }
? ? @Override
? ? public boolean onTouchEvent(MotionEvent ev) {
? ? ? ? if (innerView == null) {
? ? ? ? ? ? return super.onTouchEvent(ev);
? ? ? ? } else {
? ? ? ? ? ? myTouchEvent(ev);
? ? ? ? }
? ? ? ? return super.onTouchEvent(ev);
? ? }
? ? public void myTouchEvent(MotionEvent ev) {
? ? ? ? if (animationFinish) {
? ? ? ? ? ? switch (ev.getAction()) {
? ? ? ? ? ? ? ? case MotionEvent.ACTION_DOWN:
? ? ? ? ? ? ? ? ? ? startY = ev.getY();
? ? ? ? ? ? ? ? ? ? super.onTouchEvent(ev);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case MotionEvent.ACTION_UP:
? ? ? ? ? ? ? ? ? ? startY = 0;
? ? ? ? ? ? ? ? ? ? if (isNeedAnimation()) {
? ? ? ? ? ? ? ? ? ? ? ? animation();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? super.onTouchEvent(ev);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case MotionEvent.ACTION_MOVE:
? ? ? ? ? ? ? ? ? ? final float preY =?
? ? ? ? ? ? ? ? ? ? ? ? ? startY == 0 ? ev.getY() : startY;
? ? ? ? ? ? ? ? ? ? float nowY = ev.getY();
? ? ? ? ? ? ? ? ? ? int deltaY = (int) (preY - nowY);
? ? ? ? ? ? ? ? ? ? startY = nowY;
? ? ? ? ? ? ? ? ? ? // 當(dāng)滾動(dòng)到最上或者最下時(shí)就不會(huì)再滾動(dòng),這時(shí)移動(dòng)布局
? ? ? ? ? ? ? ? ? ? if (isNeedMove()) {
? ? ? ? ? ? ? ? ? ? ? ? if (outRect.isEmpty()) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? // 保存正常的布局位置
? ? ? ? ? ? ? ? ? ? ? ? ? ? outRect.set(innerView
? ? ? ? ? ? ? ? ? ? ? ? ? ? .getLeft(), ?innerView.getTop(),?
? ? ? ? ? ? ? ? ? ? ? ? ? ? innerView.getRight(),?
? ? ? ? ? ? ? ? ? ? ? ? ? ? innerView.getBottom());
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? // 移動(dòng)布局
? ? ? ? ? ? ? ? ? ? ? ? // 這里 deltaY/2 為了操作體驗(yàn)更好
? ? ? ? ? ? ? ? ? ? ? ? innerView.layout(innerView.getLeft(), ?
? ? ? ? ? ? ? ? ? ? ? ? innerView.getTop() - deltaY / 2, ?
? ? ? ? ? ? ? ? ? ? ? ? innerView.getRight(), ?
? ? ? ? ? ? ? ? ? ? ? ? innerView.getBottom() - deltaY / 2);
? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? super.onTouchEvent(ev);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? /**
? ? ?* 開啟移動(dòng)動(dòng)畫
? ? ?*/
? ? public void animation() {
? ? ? ? TranslateAnimation ta = new TranslateAnimation(0, 0, 0, outRect.top - innerView.getTop());
? ? ? ? ta.setDuration(400);
? ? ? ? // 減速變化 為了用戶體驗(yàn)更好
? ? ? ? ta.setInterpolator(new DecelerateInterpolator());
? ? ? ? ta.setAnimationListener(new Animation.AnimationListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onAnimationStart(Animation animation) {
? ? ? ? ? ? ? ? animationFinish = false;
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onAnimationRepeat(Animation animation) {
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onAnimationEnd(Animation animation) {
? ? ? ? ? ? ? ? innerView.clearAnimation();
? ? ? ? ? ? ? ? // 設(shè)置innerView回到正常的布局位置
? ? ? ? ? ? ? ? innerView.layout(outRect.left,?
? ? ? ? ? ? ? ? outRect.top, outRect.right, outRect.bottom);
? ? ? ? ? ? ? ? outRect.setEmpty();
? ? ? ? ? ? ? ? animationFinish = true;
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? innerView.startAnimation(ta);
? ? }
? ? /**
? ? ?* 是否需要開啟動(dòng)畫
? ? ?*/
? ? public boolean isNeedAnimation() {
? ? ? ? return !outRect.isEmpty();
? ? }
? ? /**
? ? ?* 是否需要移動(dòng)布局
? ? ?*/
? ? public boolean isNeedMove() {
? ? ? ? int offset = innerView.getMeasuredHeight() - getHeight();
? ? ? ? offset = (offset < 0) ? 0: offset;
? ? ? ? int scrollY = getScrollY();
? ? ? ? return (offset == 0 || scrollY == offset);
? ? }
}其他說明
1、下面是繼承關(guān)系:ElasticScrollView extends ScrollViewScrollView extends FrameLayoutFrameLayout extends ViewGroupViewGroup extends View
2、解決子控件 截取滑動(dòng)監(jiān)聽的代碼在onInterceptTouchEvent() ,
通過監(jiān)聽Y的變化,來(lái)判斷是點(diǎn)擊子控件還是上拉下拉
3、getMeasuredHeight()返回的是原始測(cè)量高度,與屏幕無(wú)關(guān),getHeight()返回的是在屏幕上顯示的高度。
實(shí)際上在當(dāng)屏幕可以包裹內(nèi)容的時(shí)候,他們的值是相等的,只有當(dāng)view超出屏幕后,才能看出他們的區(qū)別。當(dāng)超出屏幕后,getMeasuredHeight()等于getHeight()加上屏幕之外沒有顯示的高度。
4、getScrollY()返回的是滑動(dòng)View顯示部分的頂部
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android基于reclyview實(shí)現(xiàn)列表回彈動(dòng)畫效果
- Android?ScrollView實(shí)現(xiàn)滾動(dòng)超過邊界松手回彈
- android ScrollView實(shí)現(xiàn)水平滑動(dòng)回彈
- Android實(shí)現(xiàn)背景圖滑動(dòng)變大松開回彈效果
- Android實(shí)現(xiàn)橡皮筋回彈和平移縮放效果
- Android自定義View實(shí)現(xiàn)豎向滑動(dòng)回彈效果
- android實(shí)現(xiàn)可上下回彈的scrollview
- Android自定義實(shí)現(xiàn)可回彈的ScollView
- Android ScrollView的頂部下拉和底部上拉回彈效果
- android自定義滾動(dòng)上下回彈scollView
相關(guān)文章
Android使用自定義View實(shí)現(xiàn)餅狀圖的實(shí)例代碼
這篇文章主要介紹了Android使用自定義View實(shí)現(xiàn)餅狀圖的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-05-05
Android系統(tǒng)添加Linux驅(qū)動(dòng)
今天小編就為大家分享一篇關(guān)于Android系統(tǒng)添加Linux驅(qū)動(dòng)的文章,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10
Flutter 構(gòu)建一個(gè)常用的頁(yè)面框架
大多數(shù) App 中都會(huì)有底部導(dǎo)航欄,通過底部導(dǎo)航欄切換實(shí)現(xiàn)不同頁(yè)面之間的切換。在Flutter 中提供了 BottomNavigationBar組件實(shí)現(xiàn)底部導(dǎo)航。本篇介紹通過 BottomNavigationBar和 IndexedStack構(gòu)建最為常見的 App 頁(yè)面框架。2021-05-05
詳解Android廣播Broadcast的啟動(dòng)流程
這篇文章主要為大家介紹了Android廣播Broadcast啟動(dòng)流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Android用tabhost實(shí)現(xiàn) 界面切換,每個(gè)界面為一個(gè)獨(dú)立的activity操作
這篇文章主要介紹了Android用tabhost實(shí)現(xiàn) 界面切換,每個(gè)界面為一個(gè)獨(dú)立的activity操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-09-09

