網站首頁 編程語言 正文
本文實例為大家分享了Android Scroller實現彈性滑動的具體代碼,供大家參考,具體內容如下
首先看下實現效果,可以看到當我們手指松開時圖片會逐漸滑動到初始位置,而不是直接跳變到中心點。
代碼實現
當手指觸摸到view上時即TouchEvent位MotionEvent.ACTION_DOWN時,記錄開始的坐標位置,同時由于手指再次按到屏幕上的的時候view還在執行動畫,所以當動畫還在執行的時候我們需要將動畫停止。
if (!mScroller.isFinished()) {
? ? mScroller.abortAnimation();
? ? ? ? ? ? ? ? }
mStartX = (int) event.getX();
mStartY = (int) event.getY();
當然后當用戶手指在屏幕上面滑動的時候,即event為MotionEvent.ACTION_MOVE時,我們需要將view的位置進行移動,這里我使用的是scrollBy的方式移動view的位置。
int curX = (int) event.getX();
int curY = (int) event.getY();
Log.i(TAG, "onTouchEvent: curX" + curX + "curY" + curY);
int delX = curX - mStartX;
int delY = curY - mStartY;
mStartX = curX;
mStartY = curY;
mViewGroup.scrollBy(-delX, -delY);
為什么使用scrollBy移動位置的時候前面還有個mViewGroup呢,因為我們在使用scrollBy/scrollTo的時候實際上移動的是view中內容,所以當我們想要移動view自身的時候那么就需要得到該view的parent,然后移動parent里面的內容,即我們需要移動的View,同時可以看到我們scrollBy方法中對變化的值取了負數,這個由于View內部計算滑動距離的兩個屬性的計算方式與我們平常使用的剛好相反。
mScrollX用來記錄橫向滾動的距離:該屬性的計算方式為: view左邊緣位置減去view內容左邊緣位置
所以當我們滑動view到右側的時候,我們需要對取變化距離的負值。
mScrollY用來計算縱向滾動的距離:該屬性的計算方式為: view上邊緣位置減去view內容上邊緣的位置
緊接著當用的手指抬起的時候,即event為MotionEvent.ACTION_UP,我們需要將view平滑移動到起始位置。
case MotionEvent.ACTION_UP:
? ? ? ? ? ? ? ? mScroller.startScroll(mViewGroup.getScrollX(), mViewGroup.getScrollY(),
? ? ? ? ? ? ? ? ? ? ? ? -mViewGroup.getScrollX(), -mViewGroup.getScrollY(), 1000);
? ? ? ? ? ? ? ? invalidate();// 在ui線程中調用
? ? ? ? ? ? ? ? break;
? ? @Override
? ? public void computeScroll() {
? ? ? ? if (mScroller.computeScrollOffset()) {
? ? ? ? ? ? mViewGroup.scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
? ? ? ? ? ? postInvalidate();// 在非ui線程中調用
? ? ? ? }
? ? }
這里我們使用了scroller進行平滑移動,查看startScroll的源碼,這個函數其實并沒有干什么
該函數知識設置了一些參數,并沒有移動view的位置。View的移動其實是由下面的invalidate()觸發的,因為invalidate()會讓view 重繪,重新繪制的時候會調用到view自身的draw()方法,而draw方法又會調用到computeScroll()方法,再computeScroll()方法中,我們首先判斷判斷當前的移動是否結束,沒有結束的話通過getCurrX(),getCurrY()移動到當前動畫所在位置,然后再次重新繪制view,然后繼續調用draw,繼續上面的過程,直到scroller結束即computeScrollOffset()返回false。
完整代碼
使用的時候只需要將這個view,放置在xml中,并配置一個圖片背景
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? tools:context=".Main2Activity"> ? ? <com.example.recyclerviewlearn.CustomizeImageView ? ? ? ? android:layout_centerInParent="true" ? ? ? ? android:background="@drawable/ic_launcher_background" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content"/> </RelativeLayout>
下面是自定義ImageView的代碼
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.Scroller;
public class CustomizeImageView extends androidx.appcompat.widget.AppCompatImageView {
? ? private static final String TAG = "CustomizeImageView";
? ? private ViewGroup mViewGroup;
? ? private int mStartX = 0;
? ? private int mStartY = 0;
? ? private Scroller mScroller = new Scroller(this.getContext());
? ? public CustomizeImageView(Context context) {
? ? ? ? super(context);
? ? }
? ? public CustomizeImageView(Context context, AttributeSet attrs) {
? ? ? ? super(context, attrs);
? ? }
? ? public CustomizeImageView(Context context, AttributeSet attrs, int defStyleAttr) {
? ? ? ? super(context, attrs, defStyleAttr);
? ? }
? ? @Override
? ? protected void onAttachedToWindow() {
? ? ? ? super.onAttachedToWindow();
? ? ? ? mViewGroup = (ViewGroup) getParent();
? ? }
? ? @Override
? ? public boolean onTouchEvent(MotionEvent event) {
? ? ? ? Log.i(TAG, "onTouchEvent: ");
? ? ? ? switch (event.getAction()) {
? ? ? ? ? ? case MotionEvent.ACTION_DOWN:
? ? ? ? ? ? ? ? if (!mScroller.isFinished()) {
? ? ? ? ? ? ? ? ? ? mScroller.abortAnimation();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? mStartX = (int) event.getX();
? ? ? ? ? ? ? ? mStartY = (int) event.getY();
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case MotionEvent.ACTION_MOVE:
? ? ? ? ? ? ? ? Log.i(TAG, "onTouchEvent: startX" + mStartX + "mStartY" + mStartY);
? ? ? ? ? ? ? ? int curX = (int) event.getX();
? ? ? ? ? ? ? ? int curY = (int) event.getY();
? ? ? ? ? ? ? ? Log.i(TAG, "onTouchEvent: curX" + curX + "curY" + curY);
? ? ? ? ? ? ? ? int delX = curX - mStartX;
? ? ? ? ? ? ? ? int delY = curY - mStartY;
? ? ? ? ? ? ? ? mStartX = curX;
? ? ? ? ? ? ? ? mStartY = curY;
? ? ? ? ? ? ? ? Log.i(TAG, "onTouchEvent: ACTION_MOVE");
? ? ? ? ? ? ? ? mViewGroup.scrollBy(-delX, -delY);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case MotionEvent.ACTION_UP:
? ? ? ? ? ? ? ? mScroller.startScroll(mViewGroup.getScrollX(), mViewGroup.getScrollY(),
? ? ? ? ? ? ? ? ? ? ? ? -mViewGroup.getScrollX(), -mViewGroup.getScrollY(), 1000);
? ? ? ? ? ? ? ? invalidate();
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? default:
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? return true;
? ? }
? ? @Override
? ? public void computeScroll() {
? ? ? ? if (mScroller.computeScrollOffset()) {
? ? ? ? ? ? mViewGroup.scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
? ? ? ? ? ? invalidate();
? ? ? ? }
? ? }
}
原文鏈接:https://blog.csdn.net/liu_12345_liu/article/details/107297224
相關推薦
- 2022-03-15 antd-mobile 請求時Loading組件
- 2024-03-08 SpringBoot 項目啟動報錯 Failed to configure a DataSource
- 2022-06-14 Pycharm安裝第三方庫的超詳細步驟_python
- 2022-08-22 python深度學習tensorflow1.0參數初始化initializer_python
- 2022-01-25 項目啟動的時候報Exception in thread main 錯誤解決方法
- 2024-07-18 Spring Security之配置體系
- 2022-06-13 C語言strlen函數實現讀取字符串長度詳解_C 語言
- 2022-06-12 Redis高并發場景下秒殺超賣解決方案(秒殺場景)_Redis
- 最近更新
-
- 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同步修改后的遠程分支