網站首頁 編程語言 正文
前言
- 仿IOS回彈效果
- 為了增強用戶體驗,自定義一個可回彈的ScrollView是一個不錯的選擇,而且這種效果還是很簡單的
把原來的ScollView標簽替換一下就好了
<?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);
? ?}
}
原文鏈接:https://blog.csdn.net/weixin_45263548/article/details/98437594
相關推薦
- 2022-05-17 qt 解決Error while building/deploying project Hmi (k
- 2022-12-26 詳解C++11中的lambda匿名函數_C 語言
- 2022-06-29 python密碼學庫pynacl功能介紹_python
- 2023-02-18 Framework中實現OC和Swift的混編方案_IOS
- 2022-05-11 Python實現圖書管理系統設計_python
- 2023-03-27 Python中反轉二維數組的行和列問題_python
- 2023-02-09 Rust?所有權機制原理深入剖析_Rust語言
- 2023-07-31 el-tree自定義展示圖標
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支