Android动态壁纸的制作教程.docx_第1页
Android动态壁纸的制作教程.docx_第2页
Android动态壁纸的制作教程.docx_第3页
Android动态壁纸的制作教程.docx_第4页
Android动态壁纸的制作教程.docx_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

动态壁纸是在Android 2.1新增的一个功能。动态壁纸可以添加到Android的桌面,具有交互式的动画背景效果。在本教程中,我们将教会你如何去制作一个交互式的动态壁纸。动态壁纸是一个Android应用程序,包括一个服务(WallpaperService)。该服务必须包括一个引擎(WallpaperService.Engine)。该引擎是连接用户、桌面、系统之间的桥梁。它也可以绘制桌面壁纸。首先,必须由内在的Engine类创建一个WallpaperService类。该服务必须在AndroidManifest.xml中声明为android.service.wallpaper.WallpaperService,这样它才会作为动态壁纸被手机识别。而且还要在服务配置中附加android.permission.BIND_WALLPAPER的权限许可:?12345678910111213创建一个XML文件,放置在应用程序目录下的/res/xml/中。它用来描述你的动态壁纸。?123456再创建一个xml的属性文件attrs.xml,代码如下:?1234567891011121314动态壁纸的服务代码如下:?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105package net.androgames.blog.sample.livewallpaper;import android.content.SharedPreferences;import android.service.wallpaper.WallpaperService;import android.view.MotionEvent;import android.view.SurfaceHolder;/* Android Live Wallpaper Archetype* author antoine vianey* under GPL v3 : /licenses/gpl-3.0.html*/public class LiveWallpaperService extends WallpaperService Overridepublic Engine onCreateEngine() return new SampleEngine();Overridepublic void onCreate() super.onCreate();Overridepublic void onDestroy() super.onDestroy();public class SampleEngine extends Engine private LiveWallpaperPainting painting;SampleEngine() SurfaceHolder holder = getSurfaceHolder();painting = new LiveWallpaperPainting(holder, getApplicationContext();Overridepublic void onCreate(SurfaceHolder surfaceHolder) super.onCreate(surfaceHolder);/ register listeners and callbacks heresetTouchEventsEnabled(true);Overridepublic void onDestroy() super.onDestroy();/ remove listeners and callbacks herepainting.stopPainting();Overridepublic void onVisibilityChanged(boolean visible) if (visible) / register listeners and callbacks herepainting.resumePainting(); else / remove listeners and callbacks herepainting.pausePainting();Overridepublic void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) super.onSurfaceChanged(holder, format, width, height);painting.setSurfaceSize(width, height);Overridepublic void onSurfaceCreated(SurfaceHolder holder) super.onSurfaceCreated(holder);/ start paintingpainting.start();Overridepublic void onSurfaceDestroyed(SurfaceHolder holder) super.onSurfaceDestroyed(holder);boolean retry = true;painting.stopPainting();while (retry) try painting.join();retry = false; catch (InterruptedException e) Overridepublic void onOffsetsChanged(float xOffset, float yOffset, float xStep, float yStep, int xPixels, int yPixels) Overridepublic void onTouchEvent(MotionEvent event) super.onTouchEvent(event);painting.doTouchEvent(event);当壁纸的显示、状态或大小变化是,会调用Engine的onCreate,onDestroy,onVisibilityChanged,onSurfaceChanged,onSurfaceCreated和onSurfaceDestroyed方法。有了这些方法,动态壁纸才能展现出动画效果。而通过设置setTouchEventsEnabled(true),并且调用onTouchEvent(MotionEvent event)方法,来激活触摸事件。我们在绘画墙纸的时候,也会使用一个单独的绘画线程:?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140package net.androgames.blog.sample.livewallpaper;import android.content.Context;import android.graphics.Canvas;import android.view.MotionEvent;import android.view.SurfaceHolder;/* Android Live Wallpaper painting thread Archetype* author antoine vianey* GPL v3 : /licenses/gpl-3.0.html*/public class LiveWallpaperPainting extends Thread /* Reference to the View and the context */private SurfaceHolder surfaceHolder;private Context context;/* State */private boolean wait;private boolean run;/* Dimensions */private int width;private int height;/* Time tracking */private long previousTime;private long currentTime;public LiveWallpaperPainting(SurfaceHolder surfaceHolder, Context context) / keep a reference of the context and the surface/ the context is needed if you want to inflate/ some resources from your livewallpaper .apkthis.surfaceHolder = surfaceHolder;this.context = context;/ dont animate until surface is created and displayedthis.wait = true;/* Pauses the live wallpaper animation*/public void pausePainting() this.wait = true;synchronized(this) this.notify();/* Resume the live wallpaper animation*/public void resumePainting() this.wait = false;synchronized(this) this.notify();/* Stop the live wallpaper animation*/public void stopPainting() this.run = false;synchronized(this) this.notify();Overridepublic void run() this.run = true;Canvas c = null;while (run) try c = this.surfaceHolder.lockCanvas(null);synchronized (this.surfaceHolder) currentTime = System.currentTimeMillis();updatePhysics();doDraw(c);previousTime = currentTime; finally if (c != null) this.surfaceHolder.unlockCanvasAndPost(c);/ pause if no need to animatesynchronized (this) if (wait) try wait(); catch (Exception e) /* Invoke when the surface dimension change*/public void setSurfaceSize(int width, int height) this.width = width;this.height = height;synchronized(this) this.notify();/* Invoke while the screen is touched*/public void doTouchEvent(MotionEvent event) / handle the event here/ if there is something to animate/ then wake upthis.wait = false;synchronized(this) notify();/* Do the actual drawing stuff*/private void doDraw(Canvas canvas) /* Update the animatio

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论