日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android N Display Size

發(fā)布時間:2025/4/16 Android 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android N Display Size 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

點擊打開鏈接

Android N新特性-屏幕縮放

Android?7.0 支持用戶設(shè)置顯示尺寸,以放大或縮小屏幕上的所有元素,從而提升設(shè)備對視力不佳用戶的可訪問性。用戶無法將屏幕縮放至低于最小屏幕寬度 sw320dp,該寬度是 Nexus 4 的寬度,也是常規(guī)中等大小手機的寬度。

當(dāng)設(shè)備密度發(fā)生更改時,系統(tǒng)會以如下方式通知正在運行的應(yīng)用:?
如果是面向 API 級別 23 或更低版本系統(tǒng)的應(yīng)用,系統(tǒng)會自動終止其所有后臺進程。這意味著如果用戶切換離開此類應(yīng)用,轉(zhuǎn)而打開 Settings 屏幕并更改 Display size 設(shè)置,則系統(tǒng)會像處理內(nèi)存不足的情況一樣終止該應(yīng)用。如果應(yīng)用具有任何前臺進程,則系統(tǒng)會如處理運行時更改中所述將配置變更通知給這些進程,就像對待設(shè)備屏幕方向變更一樣。?
如果是面向?android?7.0 的應(yīng)用,則其所有進程(前臺和后臺)都會收到有關(guān)配置變更的通知,如處理運行時更改中所述。?
大多數(shù)應(yīng)用并不需要進行任何更改即可支持此功能,不過前提是這些應(yīng)用遵循 Android 最佳做法。具體要檢查的事項:

  • 在屏幕寬度為 sw320dp 的設(shè)備上測試您的應(yīng)用,并確保其充分運行。
  • 當(dāng)設(shè)備配置發(fā)生變更時,更新任何與密度相關(guān)的緩存信息,例如緩存位圖或從網(wǎng)絡(luò)加載的資源。當(dāng)應(yīng)用從暫停狀態(tài)恢復(fù)運行時,檢查配置變更。?
    注:如果您要緩存與配置相關(guān)的數(shù)據(jù),則最好也包括相關(guān)元數(shù)據(jù),例如該數(shù)據(jù)對應(yīng)的屏幕尺寸或像素密度。保存這些元數(shù)據(jù)便于您在配置變更后決定是否需要刷新緩存數(shù)據(jù)。
  • 避免用像素單位指定尺寸,因為像素不會隨屏幕密度縮放。應(yīng)改為使用與密度無關(guān)像素 (dp) 單位指定尺寸?

Dpi,dp,ppi,sp,in&px參考

Android屏幕適配全攻略


Display Size分析

Display Size 對應(yīng)手機的Settings->Display->Display size。用來縮放手機屏幕。?
流程分析?
1.?screen_zoom_activity.xml?
Display Size調(diào)整設(shè)置項對應(yīng)的布局文件,通過seekbar來顯示縮放等級,通過按鈕來調(diào)整等級?
2.?CustomPreviewSeekBarPreferenceFragment.Java

final int max = Math.max(1, mEntries.length - 1); final LabeledSeekBar seekBar = (LabeledSeekBar) content.findViewById(R.id.seek_bar); seekBar.setLabels(mEntries); seekBar.setMax(max); seekBar.setProgress(mInitialIndex);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

mEntries數(shù)組長度決定了縮放調(diào)整的級數(shù),數(shù)組內(nèi)容為每個等級的標(biāo)簽,mInitialIndex決定了當(dāng)前所在的級數(shù),第一次獲取的則為默認(rèn)等級。?
3.?ScreenZoomSettings.java?
該類繼承自PreviewSeekBarPreferenceFragment.java,該類確定了mEntries和mInitialIndex的值。這兩個值都是由DisplayDensityUtils對象獲得。?
4.?DisplayDensityUtils.java?
mEntries和mInitialIndex可以看到是通過DisplayDensityUtils的getEntries()和getCurrentIndex()方法獲得。而該方法中返回值搜索代碼即可看到是在該類的構(gòu)造方法中確定。?
源碼如下

