




已閱讀5頁,還剩3頁未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
動(dòng)態(tài)壁紙是在Android 2.1新增的一個(gè)功能。動(dòng)態(tài)壁紙可以添加到Android的桌面,具有交互式的動(dòng)畫背景效果。在本教程中,我們將教會(huì)你如何去制作一個(gè)交互式的動(dòng)態(tài)壁紙。動(dòng)態(tài)壁紙是一個(gè)Android應(yīng)用程序,包括一個(gè)服務(wù)(WallpaperService)。該服務(wù)必須包括一個(gè)引擎(WallpaperService.Engine)。該引擎是連接用戶、桌面、系統(tǒng)之間的橋梁。它也可以繪制桌面壁紙。首先,必須由內(nèi)在的Engine類創(chuàng)建一個(gè)WallpaperService類。該服務(wù)必須在AndroidManifest.xml中聲明為android.service.wallpaper.WallpaperService,這樣它才會(huì)作為動(dòng)態(tài)壁紙被手機(jī)識(shí)別。而且還要在服務(wù)配置中附加android.permission.BIND_WALLPAPER的權(quán)限許可:?12345678910111213創(chuàng)建一個(gè)XML文件,放置在應(yīng)用程序目錄下的/res/xml/中。它用來描述你的動(dòng)態(tài)壁紙。?123456再創(chuàng)建一個(gè)xml的屬性文件attrs.xml,代碼如下:?1234567891011121314動(dòng)態(tài)壁紙的服務(wù)代碼如下:?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);當(dāng)壁紙的顯示、狀態(tài)或大小變化是,會(huì)調(diào)用Engine的onCreate,onDestroy,onVisibilityChanged,onSurfaceChanged,onSurfaceCreated和onSurfaceDestroyed方法。有了這些方法,動(dòng)態(tài)壁紙才能展現(xiàn)出動(dòng)畫效果。而通過設(shè)置setTouchEventsEnabled(true),并且調(diào)用onTouchEvent(MotionEvent event)方法,來激活觸摸事件。我們?cè)诶L畫墻紙的時(shí)候,也會(huì)使用一個(gè)單獨(dú)的繪畫線程:?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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 小區(qū)防技術(shù)方案
- 舊房柱梁改造方案
- 酒吧防火分離方案
- 公園湖面養(yǎng)護(hù)服務(wù)方案
- 保護(hù)小區(qū)建設(shè)規(guī)劃方案
- 科室醫(yī)療廢物管理辦法
- 科技計(jì)劃信用管理辦法
- 科普傳播基地管理辦法
- 科研資金經(jīng)費(fèi)管理辦法
- 移動(dòng)商鋪管理辦法細(xì)則
- JG 121-2000施工升降機(jī)齒輪錐鼓形漸進(jìn)式防墜安全器
- 養(yǎng)殖場(chǎng)防疫員聘請(qǐng)協(xié)議書
- 護(hù)士考編制試題及答案
- 提升教師評(píng)價(jià)素養(yǎng)的策略及實(shí)施路徑
- 2025山西大地環(huán)境投資控股有限公司校園招聘13人筆試參考題庫附帶答案詳解
- 綠色智能建造概論 課件全套 第1-7章 緒論- 建筑綠色智能運(yùn)維
- 消防安全管理制度與操作流程匯編
- 水庫管理制度
- 《防爆安全管理》課件
- 華潤(rùn)守正評(píng)標(biāo)專家考試試題及答案
- 濟(jì)南市天橋區(qū)2025年小學(xué)六年級(jí)第二學(xué)期小升初數(shù)學(xué)試卷含解析
評(píng)論
0/150
提交評(píng)論