【移動應用開發(fā)技術】Android怎么實現(xiàn)屏幕適配_第1頁
【移動應用開發(fā)技術】Android怎么實現(xiàn)屏幕適配_第2頁
【移動應用開發(fā)技術】Android怎么實現(xiàn)屏幕適配_第3頁
【移動應用開發(fā)技術】Android怎么實現(xiàn)屏幕適配_第4頁
【移動應用開發(fā)技術】Android怎么實現(xiàn)屏幕適配_第5頁
已閱讀5頁,還剩7頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

【移動應用開發(fā)技術】Android怎么實現(xiàn)屏幕適配

這篇文章給大家介紹Android怎么實現(xiàn)屏幕適配,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。概念:1.像素單位pixel/px屏幕最小顯示單位。放大后就像每信號的電視機。2.分辨率:表示屏幕像素點個數(shù),用"寬x高"表示常見分辨率:320x480480x800720x10801080x19202k屏:2560x1440比如三星s6以后的系列機,親測看vr視頻杠杠的4k屏:4096x2160這個電視機的,當我沒說奇葩屏:例如mx4/mx4Pro,這是一種奇葩的寬屏,你家公司有這臺手機就酸爽了ios:5c5s->1136x64066s->1334x7506+6s+->1920x1080但不管iphone的還是各種Android手機,屏幕的比例都是16:9(不信你算算),所以視頻的比例幾乎都是16:9。獲取屏幕像素方法:getResources().getDisplayMetrics().widthPixels;getResources().getDisplayMetrics().heightPixels;3.尺寸單位inch英寸1inch=2.54cm,指屏幕對角線長度手機常見尺寸4.75.05.25.55.76.0加大號尺寸7.0,時不時在地鐵里看到有人捧個板磚在那:“喂!喂!”4.像素密度單位dpi(dotsperinch),翻譯過來就知道每英寸像素點的個數(shù)(當然是越多越清晰啦)計算示意圖由勾股定理知:斜邊尺寸²=寬²+高²像素密度=√寬²+高²/尺寸5.密度無關像素:單位dp/dipdensity-independentpixelAndroid特有單位,保證不同屏幕像素密度設備顯示相同的效果。密度類型代表的分辨率(px)屏幕密度(dpi)換算(px/dp)比例低密度(ldpi)240x3201201dp=0.75px3中密度(mdpi)320x4801601dp=1px4高密度(hdpi)480x8002401dp=1.5px6超高密度(xhdpi)720x12803201dp=2px8超超高密度(xxhdpi)1080x19204801dp=3px12舉個栗子:同尺寸不同分辨率屏幕假設布局中有個控件寬度為100dp,看看它的寬度是實際顯示是怎樣的第一張分辨率上100dpx2=200px,屏幕寬度的比例200:720=1:3.6第二張分辨率上100dpx3=300px,屏幕寬度的比例300:1080=1:3.6在屏幕中占比都一樣,所以界面效果是一樣的。6.獨立比例像素:單位sp/sipscale-independent-pixel用于表示字體大小,不推薦奇數(shù)容易丟失精度。雖然用dp為單位,解決了不同分辨率顯示相同尺寸,單個控件長寬一樣。但是不同手機尺寸是不一樣的,所以整體的縮放比例是不一樣的。會出現(xiàn)大屏顯示完全,小屏只顯示一大半。問題造成原因:1.訂制系統(tǒng)多種多樣:小米MIUI,魅族flyme,oppocolorOs,華為EMUI,vivoFunTouchOs等等2.各種尺寸3.類似于華為等手機帶有虛擬菜單的,而且可以調(diào)節(jié)消失與顯示,曾折磨過我一天。于是,為了解決以上問題,我們可以用以下方法,我要說了哦,就是,就是,就是:招式篇:一條很明顯的分割線1.制作.9圖請看我的另一篇文章2.用自適應和指定比例控件請看我的另一篇文章3.在自定義view中很多長度都是用px作為默認單位的,這樣會導致不同分辨率顯示不一樣,所以將要固定用dp固定長度,轉化成對應分辨率的px值,方法如下public

static

int

dp2px(Context

context,

float

dipValue)

{

final

float

scale

=

context.getResources().getDisplayMetrics().density;

return

(int)

(dipValue

*

scale

+

0.5f);

}獲取DisplayMetrics屏幕測量類,獲取密度(每dp有多少像素),dpvalue乘以密度就是像素值,但是為什么末尾要加上0.5f呢?因為精度的問題,數(shù)學上1.1四舍五入為1,1.5為2但java里,(int)1.1=1,(int)1.9=1,只會舍,不會入所以都加上0.5f,(int)(1.1+0.5)=1,(int)(1.5+0.5)=2,保證了數(shù)學上的一致。5.在項目中針對你所需要適配的手機屏幕的分辨率自適配對應dp-px換算比這是是用鴻洋大神的尺寸生成類:public

class

CreatedimenUtil

