Android實(shí)現(xiàn)單頁面浮層可拖動(dòng)view的一種方法
上一篇講到通過通過goolge官方的ViewDragHelper工具實(shí)現(xiàn)拖動(dòng)的方法(上一篇見http://www.dbjr.com.cn/article/125481.htm),那么有一個(gè)問題就是在DragframeLayout中的onTouchEvent一直接收不到觸摸消息,而且在onInterceptTouchEvent的時(shí)候,并沒有觸發(fā)ViewDragHelper.tryCaptureView方法,因此誕生了另一種比較原始的方法:通過自定義可拖動(dòng)view來實(shí)現(xiàn)
主要方法:
initEdge:設(shè)置可拖動(dòng)view能拖動(dòng)范圍的初始邊界,一般情況下為父布局的邊界。注意view.getLeft...等會(huì)獲取到會(huì)0,我是在網(wǎng)路數(shù)據(jù)返回的情況下設(shè)置邊界,并顯示的。也有方法開一個(gè)子線程獲取。
onTouchEvent:拖動(dòng)的計(jì)算以及重新layout
代碼:
import android.content.Context; import android.support.annotation.Nullable; import android.support.v7.widget.AppCompatImageView; import android.util.AttributeSet; import android.view.MotionEvent; /** * Created by hq on 2017/10/10. * 參考:http://blog.csdn.net/zane_xiao/article/details/51188867 */ public class DragImageView extends AppCompatImageView { String TAG = "DragImageView"; public DragImageView(Context context) { this(context, null); } public DragImageView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public DragImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } /** * 設(shè)置在父布局中的邊界 * @param l * @param t * @param r * @param b */ public void initEdge(int l,int t,int r,int b) { edgeLeft = l; edgeTop = t; edgeRight = r; edgeBottom = b; } int edgeLeft, edgeTop, edgeRight, edgeBottom; int lastX, lastY, movex, movey, dx, dy; @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); movex = lastX; movey = lastY; break; case MotionEvent.ACTION_MOVE: dx = (int) event.getRawX() - lastX; dy = (int) event.getRawY() - lastY; int left = getLeft() + dx; int top = getTop() + dy; int right = getRight() + dx; int bottom = getBottom() + dy; if (left < edgeLeft) { left = edgeLeft; right = left + getWidth(); } if (right > edgeRight) { right = edgeRight; left = right - getWidth(); } if (top < edgeTop) { top = edgeTop; bottom = top + getHeight(); } if (bottom > edgeBottom) { bottom = edgeBottom; top = bottom - getHeight(); } layout(left, top, right, bottom); lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); break; case MotionEvent.ACTION_UP: //避免滑出觸發(fā)點(diǎn)擊事件 if ((int) (event.getRawX() - movex) != 0 || (int) (event.getRawY() - movey) != 0) { return true; } break; default: break; } return super.onTouchEvent(event); } }
布局:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/df_content" android:layout_width="match_parent" android:layout_height="match_parent"> <com.windfindtech.ishanghai.view.SwipeScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/default_white" android:scrollbars="none"> <RelativeLayout android:id="@+id/network_tab_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/default_white"> ........... </RelativeLayout> </com.windfindtech.ishanghai.view.SwipeScrollView> <com.windfindtech.ishanghai.view.DragImageView android:id="@+id/iv_drag_adver" android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="right|top" android:src="@drawable/ic_launcher" /> </FrameLayout>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)ImageView圖片縮放和拖動(dòng)
- Android實(shí)現(xiàn)跟隨手指拖動(dòng)并自動(dòng)貼邊的View樣式(實(shí)例demo)
- Android自定義View實(shí)現(xiàn)拖動(dòng)選擇按鈕
- Android通過自定義ImageView控件實(shí)現(xiàn)圖片的縮放和拖動(dòng)的實(shí)現(xiàn)代碼
- Android開發(fā)實(shí)現(xiàn)可拖動(dòng)排序的ListView功能【附源碼下載】
- Android DragImageView實(shí)現(xiàn)下拉拖動(dòng)圖片放大效果
- Android RecyclerView滑動(dòng)刪除和拖動(dòng)排序
- Android ViewDragHelper仿淘寶拖動(dòng)加載效果
- Android自定義View圓形和拖動(dòng)圓、跟隨手指拖動(dòng)效果
- android實(shí)現(xiàn)可拖動(dòng)的浮動(dòng)view
相關(guān)文章
Android自定義控件通用驗(yàn)證碼輸入框的實(shí)現(xiàn)
這篇文章主要介紹了Android自定義控件通用驗(yàn)證碼輸入框的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03解決Could not find com.android.tools.build:gradle:3.0.0
這篇文章主要介紹了在Android Studio升級(jí)時(shí)碰到Could not find com.android.tools.build:gradle:3.0.0問題的解決方法,需要的朋友跟隨小編一起看看吧2021-08-08Android Studio 實(shí)現(xiàn)文檔注釋的快捷鍵
這篇文章主要介紹了Android Studio 實(shí)現(xiàn)文檔注釋的快捷鍵,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android實(shí)現(xiàn)微信支付的統(tǒng)一下單
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)微信支付的統(tǒng)一下單,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Android開發(fā)中畫廊視圖Gallery的兩種使用方法分析
這篇文章主要介紹了Android開發(fā)中畫廊視圖Gallery的兩種使用方法,結(jié)合實(shí)例形式分析了Android畫廊視圖Gallery的簡(jiǎn)單布局與功能實(shí)現(xiàn)相關(guān)操作技巧,需要的朋友可以參考下2018-01-01Android圖片添加水印圖片并把圖片保存到文件存儲(chǔ)的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android圖片添加水印圖片并把圖片保存到文件存儲(chǔ)的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06基于Android應(yīng)用中如何反饋Crash報(bào)告的詳解
本篇文章是對(duì)在Android應(yīng)用中如何反饋Crash報(bào)告的詳細(xì)分析介紹。需要的朋友參考下2013-05-05Android自定義View實(shí)現(xiàn)星星評(píng)分效果
這篇文章主要為大家詳細(xì)介紹了Android如何利用自定義View實(shí)現(xiàn)一個(gè)星星評(píng)分的控件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下2022-11-11