
@H_301_0@主要方法:
@H_301_0@initEdge:设置可拖动vIEw能拖动范围的初始边界,一般情况下为父布局的边界。注意vIEw.getleft...等会获取到会0,我是在网路数据返回的情况下设置边界,并显示的。也有方法开一个子线程获取。@H_301_0@ontouchEvent:拖动的计算以及重新layout@H_301_0@代码:
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,defStyleAttr); } /** * 设置在父布局中的边界 * @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: //避免滑出触发点击事件 if ((int) (event.getRawX() - movex) != 0 || (int) (event.getRawY() - movey) != 0) { return true; } break; default: break; } return super.ontouchEvent(event); }}@H_502_14@@H_301_0@布局:<?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>@H_502_14@@H_301_0@以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。 总结 以上是内存溢出为你收集整理的Android实现单页面浮层可拖动view的一种方法全部内容,希望文章能够帮你解决Android实现单页面浮层可拖动view的一种方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)