像360悬浮窗那样,用WindowManager实现炫酷的悬浮迷你音乐盒(下)
来源:互联网 发布:淘宝客怎么关闭 编辑:程序博客网 时间:2024/05/21 19:37
在上一篇文章像360悬浮窗那样,用WindowManager实现炫酷的悬浮迷你音乐盒(上)中我粗粗的向大家介绍了WindowManager和WindowManager.LayoutParams,讲的都是理论知识,现在我们就要动起手来,着手开发炫酷的悬浮迷你音乐盒了。先上效果图:
怎么样,是否打动你继续往下看呢?
如果对WindowManager没有接触过得小伙伴,建议先看我的上一篇文章,因为,两篇连着一起看才好看哦。其实实现这么一个悬浮迷你音乐盒的功能也并不难,首先需要两个布局,一个就是像效果图中展示的可以四处拖动的View的布局我们暂且称它为floatView,它的实现很简单其实就是我之前讲过的用RotateDrawable实现网易云音乐唱片机效果的迷你版。另一个就是点击floatView后跳出的歌曲控制菜单的布局,我们可以叫它playerView。
floatView.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent" > <RelativeLayout android:background="@drawable/shape_background_light" android:layout_centerInParent="true" android:layout_height="52dp" android:layout_width="52dp" > <ImageView android:id="@+id/mini_cd" android:src="@drawable/rotate_cd" android:layout_width="match_parent" android:layout_height="match_parent" /> <ImageView android:id="@+id/mini_hander" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/rotate_hander" /> </RelativeLayout></RelativeLayout>
playerView.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_height="match_parent" android:layout_width="match_parent" > <LinearLayout android:background="@drawable/shape_background_dark" android:layout_centerInParent="true" android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="vertical" android:paddingRight="12dp" android:paddingLeft="12dp" android:paddingTop="8dp" android:gravity="center" > <TextView android:layout_height="wrap_content" android:id="@+id/player_displayname" android:focusableInTouchMode="true" android:ellipsize="marquee" android:layout_width="180dp" android:textColor="#11CD6E" android:singleLine="true" android:focusable="true" tools:text="浅唱 - 许嵩" android:gravity="center" android:textSize="18sp" android:text="N/A" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/player_progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_marginLeft="8dp" android:layout_marginTop="3dp" tools:text="04:15" android:text="--:--" android:textColor="#11CD6E" android:textSize="8sp" /> <TextView android:id="@+id/player_duration" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="8dp" android:layout_alignParentRight="true" android:textColor="#11CD6E" android:layout_marginTop="3dp" android:text="--:--" tools:text="12:45" android:textSize="8sp" /> </RelativeLayout> <SeekBar android:id="@+id/player_seek" tools:progress="50" android:progress="0" android:max="100" tools:secondaryProgress="90" android:paddingBottom="3dp" android:progressDrawable="@drawable/layer_seekbar" android:thumb="@drawable/shape_thumb" android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_weight="1" android:minHeight="2dp" android:maxHeight="2dp" /> <LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" android:padding="6dp" > <ImageButton android:background="@null" android:layout_width="36dp" android:layout_height="36dp" android:id="@+id/player_previous" android:src="@mipmap/landscape_player_btn_pre_press" /> <ImageButton android:background="@null" android:layout_width="48dp" android:layout_height="48dp" android:id="@+id/player_play" android:layout_marginLeft="18dp" android:layout_marginRight="18dp" android:src="@mipmap/landscape_player_btn_play_press" /> <ImageButton android:background="@null" android:layout_width="36dp" android:layout_height="36dp" android:id="@+id/player_next" android:src="@mipmap/landscape_player_btn_next_press" /> </LinearLayout> </LinearLayout></RelativeLayout>
布局写好,接着就是写一个类来控制这两个View之间的转换以及一些属性的设置,代码确实有点,全贴出来的话,相信不少人都会看厌、看烦,我还是选择其中比较有代表性的代码,再进行一番说明,可能更有助于大家理解我写这个Demo时思考的角度,老规矩在文章末尾我会附上源码下载地址,有兴趣的小伙伴可以下载研究。
floatView的设置
public void setFloatView(View floatView) { if(floatView != null) { this.mFloatView = floatView; setContentView(mFloatView); }}
首先,floatView的设置非常简单。至于setContentView(mFloatView),是因为悬浮窗默认首先显示的是小窗口,所以在设置MenuView的时候,就将窗口的布局设置为floatView。
playerView的设置
public void setPlayerView(View PlayerView) { if(PlayerView != null) { BackgroundView backgroundView = new BackgroundView(getContext()); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT); PlayerView.setOnTouchListener(new TouchIntercept()); PlayerView.setLayoutParams(layoutParams); backgroundView.addView(PlayerView); this.mPlayerView = backgroundView; }}
menuView的设置相对于floatView的设置复杂一些,大家可以看到,我为添加进来的MenuView包裹了一层BackgroundView,而这一点正是从PopupWindow的源码中借鉴而来,BackgroundView是一个自定义内部类,继承至RelativeLayout,并且重写按键点击监听事件和触摸事件,主要是用于PlayerView的返回键关闭功能和外部点击关闭功能。
内部类BackgroundView
class BackgroundView extends RelativeLayout { public BackgroundView(Context context) { super(context); } @Override public boolean dispatchKeyEvent(KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { if (getKeyDispatcherState() == null) { return super.dispatchKeyEvent(event); } if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) { final KeyEvent.DispatcherState state = getKeyDispatcherState(); if (state != null) { state.startTracking(event, this); } return true; } else if (event.getAction() == KeyEvent.ACTION_UP) { final KeyEvent.DispatcherState state = getKeyDispatcherState(); if (state != null && state.isTracking(event) && !event.isCanceled()) { closeMenu(); return true; } } return super.dispatchKeyEvent(event); } else { return super.dispatchKeyEvent(event); } } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_UP: closeMenu(); return true; case MotionEvent.ACTION_OUTSIDE: closeMenu(); return true; } return super.onTouchEvent(event); }}
ContentView的初始化准备
private void createContentView(View contentView) { this.mContentView = contentView; contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); // 主动计算视图View的宽高信息 offsetY = getStatusBarHeight(getContext()) + contentView.getMeasuredHeight() / 2; offsetX = contentView.getMeasuredWidth() / 2; contentView.setOnTouchListener(new WindowTouchListener());}
由于我们要实现的悬浮迷你音乐盒,是需要在两个视图之间进行切换的,所以在每次视图切换之前,我都会对先要加载的视图进行一个准备,由代码contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
可以看到,主动计算出当前View的宽高信息,还有就是getStatusBarHeight(getContext())
计算设备状态栏高度的方法,因为在窗口移动的时候,默认x、y指定的是窗口左上角的坐标,这样就涉及到一个偏移量的问题,在排除偏移量问题后,我们的坐标就会精确指定窗口的正中心。而且在代码中能够明显看出,我对contentView设置了一个onTouch触摸事件,主要是实现窗口的滑动,具体实现在后面会有介绍。
WindowManager.LayoutParams初始化配置
private void initLayoutParams() { getLayoutParams().flags = getLayoutParams().flags | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL; getLayoutParams().dimAmount = 0.2f; getLayoutParams().type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; getLayoutParams().height = WindowManager.LayoutParams.WRAP_CONTENT; getLayoutParams().width = WindowManager.LayoutParams.WRAP_CONTENT; getLayoutParams().gravity = Gravity.LEFT | Gravity.TOP; getLayoutParams().format = PixelFormat.RGBA_8888; getLayoutParams().alpha = 1.0f; offsetX = 0; offsetY = getStatusBarHeight(getContext()); getLayoutParams().x = (int) (mDisplayMetrics.widthPixels - offsetX); getLayoutParams().y = (int) (mDisplayMetrics.heightPixels * 1 / 4 - offsetY);}
由上篇文章介绍,WindowManager.LayoutParams控制着窗口显示的策略,因此在窗口显示之前,我们要完成对WindowManager.LayoutParams的配置。由上面的代码看出,窗口类型是系统提示窗口,也就上篇文章中说到的可以显示在任何视图之上的那种窗口类型,窗口的宽高是自适应。对齐方式是左上角对齐。窗口的透明度为1.0,也就是不透明。虽然窗口并没有设置属性FLAG_NOT_FOCUSABLE
,也就是说我们的窗口默认是设置焦点的,但是由于我设置FLAG_WATCH_OUTSIDE_TOUCH
属性的缘故,窗口外的区域,还是能够自由接收点击触摸事件的。
视图切换
private void setContentView(View contentView) { if(contentView != null) { if(isShowing()) { getWindowManager().removeView(mContentView); createContentView(contentView); getWindowManager().addView(mContentView, getLayoutParams()); updateLocation(getDisplayMetrics().widthPixels / 2, getDisplayMetrics().heightPixels / 2 ,true); } else { createContentView(contentView); } }}
从代码中可以看出,视图切换用到的上篇文章中介绍到的视图的移除和视图的添加,是的,想要进行视图新的切换,首先要移除原有的视图,并将需要显示的视图添加进来。
窗口位置更新
private void updateLocation(float x, float y, boolean offset) { if(getContentView() != null) { if(offset) { getLayoutParams().x = (int) (x - offsetX); getLayoutParams().y = (int) (y - offsetY); } else { getLayoutParams().x = (int) x; getLayoutParams().y = (int) y; } getWindowManager().updateViewLayout(mContentView, getLayoutParams()); }}
在视图切换
中用到了窗口的移除和窗口的添加,而在位置更新
中用到的是WindowManger三个方法中的第三个方法窗口更新,通过传入我们需要重新设置WindowManager.LayoutParams中的x、y位置坐标,然后更新窗口,就能实现窗口位置的更新操作。
contentView的触摸事件
private final float DISTANCE = 15.0f;class WindowTouchListener implements View.OnTouchListener { @Override public boolean onTouch(View view, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if(!isOpen) { down(event); } break; case MotionEvent.ACTION_MOVE: if(!isOpen) { move(event); } break; case MotionEvent.ACTION_UP: if(!isOpen) { up(event); } break; case MotionEvent.ACTION_OUTSIDE: if(isOpen) { closeMenu(); return true; } break; default: break; } return false; }}/** * 按下事件处理 * */private void down(MotionEvent event) { downX = event.getRawX(); downY = event.getRawY();}/** * 移动事件处理 * */private void move(MotionEvent event) { updateLocation(event.getRawX(), event.getRawY(), true);}/** * 抬起事件处理 * */private void up(MotionEvent event) { float x = event.getRawX(); float y = event.getRawY(); if(x >= downX - DISTANCE && x <= downX + DISTANCE && y >= downY - DISTANCE && y <= downY + DISTANCE) { if(System.currentTimeMillis() - downTimeMillis > 1200) { // 长按 } else { openMenu(); //点击 } } else { ValueAnimator animator = alignAnimator(x, y); animator.start(); }}
floatView的触摸监听事件非常简单,就是根据手指触摸的坐标位置实时刷新窗口的坐标位置,当up坐标和dowm坐标的区域在预先设置好的合理范围内,触摸事件超过1秒钟则视为长按事件,小于1秒钟则视为点击事件,移除floatView,添加playerView。注意区别MotionEvent的getX、getY和getRawX、getRawY方法,getX、getY点击位置在视图内的坐标,getRawX、getRawY则是点击位置相对于整个屏幕的坐标
打开PlayerView
public void openMenu() { if(isOpen) { return ; } getLayoutParams().height = WindowManager.LayoutParams.MATCH_PARENT; getLayoutParams().width = WindowManager.LayoutParams.MATCH_PARENT; oldX = getLayoutParams().x; oldY = getLayoutParams().y; setContentView(mPlayerView); isOpen = true;}
打开playerView后,我设置的的窗口大小是占满整个屏幕,这样就能完全捕捉到BackgroundView中设置的触摸事件,实现点击窗口外部关闭的效果。oldX、oldY则用于记录floatView移除前的坐标,方便在重新显示的时候定位。
关闭PlayerView
public void closeMenu() { if(!isOpen) { return ; } isOpen = false; getLayoutParams().height = WindowManager.LayoutParams.WRAP_CONTENT; getLayoutParams().width = WindowManager.LayoutParams.WRAP_CONTENT; setContentView(mFloatView); updateLocation(oldX, oldY, false);}
关闭PlayerView的方法也很简单,就是重新将试图切换为floatView,设置窗口大小为自适应大小,并定位在之前视图记录的原有坐标。
到这里炫酷的悬浮音乐盒的实现也就实现的差不多了,其实几个在效果图中能够看到的样子没有介绍,例如:自定义SeekBar样式的使用,悬浮窗移动时,手指离开屏幕,悬浮穿自动依附到屏幕两侧的过渡动画,还有就是在3秒钟左右悬浮窗没有任何操作是变得透明的效果的实现。想要了解这些是怎么实现的小伙伴,可以猛戳下方链接下载源码进行研究哦!!
说明:无意间发现之前发布的源码存在问题,不知道已经下载的小伙伴有没有发现。就是在我们悬浮窗(迷你状态)显示的时候,点击任意应用的输入框,也是不会弹出输入法的,因为焦点让我们的悬浮窗抢占了。哈哈,问题已经整改,然后再加上之前逻辑上的一些错误,统统也都整改。再一个,为了减小demo包的大小,我音频播放的是之前写毕设用的后台在线音乐,所以使用demo的时候请确保网络处于连接状态!!
Github地址
如果文中有表述不当或阐述错误的地方,还望正在看文章的您可以帮忙指出,有疑惑也可以在评论区提问或者私信,期待您的意见和建议,欢迎关注交流。
原文链接:http://www.jianshu.com/p/7bb5dbaff7de
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
- 像360悬浮窗那样,用WindowManager实现炫酷的悬浮迷你音乐盒(下)
- 像360悬浮窗那样,用WindowManager做一个炫酷的悬浮迷你音乐盒(下)
- 像360悬浮窗那样,用WindowManager做一个炫酷的悬浮迷你音乐盒(上)
- 像360悬浮窗那样,用WindowManager做一个炫酷的悬浮迷你音乐盒(上)
- 做一个炫酷的悬浮迷你音乐盒
- WindowManager实现悬浮窗
- Android实现类似360,QQ管家那样的悬浮窗
- 使用WindowManager实现悬浮窗
- 利用WindowManager实现悬浮窗
- 使用WindowManager实现悬浮窗
- WindowManager 实现悬浮窗 详解
- 悬浮窗的实现(如360悬浮窗效果)
- 悬浮窗的实现(如360悬浮窗效果)
- WindowManager实现悬浮窗口
- WindowManager实现悬浮窗口&可自由移动的悬浮窗口
- WindowManager实现悬浮窗口&可自由移动的悬浮窗口
- WindowManager实现悬浮窗口&可自由移动的悬浮窗口
- Android WindowManager悬浮窗:不需要申请权限实现悬浮
- linux下分析Java程序内存汇总
- 手机内置U盾可行性分析
- canvas中圆的角度和弧度
- HDU1024——Max Sum Plus Plus(dp)
- .tpl模板
- 像360悬浮窗那样,用WindowManager实现炫酷的悬浮迷你音乐盒(下)
- jquery ajax 从后台获取数据到界面
- 统计一串字符串中不同的词的个数
- Codeforces Round #366 (Div. 2) 题解
- TREC2016 Real-time Summarization 总结
- JavaScript-cookie
- C语言中的冒泡排序(2种)和选择排序
- Android WebView 与js交互初探
- javascript全文检索工具