Android使用ViewDragHelper實(shí)現(xiàn)圖片下拽返回示例
微信的圖片下拽返回功能在日常使用中非常方便,同時(shí)在很多 App 中都見到了類似的設(shè)計(jì),可以說一旦習(xí)慣這種操作再也回不去了。
這幾天逛 GitHub,發(fā)現(xiàn)一個很贊的庫 https://github.com/iielse/ImageWatcher 高度還原了微信的效果,粗看了下源碼,我覺得可以更簡單的實(shí)現(xiàn)類似的效果,動手實(shí)現(xiàn)后,發(fā)現(xiàn) ViewDragHelper + ActvitySceneTransition 的方案簡單粗暴,廢話不說,先看效果:

什么是 ViewDragHelper
具體實(shí)現(xiàn)之前先簡單介紹下什么是 ViewDragHelper。
ViewDragHelper 是 support v4 兼容包中的一個工具類,用來簡化自定義 ViewGroup 中的手勢處理。
使用 ViewDragHelper 可以輕松實(shí)現(xiàn) ViewGroup 里 View 的拖拽操作,這里介紹下使用 ViewDragHelper 里幾個重要步驟。
初始化
通過靜態(tài)方法創(chuàng)建:viewGroup 即為當(dāng)前容器;sensitivity 為拖拽的靈敏度,默認(rèn)為 1;callback 為配置拖拽中的各種邏輯處理。
mViewDragHelper = ViewDragHelper.create(viewGroup, callback); ... or ... mViewDragHelper = ViewDragHelper.create(viewGroup, sensitivity, callback);
Callback
這里僅列出我們需要使用到的一些回調(diào)方法:
public abstract static class Callback {
/**
* 當(dāng)子 View 被拖動改變位置時(shí)回調(diào)此方法
*
* @param changedView 當(dāng)前子 View
* @param left 當(dāng)前子 View 的最新 X 坐標(biāo)
* @param top 當(dāng)前子 View 的最新 Y 坐標(biāo)
* @param dx 當(dāng)前子 View 的最新 X 坐標(biāo)較上次 X 的位移量
* @param dy 當(dāng)前子 View 的最新 Y 坐標(biāo)較上次 Y 的位移量
*/
public void onViewPositionChanged(@NonNull View changedView, int left, int top, int dx,
int dy) {
}
/**
* 當(dāng)子 View 被釋放后回調(diào)此方法
*
* @param releasedChild 當(dāng)前子 View
* @param xvel X 子 View 被釋放時(shí),用戶手指在屏幕上滑動的橫向加速度
* @param yvel Y 子 View 被釋放時(shí),用戶手指在屏幕上滑動的縱向加速度
*/
public void onViewReleased(@NonNull View releasedChild, float xvel, float yvel) {}
/**
* 限制子 View 水平拖拽范圍。
*
* 如果返回 0,則不能進(jìn)行水平拖動,如果要實(shí)現(xiàn)拖拽,返回值 > 0 即可。
*
*/
public int getViewHorizontalDragRange(@NonNull View child) {
return 1;
}
/**
* 限制子 View 縱向拖拽范圍。
*
* 如果返回 0,則不能進(jìn)行縱向拖動, 我們要實(shí)現(xiàn)拖拽,返回值 > 0 即可。
*
*/
public int getViewVerticalDragRange(@NonNull View child) {
return 1;
}
/**
* 判斷當(dāng)前觸摸的 View 能否被捕獲進(jìn)行拖拽,如果返回 true 則可以進(jìn)行拖拽。
*/
public abstract boolean tryCaptureView(@NonNull View child, int pointerId);
/**
* 限制當(dāng)前 View 的橫向拖拽范圍,也可說是我們可以動態(tài)修正 View 的 top 坐標(biāo),比如我們想限制 View 只在容器內(nèi)部拖動
*
* @param child 當(dāng)前拖動的 View
* @param left View 上次的 x 坐標(biāo) + 手指移動的 x 軸位移量
* @param dx 手指移動的位移量
* @return 修正后的 x 坐標(biāo),直接返回 left 表示不限制水平拖拽范圍,拖到哪兒就哪兒
*/
public int clampViewPositionHorizontal(@NonNull View child, int left, int dx) {
return left;
}
/**
* 限制當(dāng)前 View 的縱向拖拽范圍,也可說是我們可以動態(tài)修正 View 的 top 坐標(biāo),比如我們想限制 View 只在容器內(nèi)部拖動
*
* @param child 當(dāng)前拖動的 View
* @param top View 上次的 y 坐標(biāo) + 手指移動的 y 軸位移量
* @param dx 手指移動的位移量
* @return 修正后的 x 坐標(biāo),直接返回 top 表示不限制縱向拖拽范圍,拖到哪兒就哪兒
*/
public int clampViewPositionVertical(@NonNull View child, int top, int dy) {
return top;
}
}
處理 Touch 事件
復(fù)雜的觸摸邏輯就讓 ViewDragHelper 接管即可。
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return mViewDragHelper.shouldInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mViewDragHelper.processTouchEvent(event);
return true;
}
處理 View 自動復(fù)位效果
當(dāng)拖拽操作不滿足觸發(fā)條件時(shí),手指松開,View 需要自動回到初始位置,在 onViewReleased 里調(diào)用以下方法即可:
mViewDragHelper.settleCapturedViewAt(originPoint.x, originPoint.y); invalidate();
同時(shí)需要覆寫:
@Override
public void computeScroll() {
if (mViewDragHelper.continueSettling(true)) {
ViewCompat.postInvalidateOnAnimation(this);
}
}
具體實(shí)現(xiàn)步驟
1. 自定義 DragLayout,內(nèi)部使用 ViewDragHelper 來處理拖拽操作。
2. 創(chuàng)建 Activity 展示圖片,使用 DragLayout 作為根布局:
<com.liyu.fakeweather.widgets.DragLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drag_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
android:fitsSystemWindows="true">
<ImageView
android:id="@+id/picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:scaleType="fitCenter" />
</com.liyu.fakeweather.widgets.DragLayout>
并設(shè)置 Activity 背景為透明:
3. 實(shí)現(xiàn)拖拽時(shí)動態(tài)改變背景透明度以及圖片的縮放效果:
@Override
public void onViewPositionChanged(@NonNull View changedView, int left, int top, int dx, int dy) {
if (top > originPoint.y) {// 僅當(dāng)向下拖拽時(shí)才處理
float a = (float) (top - originPoint.y) / (float) (getMeasuredHeight() - originPoint.y);
setBackgroundColor(ThemeUtil.changeAlpha(0xff000000, 1 - a));// 根據(jù)拖拽位移量動態(tài)改變背景透明度
targetView.setScaleX(1 - a);// 縮放圖片
targetView.setScaleY(1 - a);// 縮放圖片
if ((top - originPoint.y) > getMeasuredHeight() / 5) {
callEvent = true; // 下拽的位移量滿足要求,可以觸發(fā)關(guān)閉操作
} else {
callEvent = false;// 不能觸發(fā)關(guān)閉操作
}
}
}
4. 手指釋放時(shí)邏輯處理:
@Override
public void onViewReleased(@NonNull View releasedChild, float xvel, float yvel) {
if (releasedChild == targetView) {
if (callEvent || yvel > 8000) {// 除了判斷下拽的位移量,還要判斷快速下拽的速度,這邊 yevl 為用戶手指快速滑動的 Y 軸加速度,當(dāng)加速度大于一定值時(shí)也可觸發(fā)關(guān)閉操作
if (listener != null) {
listener.onDragFinished();
}
callEvent = false;
} else {
// 當(dāng)拖拽不滿足觸發(fā)條件時(shí),需要將 View 歸位,這里使用自帶的方法實(shí)現(xiàn)動畫效果,傳入初始坐標(biāo)即可。
mViewDragHelper.settleCapturedViewAt(originPoint.x, originPoint.y);
invalidate();
}
}
}
5. 圖片的轉(zhuǎn)場動畫:
使用自帶轉(zhuǎn)場動畫即可實(shí)現(xiàn)圖片的打開和關(guān)閉動畫。
ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(
(Activity) context, transitView, PictureActivity.TRANSIT_PIC);
ActivityCompat.startActivity(context, intent, optionsCompat.toBundle());
...
ViewCompat.setTransitionName(mImageView, TRANSIT_PIC);
...
圖片下拽返回的功能加入到了假裝看天氣的妹子圖模塊中,完整示例代碼可前往 https://github.com/li-yu/FakeWeather/blob/master/app/src/main/java/com/liyu/fakeweather/widgets/DragLayout.java 查看。
總結(jié)
依靠簡單而又強(qiáng)大的 ViewDragHelper,不到 200 行的代碼也實(shí)現(xiàn)了類似的效果。他山之石可以攻玉,翻看其源碼,也學(xué)到一些很少用到的小技巧,比如獲取當(dāng)前觸摸位置的子 View,逆向遍歷全部子 View 集合然后判斷觸摸坐標(biāo)是否在范圍內(nèi),真是簡單粗暴:
@Nullable
public View findTopChildUnder(int x, int y) {
final int childCount = mParentView.getChildCount();
for (int i = childCount - 1; i >= 0; i--) {
final View child = mParentView.getChildAt(mCallback.getOrderedChildIndex(i));
if (x >= child.getLeft() && x < child.getRight()
&& y >= child.getTop() && y < child.getBottom()) {
return child;
}
}
return null;
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android ViewDragHelper實(shí)現(xiàn)京東、淘寶拖拽詳情功能的實(shí)現(xiàn)
- Android ViewDragHelper使用方法詳解
- Android利用ViewDragHelper輕松實(shí)現(xiàn)拼圖游戲的示例
- ViewDragHelper實(shí)現(xiàn)QQ側(cè)滑效果
- Android ViewDragHelper仿淘寶拖動加載效果
- Android 中通過ViewDragHelper實(shí)現(xiàn)ListView的Item的側(cè)拉劃出效果
- Android ViewDragHelper完全解析 自定義ViewGroup神器
- Android基于ViewDragHelper仿QQ5.0側(cè)滑界面效果
- Android使用ViewDragHelper實(shí)現(xiàn)仿QQ6.0側(cè)滑界面(一)
- Android使用ViewDragHelper實(shí)現(xiàn)QQ6.X最新版本側(cè)滑界面效果實(shí)例代碼
相關(guān)文章
Android下2d物理引擎Box2d用法簡單實(shí)例
這篇文章主要介紹了Android下2d物理引擎Box2d用法,實(shí)例分析了在Android平臺上使用Box2d的基本技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
詳解MVP模式在Android開發(fā)中的應(yīng)用
MVP是MVC衍生而來的,很早以前就由某軟公司提出,近年來在Android應(yīng)用開發(fā)中越來越多的被提及,越來越重要了。這篇文章主要介紹了詳解MVP模式在Android開發(fā)中的應(yīng)用,有興趣的可以了解一下。2016-11-11
強(qiáng)制Android應(yīng)用使用某個Locale的方法
這篇文章主要介紹了強(qiáng)制Android應(yīng)用使用某個Locale的方法,涉及Android基于Locale進(jìn)行語言設(shè)置的相關(guān)技巧,需要的朋友可以參考下2015-10-10
Android ListView填充數(shù)據(jù)的方法
這篇文章主要介紹了Android ListView填充數(shù)據(jù)的方法的相關(guān)資料,大多數(shù)實(shí)現(xiàn)這樣的功能都是使用XML文件,這里就說下不使用布局文件如何實(shí)現(xiàn),需要的朋友可以參考下2017-09-09
在Android應(yīng)用中實(shí)現(xiàn)離線數(shù)據(jù)同步的步驟詳解
在構(gòu)建 Android 應(yīng)用時(shí),離線數(shù)據(jù)同步是一個不可或缺的環(huán)節(jié),無論是網(wǎng)絡(luò)狀況不佳,還是用戶處于飛行模式,離線數(shù)據(jù)同步都能讓用戶在無網(wǎng)絡(luò)的情況下繼續(xù)使用應(yīng)用,本文將詳細(xì)介紹如何在 Android 應(yīng)用中實(shí)現(xiàn)離線數(shù)據(jù)同步,需要的朋友可以參考下2024-08-08
Android ListView出現(xiàn)異常解決辦法
這篇文章主要介紹了Android ListView出現(xiàn)異常ListView:The content of the adapter has changed but ListView did not receive a notification解決辦法的相關(guān)資料,需要的朋友可以參考下2016-11-11
Android SdkVersion的區(qū)別及獲取版本信息方法
下面小編就為大家?guī)硪黄狝ndroid SdkVersion的區(qū)別及獲取版本信息方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03