public class DisplayDensityUtils {/** Minimum increment between density scales. */private static final float MIN_SCALE_INTERVAL = 0.09f;/** Minimum density scale. This is available on all devices. */private static final float MIN_SCALE = 0.85f;/** Maximum density scale. The actual scale used depends on the device. */private static final float MAX_SCALE = 1.50f;/** Summary used for "default" scale. */public static final int SUMMARY_DEFAULT = R.string.screen_zoom_summary_default;/** Summary used for "custom" scale. */private static final int SUMMARY_CUSTOM = R.string.screen_zoom_summary_custom;/*** Summaries for scales smaller than "default" in order of smallest to* largest.*/private static final int[] SUMMARIES_SMALLER = new int[] {R.string.screen_zoom_summary_small};/*** Summaries for scales larger than "default" in order of smallest to* largest.*/private static final int[] SUMMARIES_LARGER = new int[] {R.string.screen_zoom_summary_large,R.string.screen_zoom_summary_very_large,R.string.screen_zoom_summary_extremely_large,};/*** Minimum allowed screen dimension, corresponds to resource qualifiers* "small" or "sw320dp". This value must be at least the minimum screen* size required by the CDD so that we meet developer expectations.*/private static final int MIN_DIMENSION_DP = 320;private final String[] mEntries;private final int[] mValues;private final int mDefaultDensity;private final int mCurrentIndex;public DisplayDensityUtils(Context context) {final int defaultDensity = DisplayDensityUtils.getDefaultDisplayDensity(Display.DEFAULT_DISPLAY);if (defaultDensity <= 0) {mEntries = null;mValues = null;mDefaultDensity = 0;mCurrentIndex = -1;return;}final Resources res = context.getResources();final DisplayMetrics metrics = res.getDisplayMetrics();final int currentDensity = metrics.densityDpi;int currentDensityIndex = -1;// Compute number of "larger" and "smaller" scales for this display.final int minDimensionPx = Math.min(metrics.widthPixels, metrics.heightPixels);final int maxDensity = DisplayMetrics.DENSITY_MEDIUM * minDimensionPx / MIN_DIMENSION_DP;final float maxScale = Math.min(MAX_SCALE, maxDensity / (float) defaultDensity);final float minScale = MIN_SCALE;final int numLarger = (int) MathUtils.constrain((maxScale - 1) / MIN_SCALE_INTERVAL,0, SUMMARIES_LARGER.length);final int numSmaller = (int) MathUtils.constrain((1 - minScale) / MIN_SCALE_INTERVAL,0, SUMMARIES_SMALLER.length);String[] entries = new String[1 + numSmaller + numLarger];int[] values = new int[entries.length];int curIndex = 0;if (numSmaller > 0) {final float interval = (1 - minScale) / numSmaller;for (int i = numSmaller - 1; i >= 0; i--) {// Round down to a multiple of 2 by truncating the low bit.final int density = ((int) (defaultDensity * (1 - (i + 1) * interval))) & ~1;if (currentDensity == density) {currentDensityIndex = curIndex;}entries[curIndex] = res.getString(SUMMARIES_SMALLER[i]);values[curIndex] = density;curIndex++;}}if (currentDensity == defaultDensity) {currentDensityIndex = curIndex;}values[curIndex] = defaultDensity;entries[curIndex] = res.getString(SUMMARY_DEFAULT);curIndex++;if (numLarger > 0) {final float interval = (maxScale - 1) / numLarger;for (int i = 0; i < numLarger; i++) {// Round down to a multiple of 2 by truncating the low bit.final int density = ((int) (defaultDensity * (1 + (i + 1) * interval))) & ~1;if (currentDensity == density) {currentDensityIndex = curIndex;}values[curIndex] = density;entries[curIndex] = res.getString(SUMMARIES_LARGER[i]);curIndex++;}}final int displayIndex;if (currentDensityIndex >= 0) {displayIndex = currentDensityIndex;} else {// We don't understand the current density. Must have been set by// someone else. Make room for another entry...int newLength = values.length + 1;values = Arrays.copyOf(values, newLength);values[curIndex] = currentDensity;entries = Arrays.copyOf(entries, newLength);entries[curIndex] = res.getString(SUMMARY_CUSTOM, currentDensity);displayIndex = curIndex;}mDefaultDensity = defaultDensity;mCurrentIndex = displayIndex;mEntries = entries;mValues = values;}public String[] getEntries() {return mEntries;}public int getCurrentIndex() {return mCurrentIndex;} }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133

構(gòu)造函數(shù)首先根據(jù)默認(rèn)DPI來計算可縮放的等級數(shù)->填充smaller的mEntries標(biāo)簽內(nèi)容->設(shè)置默認(rèn)密度等級的下標(biāo)和填充標(biāo)簽->填充larger的mEntries的標(biāo)簽內(nèi)容

計算等級數(shù)

final int minDimensionPx = Math.min(metrics.widthPixels, metrics.heightPixels); final int maxDensity = DisplayMetrics.DENSITY_MEDIUM * minDimensionPx / MIN_DIMENSION_DP; final float maxScale = Math.min(MAX_SCALE, maxDensity / (float) defaultDensity); final float minScale = MIN_SCALE; final int numLarger = (int) MathUtils.constrain((maxScale - 1) / MIN_SCALE_INTERVAL,0, SUMMARIES_LARGER.length); final int numSmaller = (int) MathUtils.constrain((1 - minScale) / MIN_SCALE_INTERVAL,0, SUMMARIES_SMALLER.length); String[] entries = new String[1 + numSmaller + numLarger];
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

defaultDensity是通過 DisplayDensityUtils.getDefaultDisplayDensity(Display.DEFAULT_DISPLAY)獲取,該默認(rèn)密度對應(yīng)代碼中ro.sf.lcd_density?
numSmaller為最小為0,最大為1,此處值為1?
numLarger則為0到3之間,具體取決于maxScale,它又取決于 maxDensity / (float) defaultDensity

此處以測試機5015為例,其ro.sf.lcd_density默認(rèn)為240dpi,width為480px,height為854px 則defaultDensity為240 maxDensity = DisplayMetrics.DENSITY_MEDIUM * minDimensionPx / MIN_DIMENSION_DP ==>160*480/320 =240 則maxScale = 240/240 = 1 則numLarger = 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

修改Android DPI?
代碼路徑:alps/device/sagetel/sr6580_we_n/system.prop,ro.sf.lcd_density修改該值則修改了手機的默認(rèn)密度

填充smaller

設(shè)置默認(rèn)密度

if (currentDensity == defaultDensity) {currentDensityIndex = curIndex;//設(shè)置默認(rèn)下標(biāo) } values[curIndex] = defaultDensity; entries[curIndex] = res.getString(SUMMARY_DEFAULT);填充默認(rèn)等級的描述
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

填充larger


等級客制化

可通過修改ro.sf.lcd_density值來改變縮放等級數(shù),density越小,等級數(shù)越多,但最大5級。為了界面顯示正常,我們需調(diào)整合適的density,density測試可通過

修改屏幕Dpi adb shell wm density 240 //修改屏幕密度為240dpi adb shell wm density reset //重置屏幕密度為默認(rèn)密度
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

總結(jié)

以上是生活随笔為你收集整理的Android N Display Size的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。