android - 如何根据 android displayMetrics 缩放字体大小?
问题描述
我正在编写一个应用程序来在 Android 中显示一个简单的数字时钟,我需要缩放 TextView 的字体大小,以便它尽可能地填满屏幕。我找到了一种使用 displayMetrics 缩放文本大小的非常简单的方法,但它在某些设备上无法正常工作。我想要达到的效果是这样的:
我只会在横向模式下工作,我希望它可以在我周围的旧智能手机和平板电脑以及较新的设备上运行。所以我有各种各样的屏幕类型来迎合。
要缩放字体,我执行以下操作
...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_clock);
// Get screen size and calculate text size needed to fit
// TODO: doesn't work well on some devices
TextView tvScreenSize = findViewById(R.id.tvScreenSize);
DisplayMetrics displayMetrics = new DisplayMetrics(); // From: https://stackoverflow.com/questions/4743116/get-screen-width-and-height
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
// The screen height and width are recalculated automatically by Android when the screen is redrawn on a rotation
height = displayMetrics.heightPixels;
width = displayMetrics.widthPixels;
textSP = width / 12; // Empirical value
String hString = Integer.toString(height);
String wString = Integer.toString(width);
tvScreenSize.setText("h: " + hString + " w:" + wString);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Display time of day
Date curDate = new Date();
TextView tvTime = findViewById(R.id.clockView);
String timeStr = format.format(curDate);
tvTime.setText(timeStr);
tvTime.setScaleY(1.5f);
tvTime.setWidth(width * 8 / 10); // Make it fill 80% of the screen
tvTime.setHeight(height * 8 / 10);
tvTime.setTextSize(textSP);
...
quid 是这样的:
textSP = width / 12; // Empirical value
对于某些屏幕,值 6 效果很好。对于其他屏幕,这会创建一个大到只有一小部分可见的字体。将此值更改为 12 可以解决问题。
似乎我错过了与屏幕密度有关的东西,但是什么?
谢谢!
最终结果:对于像我这样尝试缩放文本的人,这是从 ismail 输入后的最终代码:
activity_main.xml 文件:
?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvClockFace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:text="12:34"
android:textColor="@android:color/holo_red_dark"
android:textSize="30sp"
android:textStyle="bold"
android:typeface="monospace"
app:fontFamily="monospace"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvScreenSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="H:... W:..."
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
和 MainActivity.java:
package com.schalkx.testautosizeclockdisplay;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.Display;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Scale the textsize automatically
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics ();
display.getMetrics(outMetrics);
float density = getResources().getDisplayMetrics().density;
float dpHeight = outMetrics.heightPixels / density;
float dpWidth = outMetrics.widthPixels / density;
// Convert height and width to integer values
float fTextSize = dpWidth/4; // Trial and error
// int iTextSize = Math.round(fTextSize);
int scrHeight = Math.round(dpHeight);
int scrWidth = Math.round(dpWidth);
// Display de height and width on screen (for reference)
String wString = Integer.toString(scrWidth); // In case you need the width
String hString = Integer.toString(scrHeight); // Same for height
TextView tvScreenSize = findViewById(R.id.tvScreenSize);
tvScreenSize.setText("h: " + hString + " w:" + wString);
// Scale the font of the clock face
TextView tvClockFace = findViewById(R.id.tvClockFace);
// tvClockFace.setHeight(50);
// tvClockFace.setWidth(100);
tvClockFace.setTextSize(fTextSize);
tvClockFace.setScaleY(1.5f);
}
}
解决方案
根本不要在代码中使用像素值。将像素转换为与设备无关的像素(dp)
public static float convertPixelsToDp(float px, Context context){
return px / ((float) context.getResources().getDisplayMetrics().densityDpi/
DisplayMetrics.DENSITY_DEFAULT);
}
推荐阅读
- python - MultipleChoiceField 中的 Django 选项列表
- google-cloud-firestore - Firestore的Spark Subscription是否允许计算集合大小等云功能?当前函数已上传但已调用
- c - 从 C# 代码访问在 C 代码中分配的结构内存
- oracle - 将RDS实例从EC2-Classic迁移到VPC存疑
- requirejs - 尝试通过 RequireJS 使用 CanvasJS 时出错
- c# - Bot Framework v4 加载 Facebook Webview
- c# - 如何使用 Firefox Headless 下载文件?
- php - 有没有更好的方法在 php 中为 ajax 返回 .json 数组?
- c - GCC:函数参数中的静态数组索引不会触发任何警告
- git - git 添加 . 导致功能未实现错误