如何在Android中使用ViewDragHelper實(shí)現(xiàn)圖片下拽返回?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
成都創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站制作、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿(mǎn)足客戶(hù)于互聯(lián)網(wǎng)時(shí)代的龍安網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
Android是一種基于Linux內(nèi)核的自由及開(kāi)放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國(guó)Google公司和開(kāi)放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開(kāi)發(fā)。
什么是 ViewDragHelper
具體實(shí)現(xiàn)之前先簡(jiǎn)單介紹下什么是 ViewDragHelper。
ViewDragHelper 是 support v4 兼容包中的一個(gè)工具類(lèi),用來(lái)簡(jiǎn)化自定義 ViewGroup 中的手勢(shì)處理。
使用 ViewDragHelper 可以輕松實(shí)現(xiàn) ViewGroup 里 View 的拖拽操作,這里介紹下使用 ViewDragHelper 里幾個(gè)重要步驟。
初始化
通過(guò)靜態(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 被拖動(dòng)改變位置時(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í),用戶(hù)手指在屏幕上滑動(dòng)的橫向加速度 * @param yvel Y 子 View 被釋放時(shí),用戶(hù)手指在屏幕上滑動(dòng)的縱向加速度 */ public void onViewReleased(@NonNull View releasedChild, float xvel, float yvel) {} /** * 限制子 View 水平拖拽范圍。 * * 如果返回 0,則不能進(jìn)行水平拖動(dòng),如果要實(shí)現(xiàn)拖拽,返回值 > 0 即可。 * */ public int getViewHorizontalDragRange(@NonNull View child) { return 1; } /** * 限制子 View 縱向拖拽范圍。 * * 如果返回 0,則不能進(jìn)行縱向拖動(dòng), 我們要實(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 的橫向拖拽范圍,也可說(shuō)是我們可以動(dòng)態(tài)修正 View 的 top 坐標(biāo),比如我們想限制 View 只在容器內(nèi)部拖動(dòng) * * @param child 當(dāng)前拖動(dòng)的 View * @param left View 上次的 x 坐標(biāo) + 手指移動(dòng)的 x 軸位移量 * @param dx 手指移動(dòng)的位移量 * @return 修正后的 x 坐標(biāo),直接返回 left 表示不限制水平拖拽范圍,拖到哪兒就哪兒 */ public int clampViewPositionHorizontal(@NonNull View child, int left, int dx) { return left; } /** * 限制當(dāng)前 View 的縱向拖拽范圍,也可說(shuō)是我們可以動(dòng)態(tài)修正 View 的 top 坐標(biāo),比如我們想限制 View 只在容器內(nèi)部拖動(dòng) * * @param child 當(dāng)前拖動(dòng)的 View * @param top View 上次的 y 坐標(biāo) + 手指移動(dòng)的 y 軸位移量 * @param dx 手指移動(dòng)的位移量 * @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 自動(dòng)復(fù)位效果
當(dāng)拖拽操作不滿(mǎn)足觸發(fā)條件時(shí),手指松開(kāi),View 需要自動(dòng)回到初始位置,在 onViewReleased 里調(diào)用以下方法即可:
mViewDragHelper.settleCapturedViewAt(originPoint.x, originPoint.y); invalidate();
同時(shí)需要覆寫(xiě):
@Override public void computeScroll() { if (mViewDragHelper.continueSettling(true)) { ViewCompat.postInvalidateOnAnimation(this); } }
具體實(shí)現(xiàn)步驟
1. 自定義 DragLayout,內(nèi)部使用 ViewDragHelper 來(lái)處理拖拽操作。
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 背景為透明:
復(fù)制代碼 代碼如下:
activity.getWindow().getDecorView().setBackgroundColor(Color.TRANSPARENT);// 當(dāng)然也可以在 theme 里設(shè)置
3. 實(shí)現(xiàn)拖拽時(shí)動(dòng)態(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ù)拖拽位移量動(dòng)態(tài)改變背景透明度 targetView.setScaleX(1 - a);// 縮放圖片 targetView.setScaleY(1 - a);// 縮放圖片 if ((top - originPoint.y) > getMeasuredHeight() / 5) { callEvent = true; // 下拽的位移量滿(mǎn)足要求,可以觸發(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 為用戶(hù)手指快速滑動(dòng)的 Y 軸加速度,當(dāng)加速度大于一定值時(shí)也可觸發(fā)關(guān)閉操作 if (listener != null) { listener.onDragFinished(); } callEvent = false; } else { // 當(dāng)拖拽不滿(mǎn)足觸發(fā)條件時(shí),需要將 View 歸位,這里使用自帶的方法實(shí)現(xiàn)動(dòng)畫(huà)效果,傳入初始坐標(biāo)即可。 mViewDragHelper.settleCapturedViewAt(originPoint.x, originPoint.y); invalidate(); } } }
5. 圖片的轉(zhuǎn)場(chǎng)動(dòng)畫(huà):
使用自帶轉(zhuǎn)場(chǎng)動(dòng)畫(huà)即可實(shí)現(xiàn)圖片的打開(kāi)和關(guān)閉動(dòng)畫(huà)。
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é)
依靠簡(jiǎn)單而又強(qiáng)大的 ViewDragHelper,不到 200 行的代碼也實(shí)現(xiàn)了類(lèi)似的效果。他山之石可以攻玉,翻看其源碼,也學(xué)到一些很少用到的小技巧,比如獲取當(dāng)前觸摸位置的子 View,逆向遍歷全部子 View 集合然后判斷觸摸坐標(biāo)是否在范圍內(nèi),真是簡(jiǎn)單粗暴:
@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)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
文章名稱(chēng):如何在Android中使用ViewDragHelper實(shí)現(xiàn)圖片下拽返回
本文URL:http://www.2m8n56k.cn/article44/johpee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、電子商務(wù)、標(biāo)簽優(yōu)化、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)