欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-17 13:39:52
需求是需要在一个已经存在的页面添加一个可拖动的浮层广告。 使用到的技术:viewdraghelper 效果如图: 封装好的类(继承自framelayout)...

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

使用到的技术: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);//具体拖拽动作使用回调即可
}
}

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

上一篇:

下一篇: