这几天对前段事件比较火的视频壁纸产生了兴趣,也已经有了很多的实现,这里自己做下记录,也尝试着实现了一下相机壁纸,好玩是好玩就是有点费电。
为了兼容6.0之后的权限请求,可以看下之前写的Android6.0之后权限请求工具类
视频壁纸
先上效果图,gif的录制确实是个大坑
实现方法也挺简单的,都是继承了WallpaperService,只需要实现里面的一个onCreateEngine()方法即可。
实现一个继承了Engine的视频壁纸引擎,在surface创建后把MediaPlayer初始化好,之后只需要控制视频的播放和显示就好了。
-
/**
-
* 视频壁纸的引擎
-
*/
-
class VideoEngine extends Engine{
-
MediaPlayer mediaPlayer;
-
-
-
public void onSurfaceCreated(SurfaceHolder holder) {
-
super.onSurfaceCreated(holder);
-
initMediaPlayer(holder);
-
}
-
-
/**
-
* 初始化MediaPlayer
-
* @param surfaceHolder
-
*/
-
private void initMediaPlayer(SurfaceHolder surfaceHolder) {
-
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.video);
-
mediaPlayer.setSurface(surfaceHolder.getSurface());
-
mediaPlayer.setLooping( true);
-
}
-
-
-
public void onVisibilityChanged(boolean visible) {
-
super.onVisibilityChanged(visible);
-
if(visible){
-
//可见时播放
-
mediaPlayer.start();
-
} else{
-
//不可见时暂停
-
mediaPlayer.pause();
-
}
-
}
-
-
-
public void onDestroy() {
-
super.onDestroy();
-
//停止释放MediaPlayer
-
if(mediaPlayer.isPlaying()){
-
mediaPlayer.stop();
-
}
-
mediaPlayer.release();
-
mediaPlayer = null;
-
}
-
}
-
<service
-
android:name= ".service.VideoWallpaperService"
-
android:label= "@string/video_wallpaper"
-
android:permission= "android.permission.BIND_WALLPAPER">
-
<intent-filter>
-
<action android:name="android.service.wallpaper.WallpaperService" />
-
</intent-filter>
-
-
<meta-data
-
android:name= "android.service.wallpaper"
-
android:resource= "@xml/video_wallpaper" />
-
</service>
-
"1.0" encoding="utf-8" xml version=
-
<!--选择壁纸时显示的缩略图-->
-
<wallpaper xmlns:android="http://schemas.android.com/apk/res/android"
-
android:thumbnail= "@mipmap/ic_launcher"
-
android:description= "@string/video_description"
-
/>
<uses-permission android:name="android.permission.SET_WALLPAPER" />
相机桌面
同上也放一个很渣的gif图
实现和上面的视频差不多,同样是用了SurfaceHolder显示,只是用了Camera。
有意思的是我们可以接受处理桌面上的触摸事件,比如在接受到接受到点击事件的时候拍照保存之类的,还是蛮有意思的。
-
/**
-
* 相机壁纸的引擎
-
*/
-
class CameraEngine extends Engine {
-
Camera camera;
-
-
-
public void onCreate(SurfaceHolder surfaceHolder) {
-
super.onCreate(surfaceHolder);
-
//接受触摸事件
-
setTouchEventsEnabled( true);
-
}
-
-
-
public void onTouchEvent(MotionEvent event) {
-
super.onTouchEvent(event);
-
//可以在这里处理添加触摸事件,如点击拍照
-
}
-
-
-
public void onVisibilityChanged(boolean visible) {
-
super.onVisibilityChanged(visible);
-
if(visible){
-
startPreview();
-
} else{
-
stopPreview();
-
}
-
}
-
-
-
public void onDestroy() {
-
stopPreview();
-
super.onDestroy();
-
}
-
-
/**
-
* 开始预览
-
*/
-
private void startPreview() {
-
camera = Camera.open();
-
//需要旋转90度
-
camera.setDisplayOrientation( 90);
-
try {
-
camera.setPreviewDisplay(getSurfaceHolder());
-
camera.startPreview();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
-
/**
-
* 关闭预览
-
*/
-
private void stopPreview() {
-
if (camera != null) {
-
camera.stopPreview();
-
camera.release();
-
camera = null;
-
}
-
}
-
}
-
<service
-
android:name= ".service.CameraWallpaperService"
-
android:label= "@string/camera_wallpaper"
-
android:permission= "android.permission.BIND_WALLPAPER">
-
<intent-filter>
-
<action android:name="android.service.wallpaper.WallpaperService" />
-
</intent-filter>
-
-
<meta-data
-
android:name= "android.service.wallpaper"
-
android:resource= "@xml/camera_wallpaper" />
-
</service>
-
"1.0" encoding="utf-8" xml version=
-
<!--选择壁纸时显示的缩略图-->
-
<wallpaper xmlns:android="http://schemas.android.com/apk/res/android"
-
android:thumbnail= "@mipmap/ic_launcher"
-
android:description= "@string/camera_description"
-
/>
权限
因为需要使用相机,所以需要获取提供相机权限
-
<uses-permission android:name="android.permission.CAMERA" />
-
<uses-permission android:name="android.permission.SET_WALLPAPER" />
-
-
<uses-feature android:name="android.hardware.camera" />
-
<uses-feature android:name="android.hardware.camera.autofocus" />
调用
直接使用指定壁纸,传入Service组件
-
Intent intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
-
intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, new ComponentName(context, VideoWallpaperService.class));
-
startActivity(intent);