




已閱讀5頁(yè),還剩3頁(yè)未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(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/中。它用來(lái)描述你的動(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)畫效果。而通過(guò)設(shè)置setTouchEventsEnabled(true),并且調(diào)用onTouchEvent(MotionEvent event)方法,來(lái)激活觸摸事件。我們?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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 抖音商戶編導(dǎo)短視頻開(kāi)頭吸引力制度
- 全球石油市場(chǎng)供需格局調(diào)整與價(jià)格走勢(shì)對(duì)能源市場(chǎng)供需預(yù)測(cè)技術(shù)的應(yīng)用研究報(bào)告
- 浙江省寧波市鎮(zhèn)海區(qū)仁愛(ài)中學(xué)2024-2025學(xué)年數(shù)學(xué)七年級(jí)第一學(xué)期期末質(zhì)量檢測(cè)試題含解析
- 貴州黔南經(jīng)濟(jì)學(xué)院《可摘局部義齒工藝技術(shù)》2023-2024學(xué)年第一學(xué)期期末試卷
- 浙江杭州上城區(qū)2025屆化學(xué)九年級(jí)第一學(xué)期期末達(dá)標(biāo)檢測(cè)試題含解析
- 2024-2025學(xué)年陜西省咸陽(yáng)市秦嶺中學(xué)數(shù)學(xué)七年級(jí)第一學(xué)期期末達(dá)標(biāo)檢測(cè)模擬試題含解析
- 公路客運(yùn)行業(yè)2025年轉(zhuǎn)型升級(jí)與智能停車場(chǎng)建設(shè)研究報(bào)告
- 公路貨運(yùn)行業(yè)數(shù)字化轉(zhuǎn)型效率提升的關(guān)鍵瓶頸與突破路徑報(bào)告
- 共享出行市場(chǎng)共享出行市場(chǎng)潛力與2025年行業(yè)政策法規(guī)分析報(bào)告
- 國(guó)際合作與交流策略研究報(bào)告
- 水庫(kù)管理制度
- 《防爆安全管理》課件
- 華潤(rùn)守正評(píng)標(biāo)專家考試試題及答案
- 濟(jì)南市天橋區(qū)2025年小學(xué)六年級(jí)第二學(xué)期小升初數(shù)學(xué)試卷含解析
- 2025年電子商務(wù)法律法規(guī)知識(shí)測(cè)試題及答案
- 2025年人教版小學(xué)五年級(jí)下冊(cè)奧林匹克數(shù)學(xué)競(jìng)賽試卷(附參考答案)
- 四川阿壩州公開(kāi)招聘社區(qū)工作者考試全真模擬測(cè)試帶答案2024年
- 2024年寧夏中衛(wèi)公開(kāi)招聘社區(qū)工作者考試試題答案解析
- 遼寧省沈陽(yáng)市皇姑區(qū)2025屆小升初全真模擬數(shù)學(xué)檢測(cè)卷含解析
- 居住權(quán)登記合同協(xié)議
- 人工智能與大數(shù)據(jù)融合-全面剖析
評(píng)論
0/150
提交評(píng)論