網站首頁 編程語言 正文
由于Android手機廠商很多,導致了不同設備屏幕大小和分辨率都不一樣,然而我們開發者要保持在不同設備上顯示同樣的視覺效果,就需要做一些適配效果。
相關名詞解釋
- 屏幕大小:通常指的是屏幕對角線的長度,使用“寸”為單位來衡量。
- 分辨率:指手機屏幕的像素點個數,例如:720*1280,指的是寬有720個像素點,高有1280個像素點。
- dpi:指的是每英寸像素,是由對角線上的像素點數除以屏幕大小所得。
系統屏幕密度
- ldpi文件夾下對應的密度為120dpi,對應的分辨率為240*320
- mdpi文件夾下對應的密度為160dpi,對應的分辨率為320*480
- hdpi文件夾下對應的密度為240dpi,對應的分辨率為480*800
- xhdpi文件夾下對應的密度為320dpi,對應的分辨率為720*1280
- xxhdpi文件夾下對應的密度為480dpi,對應的分辨率為1080*1920
由于各種屏幕密度的不同,導致了同一張圖片在不同的手機屏幕上顯示不同;在屏幕大小相同的情況下,高密度的屏幕包含了更多的像素點。android系統將密度為160dpi的屏幕作為標準對于mdpi文件夾,在此屏幕的手機上1dp=1px。從上面系統屏幕密度可以得出各個密度值之間的換算;在mdpi中1dp=1px,在hdpi中1dp=1.5px,在xhdpi中1dp=2px,在xxhpi中1dp=3px。換算比例如下:ldpi:mdpi:hdpi:xhdpi:xxhdpi=3:4:6:8:12。
單位換算方法
/**
* dp轉換成px
*/
private int dp2px(Context context,float dpValue){
float scale=context.getResources().getDisplayMetrics().density;
return (int)(dpValue*scale+0.5f);
}
/**
* px轉換成dp
*/
private int px2dp(Context context,float pxValue){
float scale=context.getResources().getDisplayMetrics().density;
return (int)(pxValue/scale+0.5f);
}
/**
* sp轉換成px
*/
private int sp2px(Context context,float spValue){
float fontScale=context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue*fontScale+0.5f);
}
/**
* px轉換成sp
*/
private int px2sp(Context context,float pxValue){
float fontScale=context.getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue/fontScale+0.5f);
}
利用系統TypeValue類來轉換
private int dp2px(Context context,int dpValue){
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpValue,context.getResources().getDisplayMetrics());
}
private int sp2px(Context context,int spValue){
return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,spValue,context.getResources().getDisplayMetrics());
}
補充:sp與dp的區別
下面我們進行一下實驗: textSize的單位分別設置為sp和dp,然后改變系統字體大小
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="200dp" android:layout_height="wrap_content" android:text="尚硅谷科技" android:background="#ff0000" android:textSize="20sp"/> <TextView android:id="@+id/textView2" android:layout_width="200px" android:layout_height="wrap_content" android:text="尚硅谷科技" android:background="#00ff00" android:textSize="20dp"/> </LinearLayout>
1、用sp做單位,設置有效果
2、dp做單位沒有效果
總結
原文鏈接:https://blog.csdn.net/qidingquan/article/details/53714603
相關推薦
- 2022-05-20 Python?文件處理之open()函數_python
- 2022-07-13 關于自定義監聽器 onApplicationEvent方法被執行多次的問題
- 2022-05-02 深入了解Python?中線程和進程區別_python
- 2024-03-03 layui 表格select下拉不顯示全的問題
- 2022-10-12 go實現限流功能示例_Golang
- 2022-04-21 C#實現chart控件動態曲線繪制_C#教程
- 2023-02-03 Python3.10?Generator生成器Coroutine原生協程詳解_python
- 2023-11-17 Linux CentOS如何修改root用戶密碼
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支