配置清单文件设置权限
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.hlsk.touming01"> <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" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- 配置实时壁纸Service --> <service android:label="@string/app_name" android:name=".CameraLiveWallpaper" android:permission="android.permission.BIND_WALLPAPER"> <!-- 为实时壁纸配置intent-filter --> <intent-filter> <action android:name="android.service.wallpaper.WallpaperService" /> </intent-filter> <!-- 为实时壁纸配置meta-data --> <meta-data android:name="android.service.wallpaper" android:resource="@xml/livewallpaper" /> </service> </application> </manifest>
在res目录下创建xml文件夹,然后再创建livewallpaper.xml
<?xml version="1.0" encoding="utf-8"?> <!-- ic_launcher 预览是显示的图片--> <wallpaper xmlns:android="http://schemas.android.com/apk/res/android" android:thumbnail="@mipmap/ic_launcher"/>
创建CameraLiveWallpaper类
package com.example.hlsk.touming01; import android.hardware.Camera; import android.service.wallpaper.WallpaperService; import android.view.MotionEvent; import android.view.SurfaceHolder; import java.io.IOException; public class CameraLiveWallpaper extends WallpaperService { // 实现WallpaperService必须实现的抽象方法 public Engine onCreateEngine() { // 返回自定义的CameraEngine return new CameraEngine(); } class CameraEngine extends Engine implements Camera.PreviewCallback { private Camera camera; @Override public void onCreate(SurfaceHolder surfaceHolder) { super.onCreate(surfaceHolder); startPreview(); // 设置处理触摸事件 setTouchEventsEnabled(true); } @Override public void onTouchEvent(MotionEvent event) { super.onTouchEvent(event); // 时间处理:点击拍照,长按拍照 } @Override public void onDestroy() { super.onDestroy(); stopPreview(); } @Override public void onVisibilityChanged(boolean visible) { if (visible) { startPreview(); } else { stopPreview(); } } /** * 开始预览 */ public void startPreview() { camera = Camera.open(); camera.setDisplayOrientation(90); try { camera.setPreviewDisplay(getSurfaceHolder()); } catch (IOException e) { e.printStackTrace(); } camera.startPreview(); } /** * 停止预览 */ public void stopPreview() { if (camera != null) { try { camera.stopPreview(); camera.setPreviewCallback(null); // camera.lock(); camera.release(); } catch (Exception e) { e.printStackTrace(); } camera = null; } } @Override public void onPreviewFrame(byte[] bytes, Camera camera) { camera.addCallbackBuffer(bytes); } } }
main布局
<Button android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击设置" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent"/>