首先自己写了个接口,这个可以不要
public interface LiveWallpaper extends Runnable {
public void onCreate(SurfaceHolder surfaceHolder, Context context);
public void onResume();
public void onPause();
public void onStop();
public void onSizeChanged(int width, int height);
public void onTouchEvent(MotionEvent event);
}
然后写个简单的壁纸绘制类
public class SimpleLiveWallpaper implements LiveWallpaper {
private SurfaceHolder surfaceHolder;
private Context context;
/** State */
private boolean isSleep;
private boolean isRunning;
/** Dimensions */
private int width;
private int height;
/** Time tracking */
private long lastTime;
public int DEFUALT_UPDATE_FPS = 1000;
private int updateFPS = DEFUALT_UPDATE_FPS;
private SharedPreferences spf;
private Bitmap bg;
@Override
public void onCreate(SurfaceHolder surfaceHolder, Context context) {
// TODO Auto-generated method stub
this.surfaceHolder = surfaceHolder;
this.context = context;
this.isSleep = true;
spf = context
.getSharedPreferences(
WallPaperSettingActivity.WALL_PAPER_PREFS,
Context.MODE_PRIVATE);
paint = new Paint();
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setTextSize(20);
bg = KDisplayUtil.drawable2Bitmap(WallpaperManager.getInstance(context)
.getDrawable());
matrix = new Matrix();
}
public void onResume() {
updateFPS = spf.getInt(WallPaperSettingActivity.UPDATE_FPS,
DEFUALT_UPDATE_FPS);
this.isSleep = false;
synchronized (this) {
this.notify();
}
}
public void onPause() {
this.isSleep = true;
synchronized (this) {
this.notify();
}
}
public void onStop() {
this.isRunning = false;
synchronized (this) {
this.notify();
}
}
public void onSurfaceSize(int width, int height) {
this.width = width;
this.height = height;
synchronized (this) {
this.notify();
}
}
public void onTouchEvent(MotionEvent event) {
}
private int x;
private int y;
private Paint paint;
String msessge = "Hello WallPaper";
protected void updateLogic() {
x = (int) (Math.random() * this.width);
y = (int) (Math.random() * this.height);
if (x > y) {
paint.setColor(Color.RED);
} else {
paint.setColor(Color.BLUE);
}
matrix.reset();
matrix.setRotate(30, x, y);
}
private Matrix matrix;
protected void updateDraw(Canvas canvas) {
paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
canvas.drawPaint(paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC));
canvas.save();
canvas.setMatrix(matrix);
canvas.drawBitmap(bg, 0, 0, paint);
canvas.restore();
canvas.drawText(msessge, x, y, paint);
}
@Override
public void run() {
// TODO Auto-generated method stub
this.isRunning = true;
Canvas canvas = null;
while (this.isRunning) {
if (System.currentTimeMillis() - lastTime > updateFPS) {
LogUtils.e("isDraw");
try {
canvas = this.surfaceHolder.lockCanvas(null);
synchronized (this.surfaceHolder) {
updateLogic();
updateDraw(canvas);
lastTime = System.currentTimeMillis();
}
} finally {
if (canvas != null) {
this.surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
synchronized (this) {
if (this.isSleep) {
try {
wait();
} catch (Exception e) {
}
}
}
}
}
@Override
public void onSizeChanged(int width, int height) {
// TODO Auto-generated method stub
this.width = width;
this.height = height;
}
}
最主要的就是这个壁纸服务
public class KLiveWallpaperService extends WallpaperService {
@Override
public Engine onCreateEngine() {
// TODO Auto-generated method stub
return new KEngine(new SimpleLiveWallpaper());
}
public class KEngine extends Engine {
private LiveWallpaper liveWallpaper;
public KEngine(LiveWallpaper liveWallpaper) {
super();
this.liveWallpaper = liveWallpaper;
}
@Override
public void onCreate(SurfaceHolder surfaceHolder) {
// TODO Auto-generated method stub
super.onCreate(surfaceHolder);
if (liveWallpaper != null) {
liveWallpaper.onCreate(surfaceHolder, getApplicationContext());
setTouchEventsEnabled(true);
}
}
@Override
public void onVisibilityChanged(boolean visible) {
// TODO Auto-generated method stub
super.onVisibilityChanged(visible);
if (liveWallpaper != null) {
if (visible) {
liveWallpaper.onResume();
} else {
liveWallpaper.onPause();
}
}
}
@Override
public void onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
super.onTouchEvent(event);
if (liveWallpaper != null) {
liveWallpaper.onTouchEvent(event);
}
}
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
// TODO Auto-generated method stub
super.onSurfaceChanged(holder, format, width, height);
if (liveWallpaper != null) {
liveWallpaper.onSizeChanged(width, height);
}
}
@Override
public void onSurfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
super.onSurfaceCreated(holder);
if (liveWallpaper != null) {
new Thread(liveWallpaper).start();
}
}
@Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
super.onSurfaceDestroyed(holder);
if (liveWallpaper != null) {
liveWallpaper.onStop();
}
}
@Override
public void onOffsetsChanged(float xOffset, float yOffset,
float xOffsetStep, float yOffsetStep, int xPixelOffset,
int yPixelOffset) {
// TODO Auto-generated method stub
super.onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep,
xPixelOffset, yPixelOffset);
}
}
}
然后需要做如下配置,出了name基本是固定的。
<service
android:name="org.k.demo.wallpaper.KLiveWallpaperService"
android:enabled="true"
android:icon="@drawable/icon_head"
android:label="@string/app_name"
android:permission="android.permission.BIND_WALLPAPER" >
<intent-filter android:priority="1" >
<action android:name="android.service.wallpaper.WallpaperService" />
</intent-filter>
<meta-data
android:name="android.service.wallpaper"
android:resource="@xml/live_wall_paper" />
</service>
<!--如果需要做设置选项 -->
<activity
android:name="org.k.demo.wallpaper.WallPaperSettingActivity"
android:exported="true"
android:label="@string/label_wall_paper_setting" >
</activity>
资源res/xml:live_wall_paper.xml
<?xml version="1.0" encoding="utf-8"?>
<wallpaper xmlns:android="http://schemas.android.com/apk/res/android"
android:author="@string/author"
android:description="@string/wallpaer_description"
android:settingsActivity="org.k.demo.wallpaper.WallPaperSettingActivity" 、、
android:thumbnail="@drawable/ic_launcher" />
android:settingsActivity 为对应的设置界面,配置时注意需要 android:exported=”true”,否则无法显示界面。
然后然后就没有了……
