Android 模仿苹果虚拟悬浮按钮(自动靠边、可浮现任何界面上)
由于最近小蔡的手机音量键坏了,调节音量有点麻烦,突发奇想,想自己实现一个快捷键来调节音量。在忘上参考了一些代码,总结出一般本章,分享给大家。
首先 按钮要想实现悬浮在任何界面,那么必须是要写在服务里面的,使用定时器,2.5s不触摸后,背景变淡
清单文件中加权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
public class FloatViewService extends Service {
private static final String TAG = "FloatViewService";
// 定义浮动窗口布局
private LinearLayout mFloatLayout;
private WindowManager.LayoutParams wmParams;
// 创建浮动窗口设置布局参数的对象
private WindowManager mWindowManager;
private ImageButton mFloatView;
private int screenHeight;
private int screenWidth;
private MyCountDownTimer myCountDownTimer;
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate");
screenHeight = ScreenParam.getInstance().height;//自己写的工具类用来获取屏幕宽高。
screenWidth = ScreenParam.getInstance().width;
createFloatView();
myCountDownTimer = new MyCountDownTimer(, ); //设置计时2.5s
myCountDownTimer.start();
}
@SuppressWarnings("static-access")
@SuppressLint("InflateParams")
private void createFloatView() {
wmParams = new WindowManager.LayoutParams();
// 通过getApplication获取的是WindowManagerImpl.CompatModeWrapper
mWindowManager = (WindowManager) getApplication().getSystemService(
getApplication().WINDOW_SERVICE);
// 设置window type
wmParams.type = LayoutParams.TYPE_PHONE;
// 设置图片格式,效果为背景透明
wmParams.format = PixelFormat.RGBA_8888;
// 设置浮动窗口不可聚焦(实现操作除浮动窗口外的其他可见窗口的操作)
wmParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE;
// 调整悬浮窗显示的停靠位置为右侧底部
wmParams.gravity = Gravity.RIGHT | Gravity.BOTTOM;
// 以屏幕左上角为原点,设置x、y初始值,相对于gravity
wmParams.x = ;
wmParams.y = ;
// 设置悬浮窗口长宽数据
wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
LayoutInflater inflater = LayoutInflater.from(getApplication());
// 获取浮动窗口视图所在布局
mFloatLayout = (LinearLayout) inflater.inflate(
R.layout.alert_window_menu, null);
// 添加mFloatLayout
mWindowManager.addView(mFloatLayout, wmParams);
// 浮动窗口按钮
mFloatView = (ImageButton) mFloatLayout
.findViewById(R.id.alert_window_imagebtn);
mFloatLayout.measure(View.MeasureSpec.makeMeasureSpec(,
View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
.makeMeasureSpec(, View.MeasureSpec.UNSPECIFIED));
// 设置监听浮动窗口的触摸移动
mFloatView.setOnTouchListener(new OnTouchListener() {
boolean isClick;
private int leftDistance;
private float rawX;
private float rawY;
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mFloatLayout.setAlpha(1.0f);
myCountDownTimer.cancel();取消计时
rawX = event.getRawX();
rawY = event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
// getRawX是触摸位置相对于屏幕的坐标,getX是相对于按钮的坐标
int distanceX = (int) (event.getRawX()-rawX);
int distanceY = (int) (event.getRawY()-rawY);
leftDistance = (int) event.getRawX()
+ mFloatView.getMeasuredWidth() / ;
wmParams.x = wmParams.x-distanceX;
wmParams.y = wmParams.y-distanceY;
// 刷新
mWindowManager.updateViewLayout(mFloatLayout, wmParams);
rawX = event.getRawX();
rawY = event.getRawY();
break;
case MotionEvent.ACTION_UP:
myCountDownTimer.start();重新开始计时
if(wmParams.x>leftDistance){
wmParams.x = screenWidth-mFloatView.getMeasuredWidth() / ;
}else{
wmParams.x = ;
}
mWindowManager.updateViewLayout(mFloatLayout, wmParams);
break;
}
return false;
}
});
mFloatView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AudioManager audioManager = (AudioManager) getSystemService(Service.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
audioManager.getStreamVolume(AudioManager.STREAM_MUSIC), AudioManager.FLAG_SHOW_UI);
// Toast.makeText(FloatViewService.this, "hello!",
// Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
if (mFloatLayout != null) {
// 移除悬浮窗口
mWindowManager.removeView(mFloatLayout);
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
mFloatLayout.setAlpha(0.4f);
}
}
}
然后再Activity里启动服务就好了
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ScreenParam.getInstance().init(this);
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, FloatViewService.class);
//启动FloatViewService
startService(intent);
super.onStart();
}
如果不想显示Activity界面的话,可以在配置这个属性就ok了
android:theme="@android:style/Theme.NoDisplay"
就是这么简单!
Android 模仿苹果虚拟悬浮按钮(自动靠边、可浮现任何界面上)的更多相关文章
- android悬浮按钮(Floating action button)的两种实现方法
原文: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1028/1857.html 最近android中有很多新的设计规范被引入 ...
- android ——悬浮按钮及可交互提示
一.悬浮按钮 FloatingActionButton是Design Support中的一个控件,它会默认colorAccent作为按钮的颜色,还可以给按钮一个图标. 这是没有图标的,这是有图标的. ...
- Android用悬浮按钮实现翻页效果
今天给大家分享下自己用悬浮按钮点击实现翻页效果的例子. 首先,一个按钮要实现悬浮,就要用到系统顶级窗口相关的WindowManager,WindowManager.LayoutParams.那么在An ...
- Android 5.0新控件——FloatingActionButton(悬浮按钮)
Android 5.0新控件--FloatingActionButton(悬浮按钮) FloatingActionButton是5.0以后的新控件,一个悬浮按钮,之所以叫做悬浮按钮,主要是因为自带阴影 ...
- 013 Android ActionFloatingButton悬浮按钮组件与Snackbar组件使用
1.导入ActionFloatingButton组件(点击下载按钮,安装组件) 2,.ImageView图片XML设置 <ImageView android:id="@+id/imag ...
- Android FloatingActionButton(FAB) 悬浮按钮
FloatingActionButton 悬浮按钮 ...
- FloatingActionButtonDemo【悬浮按钮的使用,顺带snackBar的使用】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 FloatingActionButton简称FAB. 一. 对于App或某个页面中是否要使用FloatingActionButton ...
- android 自定义控件——(五)按钮点击变色
----------------------------------按钮点击变色(源代码下有属性解释)------------------------------------------------- ...
- Android模仿iOS iMessages10照片选择器的实现
不知不觉已经接近半年多没有写过博客了,这段时间,也是我刚好毕业走出校园的时间,由于学习工作的原因,一直没有真正静下心来写下些什么东西.这个星期刚入了小米笔记本pro的坑,本着新电脑新生活的理念嘻嘻-- ...
随机推荐
- Jasper:推送 API
ylbtech-Jasper:推送 API Control Center 还维护一个“推送 API”系统,一旦发生特定事件,就会向应用程序发送编程通知.例如,您可能选择在设备接近流量上限时收到通知.或 ...
- Springboot学习七 spring的一些注解
一 事务控制 @Service public class CityServiceImpl implements CityService { @Autowired private CityMapper ...
- web.xml中的<jsp-config>的用法详解
<jsp-config> 包括<taglib> 和<jsp-property-group> 两个子元素. 其中<taglib>元素在JSP 1.2时就已 ...
- MFC中界面自适应
void CMyDlg::OnSize(UINT nType, int cx, int cy){ CDialogEx::OnSize(nType, cx, cy); CRect rt; GetClie ...
- 【245】◀▶IEW-Unit10
Unit 10 Censorship 1. Model1题目及范文分析 Some parents believe that there is no harm in allowing their chi ...
- Robot FrameWork基础学习(二)
在Robot Framework中,测试套件(Test Suite)主要是存放测试案例,而资源文件(Resource)就是用来存放用户关键字. 内部资源:Resource 外部资源: External ...
- web API请求与参数获取
总结webAPI的常用请求方法与后台参数的获取: 一:get请求:(会将所以参数拼接到URL里面) 1:基础类型:string a=“hello” , 前端无论你是写到ajax里面的data属性还是 ...
- Jodd发送邮件
public static void main(String[] args) { Email email = Email.create().from("xxx") .to(&quo ...
- 【读后感1】SQL2008技术内幕- SQL逻辑查询处理
引言观点 1. 编程语言日新月异,但是从没有人否定sql 在现代编程中的巨大作用和 持续的可利用性.SQL以对人类友好的阅读体验提供数据查询能力( 相比其他编程语言 ), 同时在各种数据库平台中,基础 ...
- VM Fusion配置静态IP和物理机通讯
Vm虚拟机在WIndow系统上和物理机进行通讯很方便,但是在Mac上简直跟吃了屎一样难用的要死,物理机断了网以后还不能和虚拟机通讯, 如果在windows上做开发,也是简直和吃了屎一样,难用的要屎,这 ...