【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android Presentation如何實(shí)現(xiàn)雙屏異顯_第1頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android Presentation如何實(shí)現(xiàn)雙屏異顯_第2頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android Presentation如何實(shí)現(xiàn)雙屏異顯_第3頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android Presentation如何實(shí)現(xiàn)雙屏異顯_第4頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android Presentation如何實(shí)現(xiàn)雙屏異顯_第5頁(yè)
免費(fèi)預(yù)覽已結(jié)束,剩余1頁(yè)可下載查看

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】AndroidPresentation如何實(shí)現(xiàn)雙屏異顯

這篇文章主要介紹AndroidPresentation如何實(shí)現(xiàn)雙屏異顯,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!一、概述現(xiàn)在越來(lái)越多的Android設(shè)備有多個(gè)屏幕,雙屏異顯應(yīng)用場(chǎng)景最多的應(yīng)該就是類(lèi)似于收銀平臺(tái)那種設(shè)備,在主屏上店員能夠?qū)c(diǎn)商品進(jìn)行選擇錄入,副屏則是展示給我們的賬單詳情,但是它只通過(guò)了一個(gè)軟件系統(tǒng)就實(shí)現(xiàn)了雙屏異顯這個(gè)功能,而Presentation正是這其中的關(guān)鍵。二、Presentation分析1.簡(jiǎn)述:首先從它的繼承關(guān)系上來(lái)看Presentation是繼承自Dialog的,就是說(shuō)它其實(shí)就是一種特殊的Dialog用于在第二個(gè)屏幕上顯示內(nèi)容的,它在創(chuàng)建時(shí)會(huì)和它的目標(biāo)展示屏幕相關(guān)聯(lián),包括它的context和一些配置參數(shù)等。2.Context:然而這里提到的context和它的容器所處的context會(huì)有不同,它會(huì)用自身的context去加載presentation的布局和相關(guān)資源以此來(lái)確保在目標(biāo)屏幕上能夠展示正確的大小和獲取合適的屏幕密度。3.自動(dòng)移除:雖說(shuō)presentation和它相關(guān)聯(lián)的Activity的context不同,但是他們也不是完全分離的關(guān)系,當(dāng)和presentation相關(guān)聯(lián)的屏幕被移除后,presentation也會(huì)自動(dòng)的被移除,所以當(dāng)Activity處于pause和resume的狀態(tài)時(shí)Presentation也需要特別注意當(dāng)前顯示的內(nèi)容的狀態(tài)。4.屏幕選擇:因?yàn)橛袝r(shí)候我們的Android設(shè)備有多個(gè)屏幕,所以選擇合適的屏幕去展示就顯得非常重要了,所以在我們顯示Presentation的時(shí)候需要去讓我們的系統(tǒng)去選擇合適的屏幕來(lái)進(jìn)行展示,以下是兩種方式去選擇一個(gè)合適的屏幕。這是我們選擇展示presentation最簡(jiǎn)單的一種方式,mediarouter是一種系統(tǒng)層級(jí)的服務(wù),它能夠追蹤到系統(tǒng)當(dāng)中所有可用的音頻和視屏route,當(dāng)有路徑被選中或取消選中,還有當(dāng)適合用presentation進(jìn)行顯示的時(shí)候的route改變的時(shí)候它會(huì)發(fā)送一個(gè)通知,然后應(yīng)用本身會(huì)監(jiān)控這個(gè)通知自動(dòng)的去選擇presentation的展示或者隱藏,這里的推薦使用的presentationdisplay其實(shí)只是mediarouter推薦的,如果我們的應(yīng)用需要在第二個(gè)屏幕上進(jìn)行顯示就使用,如果不用的話就用本地來(lái)展示內(nèi)容。

利用mediarouter去選擇presentation的顯示屏幕利用displaymanager去選擇persentation的顯示屏幕DisplayManager能夠監(jiān)控到我們系統(tǒng)當(dāng)中的所有連接上的顯示設(shè)備,然而不是所有的設(shè)備都適用于作為副屏來(lái)進(jìn)行內(nèi)容展示的,比如當(dāng)一個(gè)Activity想顯示一個(gè)presentation在主屏幕上,其實(shí)效果就會(huì)相當(dāng)于在主Activity當(dāng)中顯示了一個(gè)特殊的Dialog,所以當(dāng)我們選擇這種方式去選用Presentation去顯示的時(shí)候就必須給它綁定一個(gè)可用的display。三、源碼分析首先來(lái)看它的構(gòu)造函數(shù)public

Presentation(Context

outerContext,

Display

display,

int

theme)

{

super(createPresentationContext(outerContext,

display,

theme),

theme,

false);

mDisplay

=

display;

mDisplayManager

=

(DisplayManager)getContext().getSystemService(DISPLAY_SERVICE);

final

Window

w

=

getWindow();

final

WindowManager.LayoutParams

attr

=

w.getAttributes();

attr.token

=

mToken;

w.setAttributes(attr);

w.setGravity(Gravity.FILL);

w.setType(TYPE_PRESENTATION);

setCanceledOnTouchOutside(false);

}在它的形參中第一個(gè)Context參數(shù)非常重要,這是應(yīng)用正在展示presentation的一個(gè)context,它是Presentation自己創(chuàng)建的它自己的一個(gè)context,基于這個(gè)context才能正確的在它所關(guān)聯(lián)的屏幕上展示合適的信息。然后代碼里面設(shè)置了這個(gè)window的相關(guān)屬性。接著我們看看它自身的Context的創(chuàng)建private

