網(wǎng)站首頁 編程語言 正文
本文實(shí)例為大家分享了Android Scroller實(shí)現(xiàn)彈性滑動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下
首先看下實(shí)現(xiàn)效果,可以看到當(dāng)我們手指松開時(shí)圖片會(huì)逐漸滑動(dòng)到初始位置,而不是直接跳變到中心點(diǎn)。
代碼實(shí)現(xiàn)
當(dāng)手指觸摸到view上時(shí)即TouchEvent位MotionEvent.ACTION_DOWN時(shí),記錄開始的坐標(biāo)位置,同時(shí)由于手指再次按到屏幕上的的時(shí)候view還在執(zhí)行動(dòng)畫,所以當(dāng)動(dòng)畫還在執(zhí)行的時(shí)候我們需要將動(dòng)畫停止。
if (!mScroller.isFinished()) {
? ? mScroller.abortAnimation();
? ? ? ? ? ? ? ? }
mStartX = (int) event.getX();
mStartY = (int) event.getY();
當(dāng)然后當(dāng)用戶手指在屏幕上面滑動(dòng)的時(shí)候,即event為MotionEvent.ACTION_MOVE時(shí),我們需要將view的位置進(jìn)行移動(dòng),這里我使用的是scrollBy的方式移動(dòng)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移動(dòng)位置的時(shí)候前面還有個(gè)mViewGroup呢,因?yàn)槲覀冊谑褂胹crollBy/scrollTo的時(shí)候?qū)嶋H上移動(dòng)的是view中內(nèi)容,所以當(dāng)我們想要移動(dòng)view自身的時(shí)候那么就需要得到該view的parent,然后移動(dòng)parent里面的內(nèi)容,即我們需要移動(dòng)的View,同時(shí)可以看到我們scrollBy方法中對(duì)變化的值取了負(fù)數(shù),這個(gè)由于View內(nèi)部計(jì)算滑動(dòng)距離的兩個(gè)屬性的計(jì)算方式與我們平常使用的剛好相反。
mScrollX用來記錄橫向滾動(dòng)的距離:該屬性的計(jì)算方式為: view左邊緣位置減去view內(nèi)容左邊緣位置
所以當(dāng)我們滑動(dòng)view到右側(cè)的時(shí)候,我們需要對(duì)取變化距離的負(fù)值。
mScrollY用來計(jì)算縱向滾動(dòng)的距離:該屬性的計(jì)算方式為: view上邊緣位置減去view內(nèi)容上邊緣的位置
緊接著當(dāng)用的手指抬起的時(shí)候,即event為MotionEvent.ACTION_UP,我們需要將view平滑移動(dòng)到起始位置。
case MotionEvent.ACTION_UP:
? ? ? ? ? ? ? ? mScroller.startScroll(mViewGroup.getScrollX(), mViewGroup.getScrollY(),
? ? ? ? ? ? ? ? ? ? ? ? -mViewGroup.getScrollX(), -mViewGroup.getScrollY(), 1000);
? ? ? ? ? ? ? ? invalidate();// 在ui線程中調(diào)用
? ? ? ? ? ? ? ? break;
? ? @Override
? ? public void computeScroll() {
? ? ? ? if (mScroller.computeScrollOffset()) {
? ? ? ? ? ? mViewGroup.scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
? ? ? ? ? ? postInvalidate();// 在非ui線程中調(diào)用
? ? ? ? }
? ? }
這里我們使用了scroller進(jìn)行平滑移動(dòng),查看startScroll的源碼,這個(gè)函數(shù)其實(shí)并沒有干什么
該函數(shù)知識(shí)設(shè)置了一些參數(shù),并沒有移動(dòng)view的位置。View的移動(dòng)其實(shí)是由下面的invalidate()觸發(fā)的,因?yàn)閕nvalidate()會(huì)讓view 重繪,重新繪制的時(shí)候會(huì)調(diào)用到view自身的draw()方法,而draw方法又會(huì)調(diào)用到computeScroll()方法,再computeScroll()方法中,我們首先判斷判斷當(dāng)前的移動(dòng)是否結(jié)束,沒有結(jié)束的話通過getCurrX(),getCurrY()移動(dòng)到當(dāng)前動(dòng)畫所在位置,然后再次重新繪制view,然后繼續(xù)調(diào)用draw,繼續(xù)上面的過程,直到scroller結(jié)束即computeScrollOffset()返回false。
完整代碼
使用的時(shí)候只需要將這個(gè)view,放置在xml中,并配置一個(gè)圖片背景
<?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
相關(guān)推薦
- 2022-07-20 初識(shí)C語言習(xí)題以及知識(shí)點(diǎn)
- 2022-12-30 Golang通道channel的源碼分析_Golang
- 2022-07-18 Linux安裝配置nginx
- 2021-11-25 使用Oracle命令進(jìn)行數(shù)據(jù)庫備份與還原_oracle
- 2022-05-10 IDEA中報(bào)錯(cuò) “Error running ‘Application‘: Command line
- 2022-11-02 Python+requests+unittest執(zhí)行接口自動(dòng)化測試詳情_python
- 2022-02-21 小程序頁面跳轉(zhuǎn)如何同時(shí)傳多個(gè)參數(shù)?
- 2022-07-14 python?中的requirements.txt?文件的使用詳情_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支