




已閱讀5頁(yè),還剩3頁(yè)未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(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à)背景效果。在本教程中,我們將教會(huì)你如何去制作一個(gè)交互式的動(dòng)態(tài)壁紙。動(dòng)態(tài)壁紙是一個(gè)Android應(yīng)用程序,包括一個(gè)服務(wù)(WallpaperService)。該服務(wù)必須包括一個(gè)引擎(WallpaperService.Engine)。該引擎是連接用戶(hù)、桌面、系統(tǒng)之間的橋梁。它也可以繪制桌面壁紙。首先,必須由內(nèi)在的Engine類(lèi)創(chuàng)建一個(gè)WallpaperService類(lèi)。該服務(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)畫(huà)效果。而通過(guò)設(shè)置setTouchEventsEnabled(true),并且調(diào)用onTouchEvent(MotionEvent event)方法,來(lái)激活觸摸事件。我們?cè)诶L畫(huà)墻紙的時(shí)候,也會(huì)使用一個(gè)單獨(dú)的繪畫(huà)線(xiàn)程:?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)益歸上傳用戶(hù)所有。
- 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ì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 樓盤(pán)迪斯尼活動(dòng)方案
- 校際網(wǎng)絡(luò)教研活動(dòng)方案
- 沈陽(yáng)年會(huì)公司團(tuán)購(gòu)活動(dòng)方案
- 治水創(chuàng)意活動(dòng)方案
- 民族影展活動(dòng)方案
- 武川企業(yè)活動(dòng)策劃方案
- 夢(mèng)想卡片活動(dòng)方案
- 河北防災(zāi)減災(zāi)活動(dòng)方案
- 植樹(shù)節(jié)活動(dòng)超市活動(dòng)方案
- 殘疾人發(fā)放禮品活動(dòng)方案
- 2025至2030中國(guó)羊毛制品行業(yè)市場(chǎng)發(fā)展現(xiàn)狀及發(fā)展趨勢(shì)與投資報(bào)告
- 股權(quán)投資項(xiàng)目可行性研究報(bào)告
- 2025年高考山東卷物理試題講評(píng)及備考策略指導(dǎo)(課件)
- 兒童沙門(mén)菌感染診療要點(diǎn)
- JGJT46-2024《施工現(xiàn)場(chǎng)臨時(shí)用電安全技術(shù)標(biāo)準(zhǔn)》條文解讀
- GB/T 10610-2009產(chǎn)品幾何技術(shù)規(guī)范(GPS)表面結(jié)構(gòu)輪廓法評(píng)定表面結(jié)構(gòu)的規(guī)則和方法
- 圍堰施工監(jiān)理實(shí)施細(xì)則
- 《世界經(jīng)濟(jì)史》課程教學(xué)大綱
- 小學(xué)語(yǔ)文一到六年級(jí)生字表
- U型橋臺(tái)施工組織設(shè)計(jì)
- 環(huán)境監(jiān)測(cè)站招聘考試試題及答案
評(píng)論
0/150
提交評(píng)論