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

 更新时间:2017年10月11日 10:22:24   作者:赖床的猫  
本篇文章主要介绍了Android实现单页面浮层可拖动view的示例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

使用到的技术:ViewDragHelper

效果如图:

封装好的类(继承自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);//具体拖拽动作使用回调即可
}
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android自定义控件实现时钟效果

    Android自定义控件实现时钟效果

    这篇文章主要介绍了Android自定义控件实现时钟效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-12-12
  • Android中Notification 提示对话框

    Android中Notification 提示对话框

    Notification,俗称通知,是一种具有全局效果的通知,它展示在屏幕的顶端,首先会表现为一个图标的形式,当用户向下滑动的时候,展示出通知具体的内容
    2016-01-01
  • Android编程之蓝牙测试实例

    Android编程之蓝牙测试实例

    这篇文章主要介绍了Android编程之蓝牙测试,较为详细的分析了Android蓝牙测试的相关运行环境与调试技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • Android 点击生成二维码功能实现代码

    Android 点击生成二维码功能实现代码

    二维码,我们也称作QRCode,QR表示quick response即快速响应,在很多App中我们都能见到二维码的身影,最常见的莫过于微信了。接下来给大家介绍android 点击生成二维码功能实现代码,需要的朋友参考下吧
    2017-11-11
  • Android与JS之间跨平台异步调用实例详解

    Android与JS之间跨平台异步调用实例详解

    这篇文章主要介绍了Android与JS之间跨平台异步调用实例详解的相关资料,需要的朋友可以参考下
    2016-11-11
  • Android自定义仿ios加载弹窗

    Android自定义仿ios加载弹窗

    这篇文章主要为大家详细介绍了Android自定义仿ios加载弹窗,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-05-05
  • Android自定义带增长动画和点击弹窗提示效果的柱状图DEMO

    Android自定义带增长动画和点击弹窗提示效果的柱状图DEMO

    这篇文章主要介绍了Android自定义带增长动画和点击弹窗提示效果的柱状图的相关资料,非常不错具有一定的参考借鉴价值,需要的朋友可以参考下
    2016-11-11
  • Android调用第三方QQ登录代码分享

    Android调用第三方QQ登录代码分享

    现在的项目开发,调用第三方登录,几乎是必须的,这篇文章主要介绍了Android调用第三方QQ登录代码分享
    2016-05-05
  • Android截屏分享功能

    Android截屏分享功能

    最近项目经理交给我一个任务,要求实现android截屏功能,包括Android截屏获取图片、将图片保存到本地、通知系统相册更新、通过微信、QQ、微博分享截屏图片。小编把实现思路分享到脚本之家平台,需要的朋友参考下
    2017-12-12
  • Android仿微信通讯录列表侧边栏效果

    Android仿微信通讯录列表侧边栏效果

    这篇文章主要为大家详细介绍了Android仿微信通讯录列表侧边栏效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-02-02

最新评论