本文主要是介绍开发实时壁纸,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
随时随地技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)
所谓实时壁纸,即动态壁纸,就是手机桌面不再是简单的图片,而是运行中的动画,该动画由程序实时绘制的,所以称为实时壁纸。Android中提供WallpaperService基类来开发实时壁纸,步骤如下:
下面用一个示例来演示,该示例原型来源于疯狂讲义一书,代码如下:
WallpaperService的子类:LiveWallpaper
package com.home.service;import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.view.MotionEvent;
import android.view.SurfaceHolder;public class LiveWallpaper extends WallpaperService {// 继承WallpaperService必须实现的抽象方法@Overridepublic Engine onCreateEngine() {// 返回自定义的Enginereturn new MyEngine();}class MyEngine extends Engine {private int ScreenWidth;private int ScreenHeight;// 记录程序界面是否可见private boolean mVisible;// 记录当前用户事件的发生位置private float mTouchX = -1;private float mTouchY = -1;// 记录当前圆圈的绘制位置private float cx = 15;private float cy = 15;// 定义画笔private Paint mPaint = new Paint();// 定义一个HandlerHandler mHandler = new Handler();// 定义一个周期性执行的任务private final Runnable drawTarget = new Runnable() {@Overridepublic void run() {drawFrame();}};public void onCreate(android.view.SurfaceHolder surfaceHolder) {super.onCreate(surfaceHolder);// 获得屏幕宽和高ScreenWidth = getDesiredMinimumWidth() / 2;ScreenHeight = getDesiredMinimumHeight();// 初始化画笔mPaint.setColor(0xffffffff);mPaint.setAntiAlias(true);mPaint.setStrokeWidth(2);mPaint.setStrokeCap(Paint.Cap.ROUND);mPaint.setStyle(Paint.Style.STROKE);// 设置处理触摸事件setTouchEventsEnabled(true);}@Overridepublic void onDestroy() {super.onDestroy();// 删除回调mHandler.removeCallbacks(drawTarget);}@Overridepublic void onVisibilityChanged(boolean visible) {mVisible = visible;// 当界面可见时,执行drawFrame()方法if (mVisible) {// 动态绘制图形drawFrame();} else {// 如果界面不可见,删除回调mHandler.removeCallbacks(drawTarget);}}@Overridepublic void onOffsetsChanged(float xOffset, float yOffset,float xOffsetStep, float yOffsetStep, int xPixelOffset,int yPixelOffset) {drawFrame();}// 当屏幕大小改变时调用该方法@Overridepublic void onSurfaceChanged(SurfaceHolder holder, int format,int width, int height) {super.onSurfaceChanged(holder, format, width, height);drawFrame();}@Overridepublic void onSurfaceDestroyed(SurfaceHolder holder) {super.onSurfaceDestroyed(holder);mVisible = false;mHandler.removeCallbacks(drawTarget);}@Overridepublic void onTouchEvent(MotionEvent event) {// 如果检测到滑动操作if (event.getAction() == MotionEvent.ACTION_MOVE) {mTouchX = event.getX();mTouchY = event.getY();} else {mTouchX = -1;mTouchY = -1;}super.onTouchEvent(event);}/*** 定义绘制图形的工具方法*/private void drawFrame() {// 获取该壁纸的SurfaceHolder对象final SurfaceHolder holder = getSurfaceHolder();Canvas c = null;try {// 对画布加锁c = holder.lockCanvas();if (c != null) {c.save();// 绘制背景色c.drawColor(0xff000000);// 在触碰点绘制圆圈drawTouchPoint(c);// 绘制圆圈c.drawCircle(cx, cy, 80, mPaint);c.restore();}} finally {if (c != null) {holder.unlockCanvasAndPost(c);}}mHandler.removeCallbacks(drawTarget);// 调度下一次重绘if (mVisible) {cx += 15;cy += 20;// 如果cx、cy移出屏幕,从左上角重新开始if (cx >= ScreenWidth - 80) {cx = 15;}if (cy >= ScreenHeight - 80) {cy = 20;}// 指定0.1秒后重新执行一次drawTargetmHandler.postDelayed(drawTarget, 100);}}/*** 在屏幕触碰点绘制圆圈* * @param canvas*/private void drawTouchPoint(Canvas canvas) {if (mTouchX >= 0 && mTouchY >= 0) {canvas.drawCircle(mTouchX, mTouchY, 40, mPaint);}}}
}
配置LiveWallpaper:
<serviceandroid:name="com.home.service.LiveWallpaper"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/mywallpaperxml"/></service>
mywallpaperxml:
<?xml version="1.0" encoding="utf-8"?>
<wallpaper xmlns:android="http://schemas.android.com/apk/res/android" android:description="@string/wallpaper_des"android:thumbnail="@drawable/ic_launcher">
</wallpaper>
权限:
<!-- 加入设置壁纸权限 --><uses-permission android:name="android.permission.SET_WALLPAPER" />
这篇关于开发实时壁纸的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!