壁纸预览1.背景有的项目需要做壁纸功能就会用到壁纸预览功能。类似于手机上的壁纸预览功能选择图片-点击预览-桌面上的组件、应用图片名称等会出现在壁纸上。图1.实际桌面与壁纸预览界面2.现状分析现在桌面已经做成一个应用了覆盖在原生的桌面上不再走原生的wallpaper因此通过原生的wallpaper设置或获取壁纸属性不会生效。3.可行性方案让负责桌面开发的同事在预览壁纸开始前把桌面主界面的view组件进行截图传给负责预览功能的同事该同事再将需要预览的图片与截好的图进行叠加完成预览功能。流程如图1所示图2. 使用view截图完成壁纸预览功能流程图4.view的组件的截图关键代码private void screenShoot() { // 获取当前屏幕的组件截图 View rootView getActivity().getWindow().getDecorView().getRootView(); rootView.setDrawingCacheEnabled(true); Bitmap screenshot viewShot(ll_main_view); rootView.setDrawingCacheEnabled(false); // 将截图保存到文件 String imagePath Environment.getExternalStorageDirectory() /screenshot.png; FileOutputStream fos null; try { fos new FileOutputStream(imagePath); screenshot.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); } Log.d(TAG, screenShoot: ); } private Bitmap viewShot(LinearLayout linearLayout) { int h 0; Bitmap bitmap; for (int i 0; i linearLayout.getChildCount(); i) { h linearLayout.getChildAt(i).getHeight(); } bitmap Bitmap.createBitmap(linearLayout.getWidth(), h, Bitmap.Config.ARGB_8888); Canvas canvas new Canvas(bitmap); linearLayout.draw(canvas); return bitmap; }NULL图3.View截图效果图随便使用一个界面进行截图效果如图3所示截出来的图只显示组件背景是透明的有点像抠图效果。需要注意的是view截图只截传进去的应用的layout里面的组件不在layout里面的组件无法截出来也不会截取导航栏。5.截图参考文章参考文章Android 多种截屏方式介绍_android 截屏_Mr_Leixiansheng的博客-CSDN博客https://blog.csdn.net/Mr_Leixiansheng/article/details/103491099https://blog.csdn.net/Mr_Leixiansheng/article/details/103491099分屏小窗功能简介类似于下面这种播放视频退出视频播放页可以系统任意位置(除开一些特殊场景)进行小窗口播放视频。一般小窗口上具有播放暂停、上下曲、关闭、放大进入播放页、可拖动功能。图4. 视频小窗效果图方法一使用Android原生画中画这个是安卓原生提供的完成视频小窗功能的组件。介绍和demo地址如下图所示参考文章安卓PiP官方地址参考地址PiP官方demo地址优点1、方便使用。缺点1、点击小窗口的时候会闪一下不知道原因。2、需要底层支持现在framework.jar没有暴露pip相关接口自定义不了如果用这个需要framework那边加用这个就相当于把应用层的视频播放逻辑整到fw那边去了不方便应用层修改到时候出问题了都不好排查。3、有一些细细小小的bug看着不严重但影响体验又改不掉改不动。4、看了网上一圈和看demo很少有人用这个来实现画中画功能一堆人劝退。建议有其他方案选其他方案。参考文章Android画中画功能避坑指南方法二使用WindowManger绑定服 务实现系统级别的自定义悬浮小窗口使用方式1、添加悬浮窗功能权限uses-permission android:nameandroid.permission.SYSTEM_ALERT_WINDOW /2、使用WindowManger add一个viewpublic void showFloatingWindowView(Context context, String usbPath) { Log.d(TAG, showFloatingWindowView: ); mContext context; // 悬浮窗显示视图 mShowView initFloatView(); // 获取系统窗口管理服务 mWindowManager (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); // 悬浮窗口参数设置及返回 mFloatParams getParams(context); // 设置窗口触摸移动事件 mShowView.setOnTouchListener(new FloatViewMoveListener()); // 悬浮窗生成 mWindowManager.addView(mShowView, mFloatParams); init(usbPath); getUSBVideoControlTool.registerMediaStatusChangeListener(TAG, iMediaStatusChange); DeviceStatusBean.getInstance().addDeviceListener(deviceListener); initData(); }3、使用服务绑定这个弹窗让其成为系统弹窗可以在其他应用上面弹出public class FloatingWindowService extends Service { private static final String TAG FloatingWindowService; public static final String ACTION com.lwj.galleryapp.service; private VideoFloatingWindow mUSB0VideoFloatingWindow; private VideoFloatingWindow mUSB1VideoFloatingWindow; Nullable Override public IBinder onBind(Intent intent) { Log.d(TAG, onBind: ); return null; } Override public void onCreate() { super.onCreate(); Log.d(TAG, onCreate: ); ServiceUtils.startForegroundNotification(this, FloatingWindowService, FloatingWindowService); TempMemoryUtil.getInstance().registerVideoScreenTypeChangeListeners(iVideoScreenTypeChangeListener); } Override public void onDestroy() { super.onDestroy(); Log.d(TAG, onDestroy: ); TempMemoryUtil.getInstance().unRegisterVideoScreenTypeChangeListeners(iVideoScreenTypeChangeListener); } Override public int onStartCommand(Intent intent, int flags, int startId) { return super.onStartCommand(intent, flags, startId); } private void showUSB0FloatingWindow() { Log.d(TAG, showUSB0FloatingWindow: ); mUSB0VideoFloatingWindow new VideoFloatingWindow(); mUSB0VideoFloatingWindow.showFloatingWindowView(this, USBConstants.USBPath.USB0_PATH); } private void dismissUSB0FloatingWindow() { Log.d(TAG, dismissUSB0FloatingWindow: ); if (mUSB0VideoFloatingWindow ! null) { mUSB0VideoFloatingWindow.dismiss(); } } private void showUSB1FloatingWindow() { Log.d(TAG, showUSB1FloatingWindow: ); mUSB1VideoFloatingWindow new VideoFloatingWindow(); mUSB1VideoFloatingWindow.showFloatingWindowView(this, USBConstants.USBPath.USB1_PATH); } private void dismissUSB1FloatingWindow() { Log.d(TAG, dismissUSB1FloatingWindow: ); if (mUSB1VideoFloatingWindow ! null) { mUSB1VideoFloatingWindow.dismiss(); } } IVideoScreenTypeChangeListener iVideoScreenTypeChangeListener new IVideoScreenTypeChangeListener() { Override public void onUSB0CurrentVideoScreenType(int type) { if (type TempMemoryUtil.VIDEO_FULL_SCREEN) { dismissUSB0FloatingWindow(); } else if (type TempMemoryUtil.VIDEO_SPLIT_SCREEN) { showUSB0FloatingWindow(); } } Override public void onUSB1CurrentVideoScreenType(int type) { if (type TempMemoryUtil.VIDEO_FULL_SCREEN) { dismissUSB1FloatingWindow(); } else if (type TempMemoryUtil.VIDEO_SPLIT_SCREEN) { showUSB1FloatingWindow(); } } }; }4、退出视频播放界面使用分屏小窗时将view上的surface绑定到公共MediaPlayer上面使画面同步渲染到小窗口上。private void showUSB1FloatingWindow() { Log.d(TAG, showUSB1FloatingWindow: ); mUSB1VideoFloatingWindow new VideoFloatingWindow(); mUSB1VideoFloatingWindow.showFloatingWindowView(this, USBConstants.USBPath.USB1_PATH); }5、在view上处理各个按钮的功能逻辑View.OnClickListener mOnClickListener new View.OnClickListener() { SuppressLint(NonConstantResourceId) Override public void onClick(View v) { switch (v.getId()) { case R.id.iv_floating_window_close_btn: USBVideoControlTool.processCommand(MediaAction.STOP, ChangeReasonData.UI_FINISH); if (mCurrentUsb.equals(USBConstants.USBPath.USB0_PATH)) { TempMemoryUtil.getInstance().setUSB0CurrentVideoScreenType(TempMemoryUtil.VIDEO_FULL_SCREEN); } else if (mCurrentUsb.equals(USBConstants.USBPath.USB1_PATH)) { TempMemoryUtil.getInstance().setUSB1CurrentVideoScreenType(TempMemoryUtil.VIDEO_FULL_SCREEN); } break; case R.id.iv_floating_window_full_screen_btn: if (mCurrentUsb.equals(USBConstants.USBPath.USB0_PATH)) { initVideoPlayActivity(CurrentPlayInfo.getInstance(MediaType.USB1_VIDEO).getCurrentPlayItem().getPath()); } else if (mCurrentUsb.equals(USBConstants.USBPath.USB1_PATH)) { initVideoPlayActivity(CurrentPlayInfo.getInstance(MediaType.USB2_VIDEO).getCurrentPlayItem().getPath()); } dismiss(); break; case R.id.iv_floating_window_pre_btn: USBVideoControlTool.processCommand(MediaAction.PRE, ChangeReasonData.CLICK); break; case R.id.iv_floating_window_play_btn: USBVideoControlTool.processCommand(MediaAction.PLAY_OR_PAUSE, ChangeReasonData.CLICK); break; case R.id.iv_floating_window_next_btn: USBVideoControlTool.processCommand(MediaAction.NEXT, ChangeReasonData.CLICK); break; } } };6、更新界面消息按钮啥的可以使用WindowManager的updateViewLayout方式Override public void onPlayStatusChange(boolean isPlaying) { mIvPlayBtn.post(new Runnable() { Override public void run() { if (isPlaying) { mIvPlayBtn.setBackground(mContext.getDrawable(com.lwj.galleryresources.R.drawable.common_pause_icon)); } else { mIvPlayBtn.setBackground(mContext.getDrawable(com.lwj.galleryresources.R.drawable.common_playing_icon)); } mWindowManager.updateViewLayout(mViewFloatingWindow, mFloatParams); } }); }7、退出view返回播放界面时记得重新将播放界面的surface与公共MediaPlayer绑定一下。详细代码查看附件1.缺点1、小窗和播放界面不能同时在一个界面播放。为保持播放页面和小窗画面同步需要使用一个MediaPlayer我了解到一个MediaPlayer只能设置一个surface其他应用分屏小窗功能好像也没有小窗口和播放界面同时播放如果无特殊需求该缺点可以忽略。2、处理不好播放节目和小窗口的画面数据渲染容易出现黑屏、停止播放等问题。优点1、可行性非常好可以除了可以满足上述分屏小窗的功能对代码层面来说还可以自定义按钮弹窗样式弹出方式和位置就类似于给个悬浮窗口想怎么整怎么整。2、可控性非常好小窗口画面不同步黑屏切换回播放主界面出点什么问题都特别好排查bug很少自定义写得好基本可以没有bug。3、博客上一大推教程大家都推荐使用这种方式我试了一下b站貌似也是用的这种方式我把悬浮窗口的权限关闭了它就不能在其他应用进行分屏小窗功能了。个人推荐使用这种方法实现视频分屏小窗功能。方法三使用Presentation看网上有那种双屏异显的时候会用到但是不会用比较考验对屏幕和surface层级的研究。感兴趣的可以自己研究一下。U盘播放时长记忆简介需要实现一个记忆各个视频播放时长的功能kk以后我们应该都会使用安卓原生MediaStore功能来查询U盘数据这个时候可以使用MediaStore的video表中的bookmark字段。顾名思义这个标签的意思是书签用于记录视频播放时长。cursor.getInt(cursor.getColumnIndex(MediaStore.Video.Media.BOOKMARK))使用方式1、在视频播放界面退出去时修改MediaStore的这个标签通知数据改变。SuppressLint(NewApi) public void updateData(FileMessage fileMessage) { Log.d(TAG, updateData: fileMessage); MediaThreadPoolExecutorUtils.getInstance().submit(new Runnable() { Override public void run() { ContentValues contentValues new ContentValues(); contentValues.put(bookmark, fileMessage.getBookmark()); int update contentResolver.update(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, contentValues, _data ?, new String[]{fileMessage.getPath()}); //通知数据变化 notifyUpdate(fileMessage); } }); }2、查询已播放时长。Override protected ListFileMessage query(Cursor cursor) { ListFileMessage videoList new ArrayList(); if (cursor ! null) { while (cursor.moveToNext()) { FileMessage fm new FileMessage(); fm.setId(cursor.getInt(cursor.getColumnIndex(MediaStore.Video.Media._ID))); fm.setPath(cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA))); fm.setDuration(cursor.getInt(cursor.getColumnIndex(MediaStore.Video.Media.DURATION))); fm.setName(cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.TITLE))); fm.setFileName(cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DISPLAY_NAME))); fm.setLastModified(cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.DATE_MODIFIED))); fm.setSize(cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.SIZE))); fm.setBookmark(cursor.getInt(cursor.getColumnIndex(MediaStore.Video.Media.BOOKMARK))); fm.setMediaType(FileMessage.VIDEO_TYPE); videoList.add(fm); } cursor.close(); } return videoList; }优点1、与媒体数据一起放在MediaStore的表中查询修改记忆方便拔插U盘也可以记忆全部视频数据的各个视频的播放时长。2、与现有的数据存储查询方案更加契合。缺点1、这个书签只在video和music这张表中使用查询的时候记得不要去查图片的数据库。