android 动态壁纸 2

1. rs/xml/mylivewallpaper.xml

<?xml version="1.0" encoding="utf-8"?>
<wallpaper xmlns:android="http://schemas.android.com/apk/res/android"
android:thumbnail="@drawable/aaa"
/>

 

2.AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lp.wallpaper">
<uses-sdk android:minSdkVersion="7" />
<uses-feature android:name="android.software.live_wallpaper" />

<application
android:icon="@drawable/icon"
android:label="@string/app_name">

<service
android:label="@string/app_name"
android:name=".MyWallpaper"
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/mylivewallpaper" />
</service>

</application>
</manifest>

 

 

3.MyWallpaper.java

 

package com.lp.wallpaper;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

public class MyWallpaper extends WallpaperService{
private Handler handler = new Handler();



@Override
public void onCreate() {
System.out.println("service onCreate");
super.onCreate();
}

@Override
public void onDestroy() {
System.out.println("service onDestory");
super.onDestroy();
}

@Override
public Engine onCreateEngine() {
System.out.println("service onCreateEngine");
return new MyEngine();
}

private class MyEngine extends Engine{

private float x = 200;
private float y = 300;
private Paint mPaint;
private boolean mVisible = false;

private Runnable drawThread = new Runnable() {
public void run() {
drawWallpaper();
}
};

public MyEngine(){
System.out.println("MyEngine");
mPaint = new Paint();
mPaint.setColor(Color.RED);
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(4);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setTextSize(24);
}

@Override
public Bundle onCommand(String action, int x, int y, int z, Bundle extras, boolean resultRequested) {
// TODO Auto-generated method stub
System.out.println("onCommand");
return super.onCommand(action, x, y, z, extras, resultRequested);
}

@Override
public void onCreate(SurfaceHolder surfaceHolder) {
System.out.println("onCreate");
super.onCreate(surfaceHolder);
//作用是使壁纸能响应touch event,默认是false
setTouchEventsEnabled(true);
}

@Override
public void onDestroy() {
System.out.println("onDestroy");
super.onDestroy();
handler.removeCallbacks(drawThread);
}

@Override
public void onOffsetsChanged(float xOffset, float yOffset, float xOffsetStep, float yOffsetStep, int xPixelOffset, int yPixelOffset) {
// TODO Auto-generated method stub
System.out.println("onoffsetsChanged");
super.onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep, xPixelOffset, yPixelOffset);
}

@Override
public void onVisibilityChanged(boolean visible) {
System.out.println("onVisibilityChanged");
mVisible = visible;
if (visible) {
//开始
handler.postDelayed(drawThread, 50);
} else {
handler.removeCallbacks(drawThread);
}
}

@Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
System.out.println("onSurfaceChanged");
// TODO Auto-generated method stub
super.onSurfaceChanged(holder, format, width, height);
}

@Override
public void onSurfaceCreated(SurfaceHolder holder) {
System.out.println("onSurfaceCreated");
super.onSurfaceCreated(holder);
}

@Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
System.out.println("onSurfaceDestoryed");
// TODO Auto-generated method stub
super.onSurfaceDestroyed(holder);
handler.removeCallbacks(drawThread);
}

@Override
public void onTouchEvent(MotionEvent event) {
System.out.println("onTouchEvent");
// TODO Auto-generated method stub
super.onTouchEvent(event);
}


private void drawWallpaper() {

SurfaceHolder holder = getSurfaceHolder();
Canvas canvas = holder.lockCanvas();
drawTime(canvas);
holder.unlockCanvasAndPost(canvas);

//循环执行
handler.postDelayed(drawThread, 50);
}

private void drawTime(Canvas canvas) {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String dataStr = sdf.format(date);
canvas.save();
canvas.drawColor(0xff00ff00);
canvas.drawText(dataStr, x, y, mPaint);
canvas.restore();
}
}

}

你可能感兴趣的:(android)