Android自定義實現(xiàn)可回彈的ScollView
更新時間:2022年04月18日 14:13:07 作者:清風(fēng)一杯酒
這篇文章主要為大家詳細(xì)介紹了Android自定義實現(xiàn)可回彈的ScollView,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
前言
- 仿IOS回彈效果
- 為了增強用戶體驗,自定義一個可回彈的ScrollView是一個不錯的選擇,而且這種效果還是很簡單的
把原來的ScollView標(biāo)簽替換一下就好了
<?xml version="1.0" encoding="utf-8"?> <com.mycompany.myapp.MyScrollView ? ?xmlns:android="http://schemas.android.com/apk/res/android" ? ?android:layout_height="match_parent" ? ?android:layout_width="match_parent" ? ?android:fillViewport="true"> ? ?<LinearLayout ? ? ? android:layout_height="match_parent" ? ? ? android:layout_width="match_parent" ? ? ? android:gravity="center" ? ? ? android:background="#FFABE346" ? ? ? android:elevation="1dp"> ? ? ? <TextView ? ? ? ? ?android:layout_height="wrap_content" ? ? ? ? ?android:layout_width="wrap_content" ? ? ? ? ?android:text="可回彈的Scollview"/> ? ?</LinearLayout> </com.mycompany.myapp.MyScrollView>
public class MyScrollView extends ScrollView { ? ?private View convertView; ? ?private Rect originalRect=new Rect(); ? ?private int startY,offsetY; ? ?public MyScrollView(Context context) ? ?{ ? ? ? super(context); ? ?} ? ?public MyScrollView(Context context, AttributeSet attrs) ? ?{ ? ? ? super(context, attrs); ? ?} ? ?public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) ? ?{ ? ? ? super(context, attrs, defStyleAttr); ? ?} ? ?@Override ? ?protected void onFinishInflate() ? ?{ ? ? ? super.onFinishInflate(); ? ? ? //獲取子視圖 ? ? ? convertView = getChildAt(0); ? ?} ? ?@Override ? ?protected void onLayout(boolean changed, int l, int t, int r, int b) ? ?{ ? ? ? super.onLayout(changed, l, t, r, b); ? ? ? //記錄原來的位置 ? ? ? originalRect.set(l,t,r,b); ? ?} ? ? ? ? ? ?@Override ? ?public boolean dispatchTouchEvent(MotionEvent ev) ? ?{ ? ? ? switch (ev.getAction()) ? ? ? { ? ? ? ? ?case MotionEvent.ACTION_DOWN: ? ? ? ? ? ? { ? ? ? ? ? ? ? ?//記錄第一次的手指觸摸位置 ? ? ? ? ? ? ? ?startY = (int) ev.getY(); ? ? ? ? ? ? } ? ? ? ? ? ? break; ? ? ? ? ?case MotionEvent.ACTION_MOVE: ? ? ? ? ? ? { ? ? ? ? ? ? ? ?//記錄拖動時的手指觸摸位置 ? ? ? ? ? ? ? ?offsetY = ((int) ev.getY()) - startY; ? ? ? ? ? ? ? ?//讓子視圖跟隨手指拖動 ? ? ? ? ? ? ? ?convertView.layout(originalRect.left,originalRect.top+(int)(offsetY*0.5f) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ,originalRect.right,originalRect.bottom+(int)(offsetY*0.5f)); ? ? ? ? ? ? } ? ? ? ? ? ? break; ? ? ? ? ?case MotionEvent.ACTION_UP: ? ? ? ? ? ? { ? ? ? ? ? ? ? ?//回彈動畫 ? ? ? ? ? ? ? ?TranslateAnimation offsetAnim=new TranslateAnimation(0,0,convertView.getTop(),originalRect.top); ? ? ? ? ? ? ? ?offsetAnim.setDuration(200); ? ? ? ? ? ? ? ?convertView.startAnimation(offsetAnim); ? ? ? ? ? ? ? ?//讓子視圖回到原來的位置 ? ? ? ? ? ? ? ?convertView.layout(originalRect.left,originalRect.top,originalRect.right,originalRect.bottom); ? ? ? ? ? ? } ? ? ? } ? ? ? return super.dispatchTouchEvent(ev); ? ?} }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Android基于reclyview實現(xiàn)列表回彈動畫效果
- Android?ScrollView實現(xiàn)滾動超過邊界松手回彈
- android ScrollView實現(xiàn)水平滑動回彈
- Android實現(xiàn)背景圖滑動變大松開回彈效果
- Android實現(xiàn)橡皮筋回彈和平移縮放效果
- Android自定義View實現(xiàn)豎向滑動回彈效果
- android實現(xiàn)可上下回彈的scrollview
- Android實現(xiàn)回彈ScrollView的原理
- Android ScrollView的頂部下拉和底部上拉回彈效果
- android自定義滾動上下回彈scollView
相關(guān)文章
使用RecyclerView實現(xiàn)點贊頭像疊加效果
這篇文章主要為大家詳細(xì)介紹了使用RecyclerView實現(xiàn)點贊頭像疊加效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-08-08Android編程Widget創(chuàng)建與使用方法簡明教程
這篇文章主要介紹了Android編程Widget創(chuàng)建與使用方法,結(jié)合實例形式分析了Widget的功能、使用方法與相關(guān)注意事項,需要的朋友可以參考下2016-10-10Android 自定義密碼輸入框?qū)崿F(xiàn)代碼
最近做個項目自定義密碼輸入框功能,下面小編把實現(xiàn)思路分享到腳本之家平臺,需要的朋友參考下吧2018-03-03Android使用Jetpack Compose開發(fā)零基礎(chǔ)起步教程
Jetpack Compose是用于構(gòu)建原生Android UI的現(xiàn)代工具包。Jetpack Compose使用更少的代碼,強大的工具和直觀的Kotlin API,簡化并加速了Android上的UI開發(fā)2023-04-04