Created
January 13, 2023 03:29
-
-
Save EHG613/3aec87b8c34819cdd03a0d2a8b58b644 to your computer and use it in GitHub Desktop.
根据DisplayMetrics.DENSITY_MEDIUM判断系统是否开启大字体,且考虑老年人使用适当放大1.25倍字体
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* AdaptScreenUtils.kt, 2023-01-05 | |
* | |
* Copyright © 2015-2023 Liangyihui. All rights reserved. | |
* | |
*/ | |
package com.dop.h_doctor.util | |
import android.content.res.Resources | |
import android.util.DisplayMetrics | |
/** | |
* @author lijian | |
* @date 2023/1/5 | |
* @since 9.5 | |
* @description 禁止跟随系统字体,华为简易模式 | |
*/ | |
object AdaptScreenUtils { | |
@JvmStatic | |
fun reset(resources: Resources?): Resources? { | |
if (resources == null) return null | |
if (resources.displayMetrics.densityDpi / resources.displayMetrics.scaledDensity < DisplayMetrics.DENSITY_MEDIUM) { | |
/* | |
2.计算目标值density, scaleDensity, densityDpi | |
targetDensity为当前设备的宽度/设计稿固定的宽度 | |
targetScaleDensity:目标字体缩放Density,等比例测算 | |
targetDensityDpi:density = dpi / 160 即dpi = density * 160 | |
*/ | |
// val displayMetrics = HDoctorApplication.getInstance().resources.displayMetrics | |
val targetDensity = resources.displayMetrics.widthPixels / 375f | |
// val targetScaleDensity = targetDensity * (displayMetrics.scaledDensity / displayMetrics.density) | |
val targetDensityDpi = (targetDensity * 160).toInt() | |
val targetScaleDensity = targetDensity * 1.25f | |
//3.替换Activity的density, scaleDensity, densityDpi | |
// val dm = activity.resources.displayMetrics | |
resources.displayMetrics.density = targetDensity | |
resources.displayMetrics.scaledDensity = targetScaleDensity | |
resources.displayMetrics.densityDpi = targetDensityDpi | |
// val densityDpi = resources.displayMetrics.widthPixels / 360f | |
// resources.displayMetrics.densityDpi = (densityDpi * 160).toInt() | |
// resources.displayMetrics.scaledDensity = densityDpi * 1.25f | |
} | |
return resources | |
} | |
@JvmStatic | |
fun getTextZoom(): Int { | |
Resources.getSystem()?.displayMetrics?.let { | |
if (it.densityDpi / it.scaledDensity < DisplayMetrics.DENSITY_MEDIUM) { | |
return 110 | |
} | |
} | |
return 100 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment