這篇文章將為大家詳細(xì)講解有關(guān)如何使用Android實(shí)現(xiàn)單頁(yè)面浮層可拖動(dòng)view,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

成都創(chuàng)新互聯(lián)長(zhǎng)期為上千客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為寶雞企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè),寶雞網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
在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>
關(guān)于“如何使用Android實(shí)現(xiàn)單頁(yè)面浮層可拖動(dòng)view”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
網(wǎng)頁(yè)標(biāo)題:如何使用Android實(shí)現(xiàn)單頁(yè)面浮層可拖動(dòng)view
本文路徑:http://chinadenli.net/article12/jijsgc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、用戶體驗(yàn)、關(guān)鍵詞優(yōu)化、營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)