{

private

int

baseW;

private

int

baseH;

private

String

dirStr

=

"./res";

private

final

static

String

WTemplate

=

"<dimen

name=\"x{0}\">{1}px</dimen>\n";

private

final

static

String

HTemplate

=

"<dimen

name=\"y{0}\">{1}px</dimen>\n";

/**

*

{0}-HEIGHT

*/

private

final

static

String

VALUE_TEMPLATE

=

"values-{0}x{1}";

private

static

final

String

SUPPORT_DIMESION

=

"320,480;480,800;480,854;540,960;600,1024;720,1184;720,1196;720,1280;768,1024;800,1280;1080,1812;1080,1920;1440,2560;";

private

String

supportStr

=

SUPPORT_DIMESION;

public

CreatedimenUtil(int

baseX,

int

baseY,

String

supportStr)

{

this.baseW

=

baseX;

this.baseH

=

baseY;

if

(!this.supportStr.contains(baseX

+

","

+

baseY))

{

this.supportStr

+=

baseX

+

","

+

baseY

+

";";

}

this.supportStr

+=

validateInput(supportStr);

System.out.println(supportStr);

File

dir

=

new

File(dirStr);

if

(!dir.exists())

{

dir.mkdir();

}

System.out.println(dir.getAbsoluteFile());

}

/**

*

@param

supportStr

*

w,h_...w,h;

*

@return

*/

private

String

validateInput(String

supportStr)

{

StringBuffer

sb

=

new

StringBuffer();

String[]

vals

=

supportStr.split("_");

int

w

=

-1;

int

h

=

-1;

String[]

wh;

for

(String

val

:

vals)

{

try

{

if

(val

==

null

||

val.trim().length()

==

0)

continue;

wh

=

val.split(",");

w

=

Integer.parseInt(wh[0]);

h

=

Integer.parseInt(wh[1]);

}

catch

(Exception

e)

{

System.out.println("skip

invalidate

params

:

w,h

=

"

+

val);

continue;

}

sb.append(w

+

","

+

h

+

";");

}

return

sb.toString();

}

public

void

generate()

{

String[]

vals

=

supportStr.split(";");

for

(String

val

:

vals)

{

String[]

wh

=

val.split(",");

generateXmlFile(Integer.parseInt(wh[0]),

Integer.parseInt(wh[1]));

}

}

private

void

generateXmlFile(int

w,

int

h)

{

StringBuffer

sbForWidth

=

new

StringBuffer();

sbForWidth.append("<?xml

version=\"1.0\"

encoding=\"utf-8\"?>\n");

sbForWidth.append("<resources>");

float

cellw

=

w

*

1.0f

/

baseW;

System.out.println("width

:

"

+

w

+

","

+

baseW

+

","

+

cellw);

for

(int

i

=

1;

i

<

baseW;

i++)

{

sbForWidth.append(WTemplate.replace("{0}",

i

+

"").replace("{1}",

change(cellw

*

i)

+

""));

}

sbForWidth.append(WTemplate.replace("{0}",

baseW

+

"").replace("{1}",

w

+

""));

sbForWidth.append("</resources>");

StringBuffer

sbForHeight

=

new

StringBuffer();

sbForHeight.append("<?xml

version=\"1.0\"

encoding=\"utf-8\"?>\n");

sbForHeight.append("<resources>");

float

cellh

=

h

*1.0f/

baseH;

System.out.println("height

:

"+

h

+

","

+

baseH

+

","

+

cellh);

for

(int

i

=

1;

i

<

baseH;

i++)

{

sbForHeight.append(HTemplate.replace("{0}",

i

+

"").replace("{1}",

change(cellh

*

i)

+

""));

}

sbForHeight.append(HTemplate.replace("{0}",

baseH

+

"").replace("{1}",

h

+

""));

sbForHeight.append("</resources>");

File

fileDir

=

new

File(dirStr

+

File.separator

+

VALUE_TEMPLATE.replace("{0}",

h

+

"")//

.replace("{1}",

w

+

""));

fileDir.mkdir();

File

layxFile

=

new

File(fileDir.getAbsolutePath(),

"lay_x.xml");

File

layyFile

=

new

File(fileDir.getAbsolutePath(),

"lay_y.xml");

try

{

PrintWriter

pw

=

new

PrintWriter(new

FileOutputStream(layxFile));

pw.print(sbForWidth.toString());

pw.close();

pw

=

new

PrintWriter(new

FileOutputStream(layyFile));

pw.print(sbForHeight.toString());

pw.close();

}

catch

(FileNotFoundException

e)

{

e.printStackTrace();

}

}

public

static

float

change(float

a)

{

int

temp

=

(int)

(a

*

100);

return

temp

/

100f;

}

public

static

void

main(String[]

args)

{

int

baseW

=

320;

int

baseH

=

400;

String

addition

=

"";

try

{

if

(args.length

>=

3)

{

baseW

=

Integer.parseInt(args[0]);

baseH

=

Integer.parseInt(args[1]);

addition

=

args[2];

}

else

if

(args.length

>=

2)

{

baseW

=

Integer.parseInt(args[0]);

baseH

=

Integer.parseInt(args[1]);

}

else

if

(args.length

>=

1)

{

addition

=

args[0];

}

}

catch

(NumberFor

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論