移動(dòng)應(yīng)用開(kāi)發(fā)課件:Widget與Gallery開(kāi)發(fā)_第1頁(yè)
移動(dòng)應(yīng)用開(kāi)發(fā)課件:Widget與Gallery開(kāi)發(fā)_第2頁(yè)
移動(dòng)應(yīng)用開(kāi)發(fā)課件:Widget與Gallery開(kāi)發(fā)_第3頁(yè)
移動(dòng)應(yīng)用開(kāi)發(fā)課件:Widget與Gallery開(kāi)發(fā)_第4頁(yè)
移動(dòng)應(yīng)用開(kāi)發(fā)課件:Widget與Gallery開(kāi)發(fā)_第5頁(yè)
已閱讀5頁(yè),還剩19頁(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)介

#7Widget開(kāi)發(fā)劉寧Email:liuning2@Widget是Android1.5的一個(gè)新特性,允許程序顯示一些常用而又重要的信息在用戶的Homescreen(桌面主屏)上標(biāo)準(zhǔn)的Android系統(tǒng)映像包含了一些示例widgets包括指針時(shí)鐘、音樂(lè)播放器和其他工具如Google搜索欄Widget和標(biāo)準(zhǔn)的Apps相比沒(méi)有太大的區(qū)別,更多的是在UI上的處理,邏輯執(zhí)行設(shè)計(jì)成服務(wù),具備更穩(wěn)定和更高的可靠性Widget的開(kāi)發(fā)34Widget的開(kāi)發(fā)查看官方文檔:/guide/topics/appwidgets/index.htmlAppWidgetDesignGuidelines6AppWidgetProviderInfo

object定義一個(gè)對(duì)象(XML)DescribesthemetadataforanAppWidget,suchastheAppWidget'slayout,updatefrequency,andtheAppWidgetProviderclass.ThisshouldbedefinedinXML.AppWidgetProvider

classimplementation實(shí)現(xiàn)一個(gè)類(java)DefinesthebasicmethodsthatallowyoutoprogrammaticallyinterfacewiththeAppWidget,basedonbroadcastevents.Throughit,youwillreceivebroadcastswhentheAppWidgetisupdated,enabled,disabledanddeleted.Viewlayout定義初始布局(XML)DefinestheinitiallayoutfortheAppWidget,definedinXML.Additionally,youcanimplementanAppWidgetconfigurationActivity.ThisisanoptionalActivitythatlauncheswhentheuseraddsyourAppWidgetandallowshimorhertomodifyAppWidgetsettingsatcreate-time.Widget的開(kāi)發(fā)Widget的開(kāi)發(fā)典型的AndroidWidget有三個(gè)主要組件,一個(gè)邊框、一個(gè)框架和圖形控件以及其他元素。新建一個(gè)Android工程命名為:WidgetDemo.準(zhǔn)備素材,一個(gè)是Widget的圖標(biāo),一個(gè)是Widget的背景。存放目錄如圖:修改string.xml文件<?xml

version="1.0"

encoding="utf-8"?>

<resources>

<string

name="hello">Hello

World,

WidetDemo!</string>

<string

name="app_name">DaysToWorldCup</string>

</resources>

Widget的開(kāi)發(fā)四、建立Widget內(nèi)容提供者文件,我們?cè)趓es下建立xml文件夾,并且新建一個(gè)widget_provider.xml代碼入下:<?xml

version="1.0"

encoding="utf-8"?>

<appwidget-provider

xmlns:android="/apk/res/android"

android:minWidth="50dip"

android:minHeight="50dip"

android:updatePeriodMillis="10000"

android:initialLayout=“@layout/main”

/>五、修改main.xml布局:<?xml

version="1.0"

encoding="utf-8"?>

<LinearLayout

xmlns:android="/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background=“@drawable/wordcup”

>

<TextView

android:id="@+id/wordcup"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

android:textSize="12px"

android:textColor=“#ff0000”

/>

