【安卓笔记】Live Wallpaper 一

 

一开始我就想弄个几只小猫往下掉(或者飘?)的效果,小猫都有了,愣是没弄出来(我对不住你们哭,喵众猫:犹豫

 

于是现在变成了系统时间(还是没有格式化的那种吐舌头)在不停跑啊跑

 

要制作动态壁纸

1.需要继承WallpaperService,这个Service里主要用Engine来实现动态壁纸

2.这个Engine,看到网上说生命周期是onCreate->onSurfaceCreated->onSurfaceDestroyed->onDestroyed,好嘛,我暂时不知道他对不对,不过我就没写onVisibilityChanged,然后出错了(还是很诡异的错)

3.自己要做长时间的刷新某个东东的事,写到一个线程中,用Handler去控制

 

首先建MyWallpaperService 这个类

public class MyWallpaperService extends WallpaperService {

	@Override
	public Engine onCreateEngine() {
		return new MyEngine();
	}
	/*
	 * surface,就叫他一个表层吧,要用一个surfaceholder来操作这个表层,这个holder可以绑定一个画布,
	 * 这么说,就把这个holder比作双面胶,把画布给粘到表层上,
	 * 至于在画布上作画的事,就要用笔(paint)来了
	 * */

	class MyEngine extends Engine {
		private Handler h = new Handler();
		private Paint p;
		private Canvas c;

		// 用一个线程来处理绘制动态壁纸的过程
		Runnable dThread = new Runnable() {
			@Override
			public void run() {
				drawWallpaper();
			}
		};

		private void drawWallpaper() {
			SurfaceHolder holder = getSurfaceHolder();
			c = holder.lockCanvas();
			c.drawColor(Color.WHITE);
			drawTime(c);
			holder.unlockCanvasAndPost(c);

			h.postDelayed(dThread, 100);
		}

		private void drawTime(Canvas c) {
			long l = System.currentTimeMillis();
			c.drawText(l + "", 200, 300, p);
			/*
			 * Saves the current matrix and clip onto a private stack.
			 * */
			// c.save();
			/*
			 * This call balances a previous call to save(), and is used to
			 * remove all modifications to the matrix/clip state since the last
			 * save call.
			 */
			// c.restore();
			/*
			 * 所以,这一段对matrix/clip的保存跟移除是个什么意义呢?暂不明,貌似不影响效果
			 * */
			// Log.d("绘制时间", "draw time");
		}

		public MyEngine() {
			p = new Paint();
			p.setColor(Color.RED);
			p.setAntiAlias(true);
			p.setStyle(Style.STROKE);
			p.setStrokeWidth(3);
			p.setTextSize(30);
		}

		@Override
		public void onCreate(SurfaceHolder surfaceHolder) {
			Log.i("MyEngine----------", "engine created");
			super.onCreate(surfaceHolder);
//			setTouchEventsEnabled(true);//在这个程序中没用到
		}

		/**
		 * 调用SurfaceHolder.Callback中的回调函数surfaceCreated
		 * */
		@Override
		public void onSurfaceCreated(SurfaceHolder holder) {
			Log.i("MyEngine----------", "surface created");
			super.onSurfaceCreated(holder);
		}

		/**
		 * 这个方法是必须实现的,要不然会一直显示"正在载入动态壁纸..." Called to inform you of the
		 * wallpaper becoming visible or hidden. It is very important that a
		 * wallpaper only use CPU while it is visible..
		 * 虽然还不太懂为什么,但人家说了,很重要,只有可见的时候才能占用CPU,为了性能也必须实现
		 * */
		@Override
		public void onVisibilityChanged(boolean visible) {
			super.onVisibilityChanged(visible);
			if (visible) {
				h.postDelayed(dThread, 100);
			} else {
				h.removeCallbacks(dThread);
			}
		}

		@Override
		public void onSurfaceDestroyed(SurfaceHolder holder) {
			Log.i("MyEngine----------", "surface destoryed");
			super.onSurfaceDestroyed(holder);
			h.removeCallbacks(dThread);
		}

		@Override
		public void onDestroy() {
			Log.i("MyEngine----------", "engine destoryed");
			super.onDestroy();
			h.removeCallbacks(dThread);
		}

	}

}

 

在AndroidManifest.xml中去配置这个service,intent-filter中action的name跟meta-data中的name都是API中推荐(还是叫规定?)的

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.saya.mylivewallpaper"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
    <uses-feature android:name="android.software.live_wallpaper" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <service
            android:name=".MyWallpaperService"
            android:enabled="true"
            android:icon="@drawable/p_01"
            android:label="天上掉下一只猫"
            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/wallpaper" />
        </service>
    </application>
</manifest>

 

 

在res下新建xml文件夹,在文件夹中建wallpaper.xml,即上面meta-data中resource中用到的,就是对你自己的这个动态壁纸的一些设置吧

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

 

 

这个thumbnail的作用,就是出现下面这只小猫

【安卓笔记】Live Wallpaper 一

 

嗯,这样就算完了

 

【安卓笔记】Live Wallpaper 一

 

 上面提到的错误,logcat没有报错,每次程序运行就停留在这个界面,坑爹

【安卓笔记】Live Wallpaper 一

 

 

 

传送门:

牛逼轰轰的一个:http://mzh3344258.blog.51cto.com/1823534/806560

生命周期出自:http://www.ophonesdn.com/article/show/278

另外:http://www.cnblogs.com/carmanloneliness/archive/2012/03/10/2388500.html

http://www.oschina.net/question/157182_37775

 

 

 

你可能感兴趣的:(live)