日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android?Scroller實現彈性滑動效果_Android

作者:15130140362 ? 更新時間: 2022-06-18 編程語言

本文實例為大家分享了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

欄目分類
最近更新