Android實現(xiàn)橡皮筋回彈和平移縮放效果
本文實例為大家分享了Android實現(xiàn)橡皮筋回彈和平移縮放的具體代碼,供大家參考,具體內(nèi)容如下
前言
由于最近在做一個view的平移縮放功能以及橡皮筋效果,不過網(wǎng)上查到的大多數(shù)都是分開實現(xiàn)的,所以我這里把這兩種功能整合到了一起
代碼實現(xiàn)
這里我寫把效果分開來寫,最后再合并
平移、縮放
mLayout.java
import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.FrameLayout; import android.widget.Scroller; /** ?* Created by ChenZehao ?* on 2019/8/4 ?*/ public class mLayout extends FrameLayout{ ? ? // 屬性變量 ? ? private float translationX; // 移動X ? ? private float translationY; // 移動Y ? ? private float scale = 1; // 伸縮比例 ? ? // 移動過程中臨時變量 ? ? private float actionX; ? ? private float actionY; ? ? private float spacing; ? ? private int moveType; // 0=未選擇,1=拖動,2=縮放 ? ? private float firstX; ? ? private float firstY; ? ? public mLayout(Context context) { ? ? ? ? this(context, null); ? ? } ? ? public mLayout(Context context, AttributeSet attrs) { ? ? ? ? this(context, attrs, 0); ? ? } ? ? public mLayout(Context context, AttributeSet attrs, int defStyleAttr) { ? ? ? ? super(context, attrs, defStyleAttr); ? ? } ? ? @Override ? ? public boolean onTouchEvent(MotionEvent event) { ? ? ? ? super.onTouchEvent(event); ? ? ? ? switch (event.getAction() & MotionEvent.ACTION_MASK) { ? ? ? ? ? ? case MotionEvent.ACTION_DOWN: ? ? ? ? ? ? ? ? moveType = 1; ? ? ? ? ? ? ? ? actionX = event.getRawX(); ? ? ? ? ? ? ? ? actionY = event.getRawY(); ? ? ? ? ? ? ? ? firstX = actionX; ? ? ? ? ? ? ? ? firstY = actionY; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_POINTER_DOWN: ? ? ? ? ? ? ? ? moveType = 2; ? ? ? ? ? ? ? ? spacing = getSpacing(event); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_MOVE: ? ? ? ? ? ? ? ? if (moveType == 1) { ? ? ? ? ? ? ? ? ? ? translationX = translationX + event.getRawX() - actionX; ? ? ? ? ? ? ? ? ? ? translationY = translationY + event.getRawY() - actionY; ? ? ? ? ? ? ? ? ? ? System.out.println(); ? ? ? ? ? ? ? ? ? ? setTranslationX(translationX); ? ? ? ? ? ? ? ? ? ? setTranslationY(translationY); ? ? ? ? ? ? ? ? ? ? actionX = event.getRawX(); ? ? ? ? ? ? ? ? ? ? actionY = event.getRawY(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else if (moveType == 2) { ? ? ? ? ? ? ? ? ? ? scale = scale * getSpacing(event) / spacing; ? ? ? ? ? ? ? ? ? ? if(scale >= 1){ ? ? ? ? ? ? ? ? ? ? ? ? setScaleX(scale); ? ? ? ? ? ? ? ? ? ? ? ? setScaleY(scale); ? ? ? ? ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? ? ? ? ? scale = 1; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_UP: ? ? ? ? ? ? case MotionEvent.ACTION_POINTER_UP: ? ? ? ? ? ? ? ? moveType = 0; ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? return true; ? ? } ? ? // 觸碰兩點間距離 ? ? private float getSpacing(MotionEvent event) { ? ? ? ? //通過三角函數(shù)得到兩點間的距離 ? ? ? ? float x = event.getX(0) - event.getX(1); ? ? ? ? float y = event.getY(0) - event.getY(1); ? ? ? ? return (float) Math.sqrt(x * x + y * y); ? ? } }
橡皮筋回彈
import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.FrameLayout; import android.widget.Scroller; /** ?* Created by ChenZehao ?* on 2019/8/4 ?*/ public class mLayout extends FrameLayout{ ? ? //系數(shù)可自己更改 ? ? private static final float DEFAULT_FATOR = 0.4f; ? ? /** ? ? ?* 阻尼因子 ? ? ?*/ ? ? private float mFator = DEFAULT_FATOR; ? ? private Scroller mScroller; ? ? /** ? ? ?* 記錄上一次觸摸事件 ? ? ?*/ ? ? private MotionEvent mLastMotionEvent; ? ? public mLayout(Context context) { ? ? ? ? this(context, null); ? ? } ? ? public mLayout(Context context, AttributeSet attrs) { ? ? ? ? this(context, attrs, 0); ? ? } ? ? public mLayout(Context context, AttributeSet attrs, int defStyleAttr) { ? ? ? ? super(context, attrs, defStyleAttr); ? ? ? ? mScroller = new Scroller(context); ? ? } ? ? @Override ? ? public boolean onTouchEvent(MotionEvent event) { ? ? ? ? super.onTouchEvent(event); ? ? ? ? switch (event.getAction()) { ? ? ? ? ? ? case MotionEvent.ACTION_DOWN: ? ? ? ? ? ? ? ? mLastMotionEvent = MotionEvent.obtain(event); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_MOVE: ? ? ? ? ? ? ? ? int dx = (int) (event.getRawX() - mLastMotionEvent.getRawX()); ? ? ? ? ? ? ? ? int dy = (int) (event.getRawY() - mLastMotionEvent.getRawY()); ? ? ? ? ? ? ? ? //如果不想對四個方向增加阻尼效果,直接刪除即可 ? ? ? ? ? ? ? ? //向上平移 ? ? ? ? ? ? ? ? if ((Math.abs(dx) < Math.abs(dy)) && dy < 0){ ? ? ? ? ? ? ? ? ? ? smoothScrollBy(0, -(int) (dy * mFator)); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? //向下平移 ? ? ? ? ? ? ? ? else if (Math.abs(dx) < Math.abs(dy) && dy > 0) { ? ? ? ? ? ? ? ? ? ? smoothScrollBy(0, -(int) (dy * mFator)); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? //向左平移 ? ? ? ? ? ? ? ? else if (Math.abs(dx) > Math.abs(dy) && dx < 0){ ? ? ? ? ? ? ? ? ? ? smoothScrollBy(-(int) (dx * mFator), 0); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? //向右平移 ? ? ? ? ? ? ? ? else if (Math.abs(dx) > Math.abs(dy) && dx > 0){ ? ? ? ? ? ? ? ? ? ? smoothScrollBy(-(int) (dx * mFator), 0); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? mLastMotionEvent = MotionEvent.obtain(event); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_UP: ? ? ? ? ? ? case MotionEvent.ACTION_CANCEL: ? ? ? ? ? ? ? ? smoothScrollTo(0, 0); ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? return true; ? ? } ? ? private void smoothScrollBy(int dx, int dy) { ? ? ? ? mScroller.startScroll(mScroller.getFinalX(), mScroller.getFinalY(), dx, dy); ? ? ? ? invalidate(); ? ? } ? ? private void smoothScrollTo(int fx, int fy) { ? ? ? ? int dx = fx - mScroller.getFinalX(); ? ? ? ? int dy = fx - mScroller.getFinalY(); ? ? ? ? smoothScrollBy(dx, dy); ? ? } ? ? @Override ? ? public void computeScroll() { ? ? ? ? if (mScroller.computeScrollOffset()) { ? ? ? ? ? ? scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); ? ? ? ? ? ? postInvalidate(); ? ? ? ? } ? ? ? ? super.computeScroll(); ? ? } }
平移、縮放、阻尼效果合并
import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.FrameLayout; import android.widget.Scroller; /** ?* Created by ChenZehao ?* on 2019/8/4 ?*/ public class mLayout extends FrameLayout{ ? ? private float scale = 1; // 伸縮比例 ? ? // 移動過程中臨時變量 ? ? private float actionX; ? ? private float actionY; ? ? private float spacing; ? ? private int moveType; // 0=未選擇,1=拖動,2=縮放 ? ? private float firstX; ? ? private float firstY; ? ? //系數(shù)可自己更改 ? ? private static final float DEFAULT_FATOR = 0.4f; ? ? /** ? ? ?* 阻尼因子 ? ? ?*/ ? ? private float mFator = DEFAULT_FATOR; ? ? private Scroller mScroller; ? ? /** ? ? ?* 記錄上一次觸摸事件 ? ? ?*/ ? ? private MotionEvent mLastMotionEvent; ? ? public mLayout(Context context) { ? ? ? ? this(context, null); ? ? } ? ? public mLayout(Context context, AttributeSet attrs) { ? ? ? ? this(context, attrs, 0); ? ? } ? ? public mLayout(Context context, AttributeSet attrs, int defStyleAttr) { ? ? ? ? super(context, attrs, defStyleAttr); ? ? ? ? mScroller = new Scroller(context); ? ? } ? ? @Override ? ? public boolean onTouchEvent(MotionEvent event) { ? ? ? ? super.onTouchEvent(event); ? ? ? ? switch (event.getAction() & MotionEvent.ACTION_MASK) { ? ? ? ? ? ? case MotionEvent.ACTION_DOWN: ? ? ? ? ? ? ? ? mLastMotionEvent = MotionEvent.obtain(event); ? ? ? ? ? ? ? ? moveType = 1; ? ? ? ? ? ? ? ? actionX = event.getRawX(); ? ? ? ? ? ? ? ? actionY = event.getRawY(); ? ? ? ? ? ? ? ? firstX = actionX; ? ? ? ? ? ? ? ? firstY = actionY; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_POINTER_DOWN: ? ? ? ? ? ? ? ? moveType = 2; ? ? ? ? ? ? ? ? spacing = getSpacing(event); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_MOVE: ? ? ? ? ? ? ? ? if (moveType == 1) { ? ? ? ? ? ? ? ? ? ? int dx = (int) (event.getRawX() - mLastMotionEvent.getRawX()); ? ? ? ? ? ? ? ? ? ? int dy = (int) (event.getRawY() - mLastMotionEvent.getRawY()); ? ? ? ? ? ? ? ? ? ? //如果不想對四個方向增加阻尼效果,直接刪除即可 ? ? ? ? ? ? ? ? ? ? //向上平移 ? ? ? ? ? ? ? ? ? ? if ((Math.abs(dx) < Math.abs(dy)) && dy < 0){ ? ? ? ? ? ? ? ? ? ? ? ? smoothScrollBy(0, -(int) (dy * mFator)); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? //向下平移 ? ? ? ? ? ? ? ? ? ? else if (Math.abs(dx) < Math.abs(dy) && dy > 0) { ? ? ? ? ? ? ? ? ? ? ? ? smoothScrollBy(0, -(int) (dy * mFator)); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? //向左平移 ? ? ? ? ? ? ? ? ? ? else if (Math.abs(dx) > Math.abs(dy) && dx < 0){ ? ? ? ? ? ? ? ? ? ? ? ? smoothScrollBy(-(int) (dx * mFator), 0); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? //向右平移 ? ? ? ? ? ? ? ? ? ? else if (Math.abs(dx) > Math.abs(dy) && dx > 0){ ? ? ? ? ? ? ? ? ? ? ? ? smoothScrollBy(-(int) (dx * mFator), 0); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? mLastMotionEvent = MotionEvent.obtain(event); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else if (moveType == 2) { ? ? ? ? ? ? ? ? ? ? scale = scale * getSpacing(event) / spacing; ? ? ? ? ? ? ? ? ? ? if(scale >= 1){ ? ? ? ? ? ? ? ? ? ? ? ? setScaleX(scale); ? ? ? ? ? ? ? ? ? ? ? ? setScaleY(scale); ? ? ? ? ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? ? ? ? ? scale = 1; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_UP: ? ? ? ? ? ? case MotionEvent.ACTION_POINTER_UP: ? ? ? ? ? ? case MotionEvent.ACTION_CANCEL: ? ? ? ? ? ? ? ? moveType = 0; ? ? ? ? ? ? ? ? if(scale == 1) ? ? ? ? ? ? ? ? ? ? smoothScrollTo(0, 0); ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? return true; ? ? } ? ? private void smoothScrollBy(int dx, int dy) { ? ? ? ? mScroller.startScroll(mScroller.getFinalX(), mScroller.getFinalY(), dx, dy); ? ? ? ? invalidate(); ? ? } ? ? private void smoothScrollTo(int fx, int fy) { ? ? ? ? int dx = fx - mScroller.getFinalX(); ? ? ? ? int dy = fx - mScroller.getFinalY(); ? ? ? ? smoothScrollBy(dx, dy); ? ? } ? ? @Override ? ? public void computeScroll() { ? ? ? ? if (mScroller.computeScrollOffset()) { ? ? ? ? ? ? scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); ? ? ? ? ? ? postInvalidate(); ? ? ? ? } ? ? ? ? super.computeScroll(); ? ? } ? ? // 觸碰兩點間距離 ? ? private float getSpacing(MotionEvent event) { ? ? ? ? //通過三角函數(shù)得到兩點間的距離 ? ? ? ? float x = event.getX(0) - event.getX(1); ? ? ? ? float y = event.getY(0) - event.getY(1); ? ? ? ? return (float) Math.sqrt(x * x + y * y); ? ? } }
使用方法
在xml文件中添加mLayout布局,便可對mLayout里面的控件和布局進行平移、縮放、阻尼效果的操作
功能擴展——在布局中添加button
如果我們在mLayout布局中添加button,那么會出現(xiàn)獲取焦點沖突的問題,導(dǎo)致觸摸到按鈕時無法進行平移等操作,因此我們需要重寫button的dispatchTouchEvent函數(shù),因此要創(chuàng)建一個類mButton來繼承Button
點擊時事件被button獲取,因此要將事件通過dispatchTouchEvent回傳給父view,再調(diào)用父view的onInterceptTouchEvent函數(shù)對攔截到的事件進行處理。
代碼如下:
mButton.java
import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; public class mButton extends android.support.v7.widget.AppCompatButton { ? ? public mButton(Context context) { ? ? ? ? super(context); ? ? } ? ? public mButton(Context context, AttributeSet attrs) { ? ? ? ? super(context, attrs); ? ? } ? ? public mButton(Context context, AttributeSet attrs, int defStyleAttr) { ? ? ? ? super(context, attrs, defStyleAttr); ? ? } ? ? @Override ? ? public boolean dispatchTouchEvent(MotionEvent ev) { ? ? ? ? switch (ev.getAction()) { ? ? ? ? ? ? case MotionEvent.ACTION_DOWN: ? ? ? ? ? ? ? ? getParent().requestDisallowInterceptTouchEvent(false); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_MOVE: ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_UP: ? ? ? ? ? ? case MotionEvent.ACTION_POINTER_UP: ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? return super.dispatchTouchEvent(ev); ? ? } }
mLayout.java
import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.FrameLayout; import android.widget.Scroller; /** ?* Created by ChenZehao ?* on 2019/8/4 ?*/ public class mLayout extends FrameLayout{ ? ? private float scale = 1; // 伸縮比例 ? ? // 移動過程中臨時變量 ? ? private float actionX; ? ? private float actionY; ? ? private float spacing; ? ? private int moveType; // 0=未選擇,1=拖動,2=縮放 ? ? private float firstX; ? ? private float firstY; ? ? //系數(shù)可自己更改 ? ? private static final float DEFAULT_FATOR = 0.4f; ? ? /** ? ? ?* 阻尼因子 ? ? ?*/ ? ? private float mFator = DEFAULT_FATOR; ? ? private Scroller mScroller; ? ? /** ? ? ?* 記錄上一次觸摸事件 ? ? ?*/ ? ? private MotionEvent mLastMotionEvent; ? ? public mLayout(Context context) { ? ? ? ? this(context, null); ? ? } ? ? public mLayout(Context context, AttributeSet attrs) { ? ? ? ? this(context, attrs, 0); ? ? } ? ? public mLayout(Context context, AttributeSet attrs, int defStyleAttr) { ? ? ? ? super(context, attrs, defStyleAttr); ? ? ? ? mScroller = new Scroller(context); ? ? } ? ? @Override ? ? public boolean onTouchEvent(MotionEvent event) { ? ? ? ? super.onTouchEvent(event); ? ? ? ? switch (event.getAction() & MotionEvent.ACTION_MASK) { ? ? ? ? ? ? case MotionEvent.ACTION_DOWN: ? ? ? ? ? ? ? ? mLastMotionEvent = MotionEvent.obtain(event); ? ? ? ? ? ? ? ? moveType = 1; ? ? ? ? ? ? ? ? actionX = event.getRawX(); ? ? ? ? ? ? ? ? actionY = event.getRawY(); ? ? ? ? ? ? ? ? firstX = actionX; ? ? ? ? ? ? ? ? firstY = actionY; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_POINTER_DOWN: ? ? ? ? ? ? ? ? moveType = 2; ? ? ? ? ? ? ? ? spacing = getSpacing(event); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_MOVE: ? ? ? ? ? ? ? ? if (moveType == 1) { ? ? ? ? ? ? ? ? ? ? int dx = (int) (event.getRawX() - mLastMotionEvent.getRawX()); ? ? ? ? ? ? ? ? ? ? int dy = (int) (event.getRawY() - mLastMotionEvent.getRawY()); ? ? ? ? ? ? ? ? ? ? //如果不想對四個方向增加阻尼效果,直接刪除即可 ? ? ? ? ? ? ? ? ? ? //向上平移 ? ? ? ? ? ? ? ? ? ? if ((Math.abs(dx) < Math.abs(dy)) && dy < 0){ ? ? ? ? ? ? ? ? ? ? ? ? smoothScrollBy(0, -(int) (dy * mFator)); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? //向下平移 ? ? ? ? ? ? ? ? ? ? else if (Math.abs(dx) < Math.abs(dy) && dy > 0) { ? ? ? ? ? ? ? ? ? ? ? ? smoothScrollBy(0, -(int) (dy * mFator)); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? //向左平移 ? ? ? ? ? ? ? ? ? ? else if (Math.abs(dx) > Math.abs(dy) && dx < 0){ ? ? ? ? ? ? ? ? ? ? ? ? smoothScrollBy(-(int) (dx * mFator), 0); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? //向右平移 ? ? ? ? ? ? ? ? ? ? else if (Math.abs(dx) > Math.abs(dy) && dx > 0){ ? ? ? ? ? ? ? ? ? ? ? ? smoothScrollBy(-(int) (dx * mFator), 0); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? mLastMotionEvent = MotionEvent.obtain(event); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else if (moveType == 2) { ? ? ? ? ? ? ? ? ? ? scale = scale * getSpacing(event) / spacing; ? ? ? ? ? ? ? ? ? ? if(scale >= 1){ ? ? ? ? ? ? ? ? ? ? ? ? setScaleX(scale); ? ? ? ? ? ? ? ? ? ? ? ? setScaleY(scale); ? ? ? ? ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? ? ? ? ? scale = 1; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_UP: ? ? ? ? ? ? case MotionEvent.ACTION_POINTER_UP: ? ? ? ? ? ? case MotionEvent.ACTION_CANCEL: ? ? ? ? ? ? ? ? moveType = 0; ? ? ? ? ? ? ? ? if(scale == 1) ? ? ? ? ? ? ? ? ? ? smoothScrollTo(0, 0); ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? return true; ? ? } ? ? //攔截子button的事件 ? ? @Override ? ? public boolean onInterceptTouchEvent(MotionEvent event) { ? ? ? ? switch (event.getAction() & MotionEvent.ACTION_MASK){ ? ? ? ? ? ? case MotionEvent.ACTION_DOWN: ? ? ? ? ? ? ? ? mLastMotionEvent = MotionEvent.obtain(event); ? ? ? ? ? ? ? ? moveType = 1; ? ? ? ? ? ? ? ? actionX = event.getRawX(); ? ? ? ? ? ? ? ? actionY = event.getRawY(); ? ? ? ? ? ? ? ? firstX = actionX; ? ? ? ? ? ? ? ? firstY = actionY; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_POINTER_DOWN: ? ? ? ? ? ? ? ? moveType = 2; ? ? ? ? ? ? ? ? spacing = getSpacing(event); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_MOVE: ? ? ? ? ? ? ? ? if (moveType == 1) { ? ? ? ? ? ? ? ? ? ? int dx = (int) (event.getRawX() - mLastMotionEvent.getRawX()); ? ? ? ? ? ? ? ? ? ? int dy = (int) (event.getRawY() - mLastMotionEvent.getRawY()); ? ? ? ? ? ? ? ? ? ? //如果不想對四個方向增加阻尼效果,直接刪除即可 ? ? ? ? ? ? ? ? ? ? //向上平移 ? ? ? ? ? ? ? ? ? ? if ((Math.abs(dx) < Math.abs(dy)) && dy < 0){ ? ? ? ? ? ? ? ? ? ? ? ? smoothScrollBy(0, -(int) (dy * mFator)); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? //向下平移 ? ? ? ? ? ? ? ? ? ? else if (Math.abs(dx) < Math.abs(dy) && dy > 0) { ? ? ? ? ? ? ? ? ? ? ? ? smoothScrollBy(0, -(int) (dy * mFator)); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? //向左平移 ? ? ? ? ? ? ? ? ? ? else if (Math.abs(dx) > Math.abs(dy) && dx < 0){ ? ? ? ? ? ? ? ? ? ? ? ? smoothScrollBy(-(int) (dx * mFator), 0); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? //向右平移 ? ? ? ? ? ? ? ? ? ? else if (Math.abs(dx) > Math.abs(dy) && dx > 0){ ? ? ? ? ? ? ? ? ? ? ? ? smoothScrollBy(-(int) (dx * mFator), 0); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? mLastMotionEvent = MotionEvent.obtain(event); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else if (moveType == 2) { ? ? ? ? ? ? ? ? ? ? scale = scale * getSpacing(event) / spacing; ? ? ? ? ? ? ? ? ? ? if(scale >= 1){ ? ? ? ? ? ? ? ? ? ? ? ? setScaleX(scale); ? ? ? ? ? ? ? ? ? ? ? ? setScaleY(scale); ? ? ? ? ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? ? ? ? ? scale = 1; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_UP: ? ? ? ? ? ? ? ? moveType = 0; ? ? ? ? ? ? ? ? if(scale == 1) ? ? ? ? ? ? ? ? ? ? smoothScrollTo(0, 0); ? ? ? ? ? ? ? ? if(firstX != event.getRawX() || firstY != event.getRawY()) ? ? ? ? ? ? ? ? ? ? return true; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_POINTER_UP: ? ? ? ? ? ? ? ? moveType = 0; ? ? ? ? ? ? ? ? if(scale == 1) ? ? ? ? ? ? ? ? ? ? smoothScrollTo(0, 0); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_CANCEL: ? ? ? ? ? ? ? ? moveType = 0; ? ? ? ? ? ? ? ? if(scale == 1) ? ? ? ? ? ? ? ? ? ? smoothScrollTo(0, 0); ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? return super.onInterceptTouchEvent(event); ? ? } ? ? private void smoothScrollBy(int dx, int dy) { ? ? ? ? mScroller.startScroll(mScroller.getFinalX(), mScroller.getFinalY(), dx, dy); ? ? ? ? invalidate(); ? ? } ? ? private void smoothScrollTo(int fx, int fy) { ? ? ? ? int dx = fx - mScroller.getFinalX(); ? ? ? ? int dy = fx - mScroller.getFinalY(); ? ? ? ? smoothScrollBy(dx, dy); ? ? } ? ? @Override ? ? public void computeScroll() { ? ? ? ? if (mScroller.computeScrollOffset()) { ? ? ? ? ? ? scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); ? ? ? ? ? ? postInvalidate(); ? ? ? ? } ? ? ? ? super.computeScroll(); ? ? } ? ? // 觸碰兩點間距離 ? ? private float getSpacing(MotionEvent event) { ? ? ? ? //通過三角函數(shù)得到兩點間的距離 ? ? ? ? float x = event.getX(0) - event.getX(1); ? ? ? ? float y = event.getY(0) - event.getY(1); ? ? ? ? return (float) Math.sqrt(x * x + y * y); ? ? } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android基于reclyview實現(xiàn)列表回彈動畫效果
- Android?ScrollView實現(xiàn)滾動超過邊界松手回彈
- android ScrollView實現(xiàn)水平滑動回彈
- Android實現(xiàn)背景圖滑動變大松開回彈效果
- Android自定義View實現(xiàn)豎向滑動回彈效果
- android實現(xiàn)可上下回彈的scrollview
- Android實現(xiàn)回彈ScrollView的原理
- Android自定義實現(xiàn)可回彈的ScollView
- Android ScrollView的頂部下拉和底部上拉回彈效果
- android自定義滾動上下回彈scollView
相關(guān)文章
Android ViewPager實現(xiàn)左右滑動的實例
這篇文章主要介紹了Android ViewPager實現(xiàn)左右滑動的實例的相關(guān)資料,這里提供實現(xiàn)代碼實現(xiàn)左右滑動的功能,希望能幫助到大家,需要的朋友可以參考下2017-08-08Android巧用Fragment解耦onActivityResult詳解
這篇文章主要給大家介紹了關(guān)于Android巧用Fragment解耦onActivityResult的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08