</LinearLayout>

Widget的開(kāi)發(fā)

Widget的開(kāi)發(fā)六、修改WidgetDemo.java代碼public

class

WidetDemo

extends

AppWidgetProvider

{

@Override

public

void

onUpdate(Context

context,

AppWidgetManager

appWidgetManager,int[]

appWidgetIds)

{}

七、修改配置文件AndroidManifest.xml八、添加到桌面

問(wèn)題:如何處理Widget事件?主屏幕實(shí)際上是一個(gè)系統(tǒng)程序,因?yàn)榘踩颍绦騿T不容易直接修改其運(yùn)行代碼Android提供給用戶程序訪問(wèn)主屏幕和修改特定區(qū)域內(nèi)容的方法:RemoteView架構(gòu)RemoteView架構(gòu)允許用戶程序更新主屏幕的View點(diǎn)擊Widget激活點(diǎn)擊事件Android會(huì)將其轉(zhuǎn)發(fā)給用戶程序,由AppWidgetProviders類處理用戶程序可更新主屏幕Widget.Workingwithremoteviews12(例子)配置文件添加:Widget事件處理13<receiverandroid:label="@string/app_name"

android:name=".LunchWidget"><intent-filter><actionandroid:name="android.appwidget.action.APPWIDGET_UPDATE"></action><actionandroid:name="com.lunchtime.LunchWidget.CLICK"></action></intent-filter><meta-dataandroid:name="vider"

android:resource="@xml/widget_provider"></meta-data></receiver>在onUpdate添加代碼定義并發(fā)送事件Widget事件處理publicclassLunchWidgetextendsAppWidgetProvider{@OverridepublicvoidonUpdate(Contextcontext,AppWidgetManager

appWidgetManager,int[]appWidgetIds){super.onUpdate(context,appWidgetManager,appWidgetIds);RemoteViews

updateViews=new RemoteViews(context.getPackageName(),

R.layout.widget);Intenti=newIntent("com.lunchtime.LunchWidget.CLICK");PendingIntentpi=PendingIntent.getBroadcast(context,0,i,0);updateViews.setOnClickPendingIntent(R.id.widgetbutton,pi);ComponentNameme=newComponentName(context,LunchWidget.class);appWidgetManager.updateAppWidget(me,updateViews);}14添加onReceive()處理事件Widget事件處理15@OverridepublicvoidonReceive(Contextcontext,Intentintent){super.onReceive(context,intent);if(intent.getAction().equals("com.lunchtime.LunchWidget.CLICK")){Toast.makeText(context,"Opps!!Youhitme!!",Toast.LENGTH_SHORT).show();}}Gallery組件可以橫向顯示一個(gè)圖像列表,當(dāng)單擊當(dāng)前圖像的后一個(gè)圖像時(shí),這個(gè)圖像列表會(huì)向左移動(dòng)一格,當(dāng)單擊當(dāng)前圖像的前一個(gè)圖像時(shí),這個(gè)圖像列表會(huì)向右移動(dòng)一樣。也可以通過(guò)拖動(dòng)的方式來(lái)向左和向右移動(dòng)圖像列表。Gallery組件綜合例子1、橫向滑動(dòng)(Gallery)2、顯示相應(yīng)圖片(ImageSwitcher)Gallery組件的使用拷貝相關(guān)文件,并將這些文件都放在res\drawable目錄中(大圖、縮略圖);引入資源:將這些圖像資源ID都保存在int數(shù)組中:privateInteger[]mThumbIds={R.drawable.sample_thumb_0,R.drawable.sample_thumb_1,R.drawable.sample_thumb_2,R.drawable.sample_thumb_3,R.drawable.sample_thumb_4,R.drawable.sample_thumb_5,R.drawable.sample_thumb_6,.drawable.sample_thumb_7};privateInteger[]mImageIds={

R.drawable.sample_0,R.drawable.sample_1,R.drawable.sample_2,R.drawable.sample_3,R.drawable.sample_4,R.drawable.sample_5,R.drawable.sample_6,R.drawable.sample_7};

Gallery組件綜合例子1、大圖2、縮略圖引入組件在image_show.xml文件中配置Gallery組件和ImageSwitcher組件<?xmlversion="1.0"encoding="utf-8"?><RelativeLayout

xmlns:android="/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"><ImageSwitcher

android:id="@+id/switcher"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_alignParentTop="true"

android:layout_alignParentLeft="true"/>

<Galleryandroid:id="@+id/gallery"

android:background="#55000000"

android:layout_width="fill_parent"

android:layout_height="60dp"

android:layout_alignParentBottom="true"

android:layout_alignParentLeft="true"

android:gravity="center_vertical"

android:spacing="16dp"/></RelativeLayout>Gallery組件綜合例子4、在ImageShowActivity的onCreate方法中加載組件public

void

onCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.image_show);

