RatioLayout乙個螢幕適配三方庫原始碼分析

2021-07-24 10:52:23 字數 3569 閱讀 1777

ratiolayout乙個螢幕適配三方庫原始碼分析
我的github部落格詳解

使用dp進行ui設定:

可以看出,在不同解析度或者密度的螢幕,相同dp值設定的效果是不同的。

使用ratio進行ui設定(按比例設定):

可以看出,在不同解析度或者密度的螢幕,ratio按比例設定的效果是比較出色的。

簡單分析ratiolayout:

1,初始化一些引數,設定資料,為了在xml中設定屬性到自定義布局中使用

2,在系統測量之前手動測量一次,獲取到參考值(螢幕最大寬度或者最大高度),根據參考值與比例ratio計算出高度或者寬度

3,生成測量規格,將新的測量規格傳遞給系統onmeasure()方法中,進行測量。

ratiolayout原始碼分析:

ratiolayoutdelegate 核心類,用於計算出ratio比例的實際寬高的測量規格

public

final

class

ratiolayoutdelegate

extends

view & ratiomeasuredelegate>

public

static

ratiolayoutdelegate obtain(target target, attributeset attrs, int defstyleattr)

@suppresswarnings("unchecked")

public

static

ratiolayoutdelegate obtain(target target, attributeset attrs, int defstyleattr, int defstyleres)

private

final target mratiomeasuredelegate;

private ratiodatummode mratiodatummode;

private

float mdatumwidth = 0.0f;

private

float mdatumheight = 0.0f;

private

int mwidthmeasurespec, mheightmeasurespec;

private

ratiolayoutdelegate(target target, attributeset attrs, int defstyleattr, int defstyleres)else

if(datum == 2)

mdatumwidth = typedarray.getfloat(r.styleable.viewsizecalculate_widthratio, mdatumwidth);

mdatumheight = typedarray.getfloat(r.styleable.viewsizecalculate_heightratio, mdatumheight);

typedarray.recycle();}}

public

void

onmeasure(int widthmeasurespec, int heightmeasurespec) else

mwidthmeasurespec = view.measurespec.makemeasurespec(measuredwidth, view.measurespec.exactly);

mheightmeasurespec = view.measurespec.makemeasurespec(measuredheight, view.measurespec.exactly);}}

public

intgetwidthmeasurespec()

public

intgetheightmeasurespec()

public

void

setratio(ratiodatummode mode,float datumwidth,float datumheight)

this.mratiodatummode = mode;

this.mdatumwidth = datumwidth;

this.mdatumheight = datumheight;

this.mratiomeasuredelegate.requestlayout();}}

關於測量重點,在onmeasure()方法中:

1,首先手動測量一次,獲取參考值(最大寬度或最大高度)

public

void

onmeasure(int widthmeasurespec, int heightmeasurespec) else

mwidthmeasurespec = view.measurespec.makemeasurespec(measuredwidth, view.measurespec.exactly);

mheightmeasurespec = view.measurespec.makemeasurespec(measuredheight, view.measurespec.exactly);

我們再看乙個具體實現ratio比例布局的例項:

public class ratioframelayout extends framelayout implements ratiomeasuredelegate

public

ratioframelayout(context context, attributeset attrs)

public

ratioframelayout(context context, attributeset attrs, int defstyleattr)

@targetapi(build.version_codes.lollipop)

public

ratioframelayout(context context, attributeset attrs, int defstyleattr, int defstyleres)

@override

protected

void

onmeasure(int widthmeasurespec, int heightmeasurespec)

super.onmeasure(widthmeasurespec, heightmeasurespec);

}@override

public

void

setdelegatemeasureddimension(int measuredwidth, int measuredheight)

@override

public

void

setratio(ratiodatummode mode, float datumwidth, float datumheight)

}}在onmeasure方法中可以看出,先動手測量一次,將新的測量規格傳遞下去,就實現了按比例設定寬高的動作。

現在,你也可以自己寫乙個類來實現比例設定ui布局的類了。

11/8/2016 1:02:56 pm

乙個點亮螢幕的service

這個版本是只能點亮不能解鎖的版本 注意很多句子都被注釋掉了,那部分是用來實現解鎖螢幕的 達到了預期的效果,特此紀念。把 貼出來 package com.larry.msglighter import android.content.context import android.content.int...

乙個巨集解決 iOS螢幕適配

用乙個巨集 解決 ios各種機型的螢幕適配問題 前提條件 設計師給出的效果圖應以iphone6p為基準。什麼時候使用這個巨集?所有控制項的尺寸 x值y值,cell的高度,文字的字型大小 如何使用這個巨集?在 supporting files 資料夾中的 prefixheader.pch 編寫 def...

使用Qt 開發乙個螢幕錄影程式

華清遠見嵌入式學院 講師。如果需要使用到獲得當前幀,那麼就需要使用到乙個定時器,下面的 中包含了針對於乙個定時器的程式設計,程式完成的功能是開啟定時器,並沒有關閉,讀者有興趣可以建立乙個類內可見的定時器,使得多個部件可以控制。這個例子是從螢幕截圖程式變化過來,所以在最後使用的是按鈕儲存的效果。ifn...