动态壁纸是一个Android应用程序,包括一个服务(WallpaperService)。该服务必须包括一个引擎(WallpaperService.Engine)。该引擎是连接用户、桌面、系统之间的桥梁。它也可以绘制桌面壁纸。
首先,必须由内在的Engine类创建一个WallpaperService类。该服务必须在l中声明为"android.service.wallpaper.WallpaperService",这样它才会作为动态壁纸被手机识别。而且还要在服务配置中附加"android.permission.BIND_WALLPAPER"的权限许可:
[size=+0]
1. <service
2. android:name="LiveWallpaperService"
3. android:enabled="true"
4. android:icon="@drawable/icon"
5. android:label="@string/app_name"
6. android:permission="android.permission.BIND_WALLPAPER">
7. <intent-filter android:priority="1" >
8. <action android:name="android.service.wallpaper.WallpaperService" />
9. </intent-filter>
10. <meta-data
11. android:name="android.service.wallpaper"
报税流程12. android:resource="@xml/wallpaper" />
13. </service>
复制代码
创建一个XML文件,放置在应用程序目录下的/res/xml/中。它用来描述你的动态壁纸。
1. <?xml version="1.0" encoding="UTF-8"?>
2. 抛光打蜡<wallpaper
3. xmlns:android="schemas.android/apk/res/android"
4. 何润东个人资料简介android:thumbnail="@drawable/thumbnail"
5. android:description="@string/description"
6. android:settingsActivity="PreferenceActivity"/>
复制代码
再创建一个xml的属性文件,代码如下:
[size=+0]
1. <declare-styleable name="Wallpaper">
2. <!-- Component name of an activity that allows the user to modify
3. the current settings for this wallpaper. -->
4. <attr name="settingsActivity" />
5.
6.
7. <attr name="thumbnail" format="reference" />
8.
9.
10. <attr name="author" format="reference" />
11.
12.
13. <attr name="description" />
14. </declare-styleable>
复制代码
动态壁纸的服务代码如下:
[size=+0]
1. package net.androgames.blog.sample.livewallpaper;
2.
3. t.SharedPreferences;
4. import android.service.wallpaper.WallpaperService;
5. import android.view.MotionEvent;
6. import android.view.SurfaceHolder;
7.
8. /**
9. * Android Live Wallpaper Archetype
10. * <a href="my.oschina/arthor" target="_blank" rel="nofollow">@author</a> antoine vianey
11. * under GPL v3 : /licenses/gpl-3.0.html
12. */
13. public class LiveWallpaperService extends WallpaperService {
14.
15. @Override
16. public Engine onCreateEngine() {
17. return new SampleEngine();
18. }
19.
20. @Override
21. 娜扎视频男主曝光public void onCreate() {
22. Create();
23. }
24.
25. @Override
26. public void onDestroy() {
27. Destroy();
28. }
29.
30. public class SampleEngine extends Engine {
31.
32. private LiveWallpaperPainting painting;
33.
34. SampleEngine() {
35. SurfaceHolder holder = getSurfaceHolder();
36. painting = new LiveWallpaperPainting(holder,
37. getApplicationContext());
38. }
39.
40. @Override
41. public void onCreate(SurfaceHolder surfaceHolder) {
42. Create(surfaceHolder);
43. // register listeners and callbacks here
44. setTouchEventsEnabled(true);陈小纭多大
45. }
46.
47. @Override
48. public void onDestroy() {
49. Destroy();
50. // remove listeners and callbacks here
51. painting.stopPainting();
52. }
53.
54. @Override
55. public void onVisibilityChanged(boolean visible) {
56. if (visible) {
57. // register listeners and callbacks here
58. sumePainting();
59. } else {
60. // remove listeners and callbacks here
61. painting.pausePainting();
62. }
63. }
64.
65. @Override
66. public void onSurfaceChanged(SurfaceHolder holder, int format,
67. int width, int height) {
68. SurfaceChanged(holder, format, width, height);
69. painting.setSurfaceSize(width, height);
70. }
71.
72. @Override
73. public void onSurfaceCreated(SurfaceHolder holder) {
74. SurfaceCreated(holder);
75. // start painting
76. painting.start();
77. }
78.
79. @Override
80. public void onSurfaceDestroyed(SurfaceHolder holder) {
81. SurfaceDestroyed(holder);
82. boolean retry = true;
83. painting.stopPainting();
84. while (retry) {
85. try {
86. painting.join();
87. retry = false;
88. } catch (InterruptedException e) {}
89. }
90. }
91.
92. @Override
93. public void onOffsetsChanged(float xOffset, float yOffset,
94. float xStep, float yStep, int xPixels, int yPixels) {
95. }
96.
97. @Override
98. public void onTouchEvent(MotionEvent event) {
99. TouchEvent(event);
100. painting.doTouchEvent(event);
101. }
102.
103. }
104.
105. }
复制代码
当壁纸的显示、状态或大小变化是,会调用Engine的onCreate, onDestroy,onVisibilityChanged, onSurfaceChanged, onSurfaceCreated 和onSurfaceDestroyed方法。有了这些方法,动态壁纸才能展现出动画效果。而通过设置setTouchEventsEnabled(true),烟绯并且调用onTouchEvent(MotionEvent event)方法,来激活触摸事件。
我们在绘画墙纸的时候,也会使用一个单独的绘画线程:
1. package net.androgames.blog.sample.livewallpaper;
2.
3. t.Context;
4. aphics.Canvas;
发布评论