dragView 屏幕拖拽并且弹出菜单的控件
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 屏幕拖拽并且弹出菜单的控件的更多相关文章
- asp.net 弹出式日历控件 选择日期 Calendar控件
原文地址:asp.net 弹出式日历控件 选择日期 Calendar控件 作者:逸苡 html代码: <%@ Page Language="C#" CodeFile=&quo ...
- 2015-11-04 asp.net 弹出式日历控件 选择日期 Calendar控件
html代码: <%@ Page Language="C#" CodeFile="calendar.aspx.cs" Inherits="cal ...
- Web之-----弹出确认框控件应用
引用文件!-------- <link rel="stylesheet" type="text/css" href="@Url.FrontUrl ...
- Swing-JPopupMenu弹出菜单用法-入门
弹出菜单是GUI程序中非常常见的一种控件.它通常由鼠标右击事件触发,比如在windows系统桌面上右击时,会弹出一个包含“刷新”.“属性”等菜单的弹出菜单.Swing中的弹出菜单是JPopupMenu ...
- win32进阶之路:程序托盘图标+右键弹出菜单
开场白 本次介绍两个非常棒且实用的技巧:程序托盘图标和右键弹出菜单,效果如下图. 程序托盘图标用了迅雷的图标,右键点击时候会弹出三个选项的菜单. 程序托盘图标设置 我会用尽可能清晰明了的步骤介绍方式 ...
- 【Android】创建Popwindow弹出菜单的两种方式
方法一的Activity package com.app.test02; import android.app.Activity; import android.os.Bundle; import a ...
- 【转】android创建Popwindow弹出菜单的两种方式
方法一的Activity package com.app.test02; import android.app.Activity; import android.os.Bundle; import a ...
- iOS 弹出菜单UIMenuController的基本使用
UIMenuController,弹出菜单@implementation DragView{ CGPoint startLocation; CGFloat rotation;}-(inst ...
- 用PopupWindow实现弹出菜单(弹出的菜单采用自定义布局)
用PopupWindow实现弹出菜单是一个比较好的方式.当然我们还有一个类PopupMenu也能实现弹出菜单,但那个太过于局限了,所以不是很推荐. 这个实例的效果是这样的:点击按钮后,一个菜 ...
随机推荐
- Linux vi命令快操作汇总
第一部份:一般指令模式可用的按钮說明,游标移动.复制粘贴.搜寻取代等 一.移动游标的方法h 或 向左方向鍵(←) 游标向左移动一个字节j 或 向下方向鍵(↓) 游标向下移动一个字节k 或 向上方向 ...
- 我的C++笔记(类与对象)
/* * Main.cpp * * Created on: 2015-7-24 * Author: feiruo */ /* * 类与对象: * * 1.抽象: * 面向对象方法中的抽象,是指对具体问 ...
- Block Functionality
Block Functionality A block is an anonymous inline collection of code that: Has a typed argument lis ...
- VS Code中编写html(4) 标签的宽高颜色背景设置
1 <!+Tab键--> <!--有两个div标签时,分别设置style,有两种方法--> <div id="div1">第一个div标签:& ...
- RemoveAll测试
foreach (var item in procode) { var reslit = LoadData((string)item.ProductCode.Trim(), item.product_ ...
- Mysql笔记2-----重要小点
1.逻辑删除:
- Project Euler 31 1000-digit Fibonacci number( DP )
题意:在无限硬币的情况下能组成200的方案数有多少个 思路:DP,设数组 dp[ n ] [ k ] 代表前 n 种硬币能够组成 k 元的方案数,那么就能得到 dp [ n ] [ k ] = dp ...
- [USACO4.2] 草地排水 Drainage Ditches (最大流)
题目背景 在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫约翰修建了一套排水系统来使贝茜的草地免除被大水淹没 ...
- Django安装部署
MVC模式说明 Model:是应用程序中用于处理应用程序数据逻辑的部分,通常模型对象负责在数据库中存取数据 View: 是应用程序中处理数据显示的部分,通常视图是依据模型数据创建的 Controlle ...
- 编译htop
git clone https://github.com/hishamhm/htop cd htop ./autogen.sh ./configure make make install