欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android?Scroller實現(xiàn)彈性滑動效果

 更新時間:2022年04月18日 16:16:26   作者:15130140362  
這篇文章主要為大家詳細(xì)介紹了Android?Scroller實現(xiàn)彈性滑動效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android Scroller實現(xiàn)彈性滑動的具體代碼,供大家參考,具體內(nèi)容如下

首先看下實現(xiàn)效果,可以看到當(dāng)我們手指松開時圖片會逐漸滑動到初始位置,而不是直接跳變到中心點(diǎn)。

代碼實現(xiàn)

當(dāng)手指觸摸到view上時即TouchEvent位MotionEvent.ACTION_DOWN時,記錄開始的坐標(biāo)位置,同時由于手指再次按到屏幕上的的時候view還在執(zhí)行動畫,所以當(dāng)動畫還在執(zhí)行的時候我們需要將動畫停止。

if (!mScroller.isFinished()) {
? ? mScroller.abortAnimation();
? ? ? ? ? ? ? ? }
mStartX = (int) event.getX();
mStartY = (int) event.getY();

當(dāng)然后當(dāng)用戶手指在屏幕上面滑動的時候,即event為MotionEvent.ACTION_MOVE時,我們需要將view的位置進(jìn)行移動,這里我使用的是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的時候?qū)嶋H上移動的是view中內(nèi)容,所以當(dāng)我們想要移動view自身的時候那么就需要得到該view的parent,然后移動parent里面的內(nèi)容,即我們需要移動的View,同時可以看到我們scrollBy方法中對變化的值取了負(fù)數(shù),這個由于View內(nèi)部計算滑動距離的兩個屬性的計算方式與我們平常使用的剛好相反。

mScrollX用來記錄橫向滾動的距離:該屬性的計算方式為: view左邊緣位置減去view內(nèi)容左邊緣位置

所以當(dāng)我們滑動view到右側(cè)的時候,我們需要對取變化距離的負(fù)值。

mScrollY用來計算縱向滾動的距離:該屬性的計算方式為: view上邊緣位置減去view內(nèi)容上邊緣的位置

緊接著當(dāng)用的手指抬起的時候,即event為MotionEvent.ACTION_UP,我們需要將view平滑移動到起始位置。

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)行平滑移動,查看startScroll的源碼,這個函數(shù)其實并沒有干什么

該函數(shù)知識設(shè)置了一些參數(shù),并沒有移動view的位置。View的移動其實是由下面的invalidate()觸發(fā)的,因為invalidate()會讓view 重繪,重新繪制的時候會調(diào)用到view自身的draw()方法,而draw方法又會調(diào)用到computeScroll()方法,再computeScroll()方法中,我們首先判斷判斷當(dāng)前的移動是否結(jié)束,沒有結(jié)束的話通過getCurrX(),getCurrY()移動到當(dāng)前動畫所在位置,然后再次重新繪制view,然后繼續(xù)調(diào)用draw,繼續(xù)上面的過程,直到scroller結(jié)束即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();
? ? ? ? }
? ? }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論