如何在Android中使用ViewDragHelper實現圖片下拽返回?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
Android是一種基于Linux內核的自由及開放源代碼的操作系統,主要使用于移動設備,如智能手機和平板電腦,由美國Google公司和開放手機聯盟領導及開發。
什么是 ViewDragHelper
具體實現之前先簡單介紹下什么是 ViewDragHelper。
ViewDragHelper 是 support v4 兼容包中的一個工具類,用來簡化自定義 ViewGroup 中的手勢處理。
使用 ViewDragHelper 可以輕松實現 ViewGroup 里 View 的拖拽操作,這里介紹下使用 ViewDragHelper 里幾個重要步驟。
初始化
通過靜態方法創建:viewGroup 即為當前容器;sensitivity 為拖拽的靈敏度,默認為 1;callback 為配置拖拽中的各種邏輯處理。
mViewDragHelper = ViewDragHelper.create(viewGroup, callback); ... or ... mViewDragHelper = ViewDragHelper.create(viewGroup, sensitivity, callback);
Callback
這里僅列出我們需要使用到的一些回調方法:
public abstract static class Callback {
/**
* 當子 View 被拖動改變位置時回調此方法
*
* @param changedView 當前子 View
* @param left 當前子 View 的最新 X 坐標
* @param top 當前子 View 的最新 Y 坐標
* @param dx 當前子 View 的最新 X 坐標較上次 X 的位移量
* @param dy 當前子 View 的最新 Y 坐標較上次 Y 的位移量
*/
public void onViewPositionChanged(@NonNull View changedView, int left, int top, int dx,
int dy) {
}
/**
* 當子 View 被釋放后回調此方法
*
* @param releasedChild 當前子 View
* @param xvel X 子 View 被釋放時,用戶手指在屏幕上滑動的橫向加速度
* @param yvel Y 子 View 被釋放時,用戶手指在屏幕上滑動的縱向加速度
*/
public void onViewReleased(@NonNull View releasedChild, float xvel, float yvel) {}
/**
* 限制子 View 水平拖拽范圍。
*
* 如果返回 0,則不能進行水平拖動,如果要實現拖拽,返回值 > 0 即可。
*
*/
public int getViewHorizontalDragRange(@NonNull View child) {
return 1;
}
/**
* 限制子 View 縱向拖拽范圍。
*
* 如果返回 0,則不能進行縱向拖動, 我們要實現拖拽,返回值 > 0 即可。
*
*/
public int getViewVerticalDragRange(@NonNull View child) {
return 1;
}
/**
* 判斷當前觸摸的 View 能否被捕獲進行拖拽,如果返回 true 則可以進行拖拽。
*/
public abstract boolean tryCaptureView(@NonNull View child, int pointerId);
/**
* 限制當前 View 的橫向拖拽范圍,也可說是我們可以動態修正 View 的 top 坐標,比如我們想限制 View 只在容器內部拖動
*
* @param child 當前拖動的 View
* @param left View 上次的 x 坐標 + 手指移動的 x 軸位移量
* @param dx 手指移動的位移量
* @return 修正后的 x 坐標,直接返回 left 表示不限制水平拖拽范圍,拖到哪兒就哪兒
*/
public int clampViewPositionHorizontal(@NonNull View child, int left, int dx) {
return left;
}
/**
* 限制當前 View 的縱向拖拽范圍,也可說是我們可以動態修正 View 的 top 坐標,比如我們想限制 View 只在容器內部拖動
*
* @param child 當前拖動的 View
* @param top View 上次的 y 坐標 + 手指移動的 y 軸位移量
* @param dx 手指移動的位移量
* @return 修正后的 x 坐標,直接返回 top 表示不限制縱向拖拽范圍,拖到哪兒就哪兒
*/
public int clampViewPositionVertical(@NonNull View child, int top, int dy) {
return top;
}
}處理 Touch 事件
復雜的觸摸邏輯就讓 ViewDragHelper 接管即可。
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return mViewDragHelper.shouldInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mViewDragHelper.processTouchEvent(event);
return true;
}處理 View 自動復位效果
當拖拽操作不滿足觸發條件時,手指松開,View 需要自動回到初始位置,在 onViewReleased 里調用以下方法即可:
mViewDragHelper.settleCapturedViewAt(originPoint.x, originPoint.y); invalidate();
同時需要覆寫:
@Override
public void computeScroll() {
if (mViewDragHelper.continueSettling(true)) {
ViewCompat.postInvalidateOnAnimation(this);
}
}具體實現步驟
1. 自定義 DragLayout,內部使用 ViewDragHelper 來處理拖拽操作。
2. 創建 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>
并設置 Activity 背景為透明:
復制代碼 代碼如下:
activity.getWindow().getDecorView().setBackgroundColor(Color.TRANSPARENT);// 當然也可以在 theme 里設置
3. 實現拖拽時動態改變背景透明度以及圖片的縮放效果:
@Override
public void onViewPositionChanged(@NonNull View changedView, int left, int top, int dx, int dy) {
if (top > originPoint.y) {// 僅當向下拖拽時才處理
float a = (float) (top - originPoint.y) / (float) (getMeasuredHeight() - originPoint.y);
setBackgroundColor(ThemeUtil.changeAlpha(0xff000000, 1 - a));// 根據拖拽位移量動態改變背景透明度
targetView.setScaleX(1 - a);// 縮放圖片
targetView.setScaleY(1 - a);// 縮放圖片
if ((top - originPoint.y) > getMeasuredHeight() / 5) {
callEvent = true; // 下拽的位移量滿足要求,可以觸發關閉操作
} else {
callEvent = false;// 不能觸發關閉操作
}
}
}4. 手指釋放時邏輯處理:
@Override
public void onViewReleased(@NonNull View releasedChild, float xvel, float yvel) {
if (releasedChild == targetView) {
if (callEvent || yvel > 8000) {// 除了判斷下拽的位移量,還要判斷快速下拽的速度,這邊 yevl 為用戶手指快速滑動的 Y 軸加速度,當加速度大于一定值時也可觸發關閉操作
if (listener != null) {
listener.onDragFinished();
}
callEvent = false;
} else {
// 當拖拽不滿足觸發條件時,需要將 View 歸位,這里使用自帶的方法實現動畫效果,傳入初始坐標即可。
mViewDragHelper.settleCapturedViewAt(originPoint.x, originPoint.y);
invalidate();
}
}
}5. 圖片的轉場動畫:
使用自帶轉場動畫即可實現圖片的打開和關閉動畫。
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 查看。
總結
依靠簡單而又強大的 ViewDragHelper,不到 200 行的代碼也實現了類似的效果。他山之石可以攻玉,翻看其源碼,也學到一些很少用到的小技巧,比如獲取當前觸摸位置的子 View,逆向遍歷全部子 View 集合然后判斷觸摸坐標是否在范圍內,真是簡單粗暴:
@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;
}看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。