Android实现单页面浮层可拖动view的示例代码

作者:赖床的猫 时间:2023-05-25 16:41:03 

需求是需要在一个已经存在的页面添加一个可拖动的浮层广告。

使用到的技术:ViewDragHelper

效果如图:

Android实现单页面浮层可拖动view的示例代码

封装好的类(继承自FrameLayout)


import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

import java.util.ArrayList;

/**
* Created by hq on 2017/10/10.
*/
public class DragFrameLayout extends FrameLayout {

String TAG = "DragFrameLayout";
 ViewDragHelper dragHelper;
 ArrayList<View> viewList;
 public DragFrameLayout(@NonNull Context context) {
   this(context, null);
 }

public DragFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
   this(context, attrs, 0);
 }

public DragFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
   super(context, attrs, defStyleAttr);
   //第二步:创建存放View的集合
   viewList = new ArrayList<>();

dragHelper = ViewDragHelper.create(this, 1.0f, new ViewDragHelper.Callback() {

/**
      * 是否捕获childView:
      * 如果viewList包含child,那么捕获childView
      * 如果不包含child,就不捕获childView
      */
     @Override
     public boolean tryCaptureView(View child, int pointerId) {
       return viewList.contains(child);
     }

@Override
     public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
       super.onViewPositionChanged(changedView, left, top, dx, dy);
     }

/**
      * 当捕获到child后的处理:
      * 获取child的监听
      */
     @Override
     public void onViewCaptured(View capturedChild, int activePointerId) {
       super.onViewCaptured(capturedChild, activePointerId);
       if (onDragDropListener != null) {
         onDragDropListener.onDragDrop(true);
       }
     }

/**
      * 当释放child后的处理:
      * 取消监听,不再处理
      */
     @Override
     public void onViewReleased(View releasedChild, float xvel, float yvel) {
       super.onViewReleased(releasedChild, xvel, yvel);
       if (onDragDropListener != null) {
         onDragDropListener.onDragDrop(false);
       }
     }

/**
      * 到左边界的距离
      */
     @Override
     public int clampViewPositionHorizontal(View child, int left, int dx) {
       return left;
     }

/**
      * 到上边界的距离
      */
     @Override
     public int clampViewPositionVertical(View child, int top, int dy) {
       return top;
     }
   });
 }

/**
  * 把要实现拖动的子view添加进来
  * @param view
  */
 public void addDragChildView(View view){
   viewList.add(view);
 }

@Override
 public boolean onInterceptTouchEvent(MotionEvent ev) {
   //当手指抬起或事件取消的时候 就不拦截事件
   int actionMasked = ev.getActionMasked();
   if (actionMasked == MotionEvent.ACTION_CANCEL || actionMasked == MotionEvent.ACTION_UP) {
     return false;
   }
   return dragHelper.shouldInterceptTouchEvent(ev);
 }

@Override
 public boolean onTouchEvent(MotionEvent event) {
   dragHelper.processTouchEvent(event);
   return true;
 }
 public interface OnDragDropListener {
   void onDragDrop(boolean captured);
 }

private OnDragDropListener onDragDropListener;

public void setOnDragDropListener(OnDragDropListener onDragDropListener) {
   this.onDragDropListener = onDragDropListener;
 }
}

使用方法:

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<com.windfindtech.hqdemo.view.DragFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/df_content"
tools:context="com.windfindtech.hqdemo.MainActivity">

<ImageView
 android:id="@+id/iv1"
 android:layout_width="100dp"
 android:layout_height="100dp"
 android:src="@mipmap/ic_launcher" />

<TextView
 android:id="@+id/tv1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:text="可拖拽文字" />
</com.windfindtech.hqdemo.view.DragFrameLayout>

MainActivity.java类


public class MainActivity extends AppCompatActivity {

DragFrameLayout m_dragFrameLayout;
ImageView m_imageView1;
TextView m_textView1;
String TAG = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 m_dragFrameLayout = (DragFrameLayout) findViewById(R.id.df_content);
 m_imageView1 = (ImageView)findViewById(R.id.iv1);
 m_textView1 = (TextView) findViewById(R.id.tv1);
//   m_dragFrameLayout.addDragChildView(m_imageView1);
 m_dragFrameLayout.addDragChildView(m_textView1);//具体拖拽动作使用回调即可
}
}

来源:http://www.jianshu.com/p/94477b804136

标签:android,拖动view
0
投稿

猜你喜欢

  • Flutter验证码输入框的2种方法实现

    2023-07-17 16:22:07
  • Java实例讲解多态数组的使用

    2021-08-30 19:34:46
  • C#中的虚方法和抽象方法的运用

    2023-06-02 15:49:40
  • 当Mybatis遇上目录树超全完美解决方案

    2021-09-28 16:21:13
  • C#如何调用MFC 窗口 DLL

    2022-04-14 09:57:51
  • Java 数据结构进阶二叉树题集下

    2022-07-11 19:16:18
  • Java中方法的使用、重载与递归的详细介绍

    2022-03-02 02:50:05
  • 将来会是Python、Java、Golang三足鼎立的局面吗

    2023-11-22 09:18:31
  • Android调用外置摄像头的方法

    2021-10-19 01:25:13
  • Java实现读取163邮箱,qq邮箱的邮件内容

    2021-09-27 14:26:31
  • 教你创建一个带诊断工具的.NET镜像

    2021-09-28 03:11:11
  • Java聊天室之实现获取Socket功能

    2023-09-19 03:57:10
  • C#实现开机自动启动设置代码分享

    2021-10-08 20:45:14
  • JavaWeb建立简单三层项目步骤图解

    2023-03-08 16:51:02
  • Android TextWatcher监控EditText中的输入内容并限制其个数

    2022-08-18 13:27:41
  • MyBatis-Plus自动填充功能失效导致的原因及解决

    2023-05-11 13:16:08
  • C#中的时间显示格式(12小时制VS24小时制)

    2021-10-19 12:58:41
  • C#控制台程序中使用官方依赖注入的实现

    2021-07-12 15:35:09
  • C#绘制中国象棋棋盘

    2021-05-27 15:08:44
  • Android应用自动更新功能实现的方法

    2022-12-02 07:01:53
  • asp之家 软件编程 m.aspxhome.com