static

Context

createPresentationContext(

Context

outerContext,

Display

display,

int

theme)

{

//首先判斷傳入的context和display是否為空,為空則拋出異常

if

(outerContext

==

null)

{

throw

new

IllegalArgumentException("outerContext

must

not

be

null");

}

if

(display

==

null)

{

throw

new

IllegalArgumentException("display

must

not

be

null");

}

Context

displayContext

=

outerContext.createDisplayContext(display);

//這里是對(duì)它的主題的判斷,為0即為默認(rèn)主題

if

(theme

==

0)

{

TypedValue

outValue

=

new

TypedValue();

displayContext.getTheme().resolveAttribute(

ernal.R.attr.presentationTheme,

outValue,

true);

theme

=

outValue.resourceId;

}

//

Derive

the

display's

window

manager

from

the

outer

window

manager.

//

We

do

this

because

the

outer

window

manager

have

some

extra

information

//

such

as

the

parent

window,

which

is

important

if

the

presentation

uses

//

an

application

window

type.

final

WindowManagerImpl

outerWindowManager

=

(WindowManagerImpl)outerContext.getSystemService(WINDOW_SERVICE);

final

WindowManagerImpl

displayWindowManager

=

outerWindowManager.createPresentationWindowManager(displayContext);

//因?yàn)镃ontextThemeWrapper的父類(lèi)是我們的Context

//所以這里最終返回的就是Presentation給我們創(chuàng)建好的Context

return

new

ContextThemeWrapper(displayContext,

theme)

{

//在這個(gè)方法中又返回的是Object對(duì)象

@Override

public

Object

getSystemService(String

name)

{

if

(WINDOW_SERVICE.equals(name))

{

return

displayWindowManager;

//如果和這個(gè)傳入的name相同的話返回的就是上面windowManager創(chuàng)建出來(lái)的WindowManager的一個(gè)具體實(shí)現(xiàn)

}

//否則就返回的是Context這個(gè)抽象類(lèi)中的一種服務(wù)類(lèi)型

return

super.getSystemService(name);

}

};

}接著我們繼續(xù)看一下Presentation對(duì)屏幕增加、移除和改變的監(jiān)聽(tīng)private

final

DisplayListener

mDisplayListener

=

new

DisplayListener()

{

@Override

public

void

onDisplayAdded(int

displayId)

{

}

@Override

public

void

onDisplayRemoved(int

displayId)

{

if

(displayId

==

mDisplay.getDisplayId())

{

handleDisplayRemoved();

}

}

@Override

public

void

onDisplayChanged(int

displayId)

{

if

(displayId

==

mDisplay.getDisplayId())

{

handleDisplayChanged();

}

}

};這里我們看到它對(duì)add并沒(méi)有進(jìn)行處理,所以我們進(jìn)一步去看一下onDisplayRemoved中的關(guān)鍵handlerDisplayRemoved和onDisplayChanged中的核心handlerDisplayChanged的實(shí)現(xiàn)handlerDisplayChanged:private

void

handleDisplayChanged()

{

onDisplayChanged();

//

We

currently

do

not

support

configuration

changes

for

presentations

//

(although

we

could

add

that

feature

with

a

bit

more

work).

//

If

the

display

metrics

have

changed

in

any

way

then

the

current

configuration

//

is

invalid

and

the

application

must

recreate

the

presentation

to

get

//

a

new

context.

if

(!isConfigurationStillValid())

{

Log.i(TAG,

"Presentation

is

being

dismissed

because

the

"

+

"display

metrics

have

changed

since

it

was

created.");

cancel();

}

}在這個(gè)方法中,我們遺憾的發(fā)現(xiàn)Google程序員并沒(méi)有對(duì)當(dāng)前屏幕配置發(fā)生改變后做特殊的處理,所以當(dāng)我們的屏幕尺寸等信息改變時(shí),我們的presentation必須重新Create去重新獲取context,再重新進(jìn)行顯示。@Override

protected

void

onStart()

{

super.onStart();

mDisplayManager.registerDisplayListener(mDisplayListener,

mHandler);

//

Since

we

were

not

watching

for

display

changes

until

just

now,

there

is

a

//

chance

that

the

display

metrics

have

changed.

If

so,

we

will

need

to

//

dismiss

the

presentation

immediately.

This

case

is

expected

//

to

be

rare

but

surprising,

so

we'll

write

a

log

message

about

it.

if

(!isConfigurationStillValid())

{

Log.i(TAG,

"Presentation

is

being

dismissed

because

the

"

+

"display

metrics

have

changed

since

it

was

created.");

mHandler.sendEmptyMessage(MSG_CANCEL);

}

}同時(shí)在onStart中我們也能發(fā)現(xiàn),因?yàn)樗鼤簳r(shí)還沒(méi)有對(duì)displaychange進(jìn)行監(jiān)聽(tīng),而這里又是有可能會(huì)改變的,所以在這種情況下它是打印了一

溫馨提示

  • 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)論