这篇文章将为大家详细讲解有关如何使用Android实现单页面浮层可拖动view,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。在DragframeLayout中的onTouchEvent一直接收不到触摸消息
这篇文章将为大家详细讲解有关如何使用Android实现单页面浮层可拖动view,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
在DragframeLayout中的onTouchEvent一直接收不到触摸消息,而且在onInterceptTouchEvent的时候,并没有触发ViewDragHelper.tryCaptureView方法,因此诞生了另一种比较原始的方法:通过自定义可拖动view来实现
主要方法:
initEdge:设置可拖动view能拖动范围的初始边界,一般情况下为父布局的边界。注意view.getLeft...等会获取到会0,我是在网路数据返回的情况下设置边界,并显示的。也有方法开一个子线程获取。
onTouchEvent:拖动的计算以及重新layout
代码:
import android.content.Context;import android.support.annotation.Nullable;import android.support.v7.widget.AppCompatImageView;import android.util.AttributeSet;import android.view.MotionEvent;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); } 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: //避免滑出触发点击事件 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>
关于“如何使用Android实现单页面浮层可拖动view”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
--结束END--
本文标题: 如何使用Android实现单页面浮层可拖动view
本文链接: https://www.lsjlt.com/news/220750.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0