網站首頁 編程語言 正文
前言
- 仿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
相關推薦
- 2023-07-07 TP6的服務在自定義composer包中如何使用
- 2023-09-12 spring webflux配置成tomcat的線程池
- 2022-09-18 iOS開發探索多線程GCD隊列示例詳解_IOS
- 2022-07-03 Golang之空結構體和零長數組的實踐
- 2022-08-01 Python?OpenCV基于HSV的顏色分割實現示例_python
- 2022-02-18 matplotlib的legend參數與設置
- 2022-08-17 R語言UpSet包實現集合可視化示例詳解_R語言
- 2023-01-08 利用C#實現批量圖片格式轉換功能_C#教程
- 最近更新
-
- 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同步修改后的遠程分支