实现步骤:
manifest中:
<!-- 配置 实时壁纸 service --> <service android:label="@string/app_name" android:name=".service.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>
<activity android:name=".wallpaper.MainActivity" />
2.在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" />
3.新建实现动态壁纸的LiveWallpaper.java:
package com.dyf.dyfkotlindemo.service; import android.hardware.Camera; import android.service.wallpaper.WallpaperService; import android.view.MotionEvent; import android.view.SurfaceHolder; import java.io.IOException; /** * Created by dyf on 2017/7/26. */ public class CameraLiveWallpaper extends WallpaperService { //实现wallpaperService 必须实现的抽象方法 @Override 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(); } //这个一定要加, 不然不行 黑屏 小米4c 亲测有效 @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){ camera.stopPreview(); camera.setPreviewCallback(null); // camera.lock(); camera.release(); // camera = null; } } @Override public void onPreviewFrame(byte[] bytes, Camera camera) { camera.addCallbackBuffer(bytes); } } }
4.在MainActivity中添加如下代码:
package com.dyf.dyfkotlindemo.wallpaper; import android.Manifest; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Toast; import com.dyf.dyfkotlindemo.R; /** * Created by dyf on 2017/7/26. */ public class MainActivity extends AppCompatActivity { private static final int PERMISSIONS_REQUEST_CAMERA = 454; private Context mContext; static final String PERMISSION_CAMERA = Manifest.permission.CAMERA; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_wallpaper); mContext = this; findViewById(R.id.text).setOnClickListener(v -> { checkSelfPermission(); }); } void checkSelfPermission(){ if (ContextCompat.checkSelfPermission(mContext, PERMISSION_CAMERA) != PackageManager.PERMISSION_GRANTED){ ActivityCompat.requestPermissions(this, new String[]{PERMISSION_CAMERA}, PERMISSIONS_REQUEST_CAMERA); }else { startWallpaper(); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode){ case PERMISSIONS_REQUEST_CAMERA: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ startWallpaper(); }else{ Toast.makeText(mContext, getString(R.string._lease_open_permissions), Toast.LENGTH_SHORT).show(); } return; } } } void startWallpaper(){ final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER); Intent chooser = Intent.createChooser(pickWallpaper, getString(R.string.choose_wallpaper)); startActivity(chooser); } }
附:string.xml
<resources> <string name="app_name">dyfkotlinDemo</string> <string name="content"> 史上最浅显易懂的Git教程!\n\r 为什么要编写这个教程?因为我在学习Git的过程中,买过书,也在网上Google了一堆Git相关的文章和教程,但令人失望的是,这些教程不是难得令人发指,就是简单得一笔带过,或者,只支离破碎地介绍Git的某几个命令,还有直接从Git手册粘贴帮助文档的,总之,初学者很难找到一个由浅入深,学完后能立刻上手的Git教程。\n\r 既然号称史上最浅显易懂的Git教程,那这个教程有什么让你怦然心动的特点呢?\n\r 首先,本教程绝对面向初学者,没有接触过版本控制概念的读者也可以轻松入门,不必担心起步难度;\n\r 其次,本教程实用性超强,边学边练,一点也不觉得枯燥。而且,你所学的Git命令是“充分且必要”的,掌握了这些东西,你就可以通过Git轻松地完成你的工作。\n\r </string>\n\r <string name="title">史上最浅显易懂的Git教程!</string> <string name="data"> {\"code\":200,\"message\":\"数据获取成功\",\"result\":[{\"order_id\":\"64\",\"price\":\"0.01\",\"order_sn\":\"201707111809105964a3c6c9239\",\"time\":\"2017-07-11 18:09:10\",\"goods_amount\":\"0.01\",\"fee\":\"0.00\",\"num\":\"1\",\"status\":\"0\",\"pay_status\":\"1\",\"goodslist\":[{\"gid\":\"18\",\"gname\":\"Air Jordan 12 Low AJ12 狼灰 麂皮 308317 308305-002\",\"attr_name\":\"黄色|29\",\"num\":\"1\",\"price\":\"0.01\",\"total\":\"0.01\"}]},{\"order_id\":\"63\",\"price\":\"0.01\",\"order_sn\":\"201707111806545964a33e652c0\",\"time\":\"2017-07-11 18:06:54\",\"goods_amount\":\"0.01\",\"fee\":\"0.00\",\"num\":\"1\",\"status\":\"0\",\"pay_status\":\"0\",\"goodslist\":[{\"gid\":\"18\",\"gname\":\"Air Jordan 12 Low AJ12 狼灰 麂皮 308317 308305-002\",\"attr_name\":\"黄色|29\",\"num\":\"1\",\"price\":\"0.01\",\"total\":\"0.01\"},{\"gid\":\"18\",\"gname\":\"Air Jordan 12 Low AJ12 狼灰 麂皮 308317 308305-002\",\"attr_name\":\"黄色|29\",\"num\":\"1\",\"price\":\"0.01\",\"total\":\"0.01\"}]},{\"order_id\":\"62\",\"price\":\"0.01\",\"order_sn\":\"201707111804125964a29c26f93\",\"time\":\"2017-07-11 18:04:12\",\"goods_amount\":\"0.01\",\"fee\":\"0.00\",\"num\":\"1\",\"status\":\"0\",\"pay_status\":\"1\",\"goodslist\":[{\"gid\":\"18\",\"gname\":\"Air Jordan 12 Low AJ12 狼灰 麂皮 308317 308305-002\",\"attr_name\":\"黄色|29\",\"num\":\"1\",\"price\":\"0.01\",\"total\":\"0.01\"},{\"gid\":\"18\",\"gname\":\"Air Jordan 12 Low AJ12 狼灰 麂皮 308317 308305-002\",\"attr_name\":\"黄色|29\",\"num\":\"1\",\"price\":\"0.01\",\"total\":\"0.01\"},{\"gid\":\"18\",\"gname\":\"Air Jordan 12 Low AJ12 狼灰 麂皮 308317 308305-002\",\"attr_name\":\"黄色|29\",\"num\":\"1\",\"price\":\"0.01\",\"total\":\"0.01\"}]},{\"order_id\":\"61\",\"price\":\"0.01\",\"order_sn\":\"201707111802155964a22749b60\",\"time\":\"2017-07-11 18:02:15\",\"goods_amount\":\"0.01\",\"fee\":\"0.00\",\"num\":\"1\",\"status\":\"0\",\"pay_status\":\"1\",\"goodslist\":[{\"gid\":\"18\",\"gname\":\"Air Jordan 12 Low AJ12 狼灰 麂皮 308317 308305-002\",\"attr_name\":\"黄色|29\",\"num\":\"1\",\"price\":\"0.01\",\"total\":\"0.01\"},{\"gid\":\"18\",\"gname\":\"Air Jordan 12 Low AJ12 狼灰 麂皮 308317 308305-002\",\"attr_name\":\"黄色|29\",\"num\":\"1\",\"price\":\"0.01\",\"total\":\"0.01\"}]},{\"order_id\":\"60\",\"price\":\"0.01\",\"order_sn\":\"2017071117402559649d09f1895\",\"time\":\"2017-07-11 17:40:25\",\"goods_amount\":\"0.01\",\"fee\":\"0.00\",\"num\":\"1\",\"status\":\"0\",\"pay_status\":\"1\",\"goodslist\":[{\"gid\":\"18\",\"gname\":\"Air Jordan 12 Low AJ12 狼灰 麂皮 308317 308305-002\",\"attr_name\":\"黄色|29\",\"num\":\"1\",\"price\":\"0.01\",\"total\":\"0.01\"}]},{\"order_id\":\"59\",\"price\":\"0.01\",\"order_sn\":\"2017071117350259649bc63a59e\",\"time\":\"2017-07-11 17:35:02\",\"goods_amount\":\"0.01\",\"fee\":\"0.00\",\"num\":\"1\",\"status\":\"0\",\"pay_status\":\"0\",\"goodslist\":[{\"gid\":\"18\",\"gname\":\"Air Jordan 12 Low AJ12 狼灰 麂皮 308317 308305-002\",\"attr_name\":\"黄色|29\",\"num\":\"1\",\"price\":\"0.01\",\"total\":\"0.01\"}]}]} </string> <string name="database">data test</string> <string name="_lease_open_permissions">请打开权限</string> <string name="choose_wallpaper">关闭动态壁纸</string> </resources>
原作者github地址:https://github.com/songixan/Wallpaper
我的github地址:https://github.com/dyf505776897/dyfkotlindemo 欢迎star 共同进步学习探讨
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