dragView

  • 因项目新需求需要添加一个屏幕拖拽按钮可以弹出菜单的控件,因为不是我做的闲来无事写一个demo吧 可能存在一些小bug(毕竟就写了几个小时)兄弟姐妹们理解思路就行 具体的可以自己调试一下 废话不多说先来一个gif走一走(调了帧数 可能看着比较快 不要介意)

  • GitHub 传送车https://github.com/guanhaoran/dragView

  • gif有点小 对付看吧 (太大了传github的时候 会出错 放图片还看不出来效果 不要介意)

  • 废话不多话直接看代码

    1.写一个类继承 Button 重写构造方法&onTouchEvent(MotionEvent event)

      public DragView(Context context) {
    this(context, null); }
    public DragView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
    }
    public DragView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    this.context = context;
    //获取屏幕宽高,用于控制控件在屏幕内移动
    DisplayMetrics dm = getResources().getDisplayMetrics();
    mScreenWidth = dm.widthPixels;
    mScreenHeight = dm.heightPixels - 100;//这里减去的100是下边的back键和menu键那一栏的高度,看情况而定
    showPopup = new ShowPopup(context);
    }

    2.onTouchEvent(MotionEvent event)

    大家可能看的不太懂 先大体看下结构 稍后我会在代码里加上注释 确保每条代码都有注释一些没有的 我就不会加注释 也浪费大家时间

      public boolean onTouchEvent(MotionEvent event) {
    super.onTouchEvent(event); int lastMoveX = 0;
    int lastMoveY = 0;
    switch (event.getAction()) { case MotionEvent.ACTION_DOWN:
    falg = true;
    startDownX = (int) event.getRawX();
    startDownY = (int) event.getRawY();
    break;
    case MotionEvent.ACTION_MOVE:
    if (showPopup.isShowing()) {
    showPopup.dissPopup();
    }
    lastMoveX = (int) event.getRawX();
    lastMoveY = (int) event.getRawY(); int dx = lastMoveX - startDownX;
    int dy = lastMoveY - startDownY;
    if (Math.abs(dx) > 2 || Math.abs(dy) > 2) {
    falg = false;
    } left = getLeft() + dx;
    top = getTop() + dy;
    right = getRight() + dx;
    bottom = getBottom() + dy;
    if (left < 0) {
    left = 0;
    right = left + getWidth();
    }
    if (right > mScreenWidth) {
    right = mScreenWidth;
    left = right - getWidth();
    }
    if (top < 0) {
    top = 0;
    bottom = top + getHeight();
    }
    if (bottom > mScreenHeight) {
    bottom = mScreenHeight;
    top = bottom - getHeight();
    }
    layout(left, top, right, bottom);
    Log.i("____________________", left + "___" + top + "___" + right + "___" + bottom);
    invalidate();
    startDownX = (int) event.getRawX();
    startDownY = (int) event.getRawY();
    break;
    case MotionEvent.ACTION_UP:
    int lastMoveDx = Math.abs((int) event.getRawX() - startDownX);
    int lastMoveDy = Math.abs((int) event.getRawY() - startDownY);
    if (falg) {
    ToastUtils.showToast(context, "我点击了");
    showContent(360, true);
    }
    falg = false;
    break;
    }
    return true;

    3.点击按钮出现的一个小圆点 这个使用popupWindow做的包括背景颜色变暗也是在popupWindow中实现的 这个稍后也会有注释 我只是写完了 不喜欢写注释

    (这个毛病很坏 - -)

      private void showContent(int angles, boolean isShow) {
    if (showPopup.isShowing()) {
    showPopup.dissPopup();
    } else {
    int excursionX = -(getWidth() / 2) - 5;
    int excursionY = 0;
    if (left == 0 && right == 0 && top == 0 && bottom == 0) {
    excursionX = -(getWidth() / 2)-5;
    excursionY = 0;
    } else if (left < 50 && top < 50) {
    excursionX = getWidth();
    excursionY = 0;
    } else if (mScreenWidth - right < 50 && top < 50) {
    excursionX = -getWidth() * 2 - 20;
    excursionY = 0;
    } else if (mScreenWidth - right < 50 && mScreenHeight - bottom < getHeight()*2) {
    excursionX = -getWidth() * 2 - 20;
    excursionY = -getHeight() * 3 - 20;
    } else if (mScreenHeight - bottom < getHeight()*2 && left < 50) {
    excursionX = getWidth();
    excursionY = -getHeight() * 3 - 20;
    } else if (left < 50) {
    excursionX = getWidth();
    excursionY = -getHeight() - getHeight() / 2;
    } else if (top < 50) {
    excursionX = -(getWidth() / 2)-10;
    excursionY = -10;
    } else if (mScreenWidth - right < 50) {
    excursionX = -getWidth() * 2 - 10;
    excursionY = -getHeight() - getHeight() / 2 - 10;
    } else if (mScreenHeight - bottom < getHeight() * 2.5) {
    excursionX = -getWidth() / 2 -10;
    excursionY = -getHeight() * 3 - 10;
    } else {
    }
    showPopup.showPopup(this, excursionX, excursionY);
    }
    }

    4.还有一个popupWindow 我就不贴在这里了 大家可以看代码就好了,可能代码包结构很乱大家不要介意,我也是为了效果别的我也没注意

  • 最后我在上传GitHub的时候 哇!!!很痛苦 全是英文(奈何我的英文还不好)传了好久才传上去 下一篇文章我会把GitHub的使用给兄弟们详详细细的说一遍

  • GitHub 传送车https://github.com/guanhaoran/dragView

QQ:765307272

dragView 屏幕拖拽并且弹出菜单的控件的更多相关文章

  1. asp.net 弹出式日历控件 选择日期 Calendar控件

    原文地址:asp.net 弹出式日历控件 选择日期 Calendar控件 作者:逸苡 html代码: <%@ Page Language="C#" CodeFile=&quo ...

  2. 2015-11-04 asp.net 弹出式日历控件 选择日期 Calendar控件

    html代码: <%@ Page Language="C#" CodeFile="calendar.aspx.cs" Inherits="cal ...

  3. Web之-----弹出确认框控件应用

    引用文件!-------- <link rel="stylesheet" type="text/css" href="@Url.FrontUrl ...

  4. Swing-JPopupMenu弹出菜单用法-入门

    弹出菜单是GUI程序中非常常见的一种控件.它通常由鼠标右击事件触发,比如在windows系统桌面上右击时,会弹出一个包含“刷新”.“属性”等菜单的弹出菜单.Swing中的弹出菜单是JPopupMenu ...

  5. win32进阶之路:程序托盘图标+右键弹出菜单

     开场白 本次介绍两个非常棒且实用的技巧:程序托盘图标和右键弹出菜单,效果如下图. 程序托盘图标用了迅雷的图标,右键点击时候会弹出三个选项的菜单. 程序托盘图标设置 我会用尽可能清晰明了的步骤介绍方式 ...

  6. 【Android】创建Popwindow弹出菜单的两种方式

    方法一的Activity package com.app.test02; import android.app.Activity; import android.os.Bundle; import a ...

  7. 【转】android创建Popwindow弹出菜单的两种方式

    方法一的Activity package com.app.test02; import android.app.Activity; import android.os.Bundle; import a ...

  8. iOS 弹出菜单UIMenuController的基本使用

    UIMenuController,弹出菜单@implementation DragView{    CGPoint startLocation;    CGFloat rotation;}-(inst ...

  9. 用PopupWindow实现弹出菜单(弹出的菜单采用自定义布局)

         用PopupWindow实现弹出菜单是一个比较好的方式.当然我们还有一个类PopupMenu也能实现弹出菜单,但那个太过于局限了,所以不是很推荐. 这个实例的效果是这样的:点击按钮后,一个菜 ...

随机推荐

  1. WP 图片加载时显示加载中

    private BitmapImage srcimage = new BitmapImage(); public MainPage() { InitializeComponent(); progres ...

  2. HighCharts 图表插件 自定义绑定 时间轴数据

    HighCharts 图表插件 自定义绑定 时间轴数据,解决时间轴自动显示数据与实际绑定数据时间不对应问题! 可能要用到的源码片段:http://code.662p.com/list/14_1.htm ...

  3. Boost字符串处理

    (1):Boost学习之格式化输出--format: 原文链接:http://www.cnblogs.com/lzjsky/archive/2011/05/05/2037327.html 此文非常详细 ...

  4. 将数据库返回的ResultSett转换成List装Map形式的方法(ResultSetToList)

    多时候想做一些关于数据库实验,如果先搭建框架太麻烦,直接得到ResultSet处理起来取值什么的也很繁琐,为此我做了一个将ResultSet转换成List<Map<String,Objec ...

  5. Python笔记17---------魔法方法

    魔法方法也为特殊方法,即用两个下划线形成的(__方法__).自己定义的方法最好不要采用这种方式,因为这些方法会在一些特殊的情况下直接被调用. 1.第一个魔法方法:类中常用的__init__()方法:相 ...

  6. sql2008删除语句.txt

    delete from [aixinxing].[dbo].[bbs2] where announceID in (select AnnounceID from [aixinxing].[dbo].[ ...

  7. 51nod-独木舟问题

    n个人,已知每个人体重,独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人.显然要求总重量不超过独木舟承重,假设每个人体重也不超过独木舟承重,问最少需要几只独木舟?分析:  一个显然的策略 ...

  8. linux 中配置假域名来测试

    1.linux中配置假域名 找到hosts文件进行编辑 命令:vim /etc/hosts 配置: #centos(本机IP)192.168.1.179 www.imooc.com(假域名,自己设置) ...

  9. 数据库优化一般思路(PLSQL、Navicat)

    SQL执行过程: 1.执行SQL时,sql解析引擎会被启动 2.数据类型和数据库表定义的数据类型不一致,数据库引擎会自动转化 3.数据库表定义了多个索引,sql引擎会帮你选择最优的一个 4.数据库引擎 ...

  10. String与StringBuffer与StringBuilder

    package test; public class Test { public static void main(String[] args) { StringBuffer sb = new Str ...