setTitle("ImageShowActivity");//裝載ImageSwitcher組件

mSwitcher=(ImageSwitcher)findViewById(R.id.switcher);

mSwitcher.setFactory(this);mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));

mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));

//裝載Gallery組件Galleryg=(Gallery)findViewById(R.id.gallery);//創(chuàng)建用于描述圖像數(shù)據(jù)的ImageAdapter對(duì)象

g.setAdapter(new

ImageAdapter(this));

g.setOnItemSelectedListener(this);}Gallery組件綜合例子重要的類:ImageAdapter是android.widget.BaseAdapter的子類,用于描述圖像信息。public

class

ImageAdapter

extends

BaseAdapter{

privateContextmContext;

public

ImageAdapter(Contextc){mContext=c;}

public

int

getCount(){return

mThumbIds.length}

publicObjectgetItem(intposition){returnposition;}

public

long

getItemId(intposition){returnposition;}

publicViewgetView(intposition,ViewconvertView,ViewGroupparent){

ImageView

i=new

ImageView(mContext);

i.setImageResource(mThumbIds[position]);

i.setImageResource(mThumbIds[position]);

i.setAdjustViewBounds(true);

i.setLayoutParams(new

Gallery.LayoutParams(

LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

i.setBackgroundResource(R.drawable.picture_frame);return

i;}

}Gallery組件綜合例子Gallery組件綜合例子Gallery只能有限地顯示指定的圖像。當(dāng)組件顯示到最后一張時(shí),就不會(huì)再繼續(xù)顯示。當(dāng)希望圖像顯示到最后一張時(shí)再重第一張開(kāi)始顯示,也就是循環(huán)顯示。就需要對(duì)Gallery的Adapter對(duì)象進(jìn)行一番改進(jìn)。在ImageAdapter類中有兩個(gè)非常重要的方法:getCount:此方法在基類中的描述

abstractint

getCount()HowmanyitemsareinthedatasetrepresentedbythisAdapter.

用于返回圖像總數(shù)。getView。當(dāng)Gallery組件要顯示某一個(gè)圖像時(shí),就會(huì)調(diào)用getView方法,并將當(dāng)前的圖像索引(position參數(shù))傳入該方法。一般getView方法用于返回每一個(gè)顯示圖像的組件(ImageView對(duì)象)。在getView方法中除了創(chuàng)建了ImageView對(duì)象,還用從mThumbIDs數(shù)組中獲得了相應(yīng)的圖像資源ID來(lái)設(shè)置在ImageView中顯示的圖像。最后還設(shè)置了Gallery組件的背景顯示風(fēng)格。Gallery組件綜合例子public

intgetCount(){return

mThumbIds.length;}publicViewgetView(intposition,ViewconvertView,ViewGroupparent){ImageViewi=newImageView(mContext);i.setImageResource(mThumbIds[position]);i.setImageResource(mThumbIds[position]);i.setAdjustViewBounds(true);i.setLayoutParams(newGallery.LayoutPar

溫馨提示

  • 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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論