依赖框架:https://gitee.com/wasdzy/BasicFramework/releases/2.3.0.1
java代码
package cn.mrlong.fontsizechangedemo;
import androidx.appcompat.app.AppCompatActivity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.TextView;
import cn.mrlong.basicframework.base.BaseAppCompatActivity;
import cn.mrlong.basicframework.cview.FontSizeView;
import cn.mrlong.basicframework.utils.DensityUtils;
import cn.mrlong.basicframework.utils.SharedPreferencesUtils;
import static cn.mrlong.basicframework.utils.StaticConstUtils.FONT_SIZE_FILE;
import static cn.mrlong.basicframework.utils.StaticConstUtils.FONT_SIZE_VALUE;
import static cn.mrlong.basicframework.utils.StaticConstUtils.FONT_SIZE_VALUE_SP;
public class MainActivity extends BaseAppCompatActivity {
private TextView textView;
private float defaultTextSize;
public int fontSzie;
private float ztSize = 1;
private int current;
@Override
public int setContentView() {
return R.layout.activity_main;
}
@Override
public void initView() {
FontSizeView fontSizeView = findViewById(R.id.fontSize);
textView = findViewById(R.id.testView);
defaultTextSize = textView.getTextSize();
//设置正常字体大小的倍数
current = SharedPreferencesUtils.getInt(this, FONT_SIZE_FILE, "FONT_SIZE_VALUE_SP_current", 1);
fontSizeView.setDefaultPosition((int) current);
fontSizeView.setChangeCallbackListener(new FontSizeView.OnChangeCallbackListener() {
@Override
public void onChangeListener(int position) {
if (current == position) return;
current = position;
fontSzie = position + 1;
if (fontSzie == 1) {//小号字体
ztSize = 0.85f;
} else if (fontSzie == 2) {//标准字体
ztSize = 1.0f;
} else if (fontSzie == 3) {//中号字体
ztSize = 1.3f;
} else if (fontSzie == 4) {//大号字体
ztSize = 1.5f;
} else if (fontSzie == 5) {//超大字体
ztSize = 1.7f;
} else if (fontSzie == 6) {//巨大字体
ztSize = 2f;
}
changeTextSize(ztSize);
SharedPreferencesUtils.putFloat(MainActivity.this, FONT_SIZE_FILE, FONT_SIZE_VALUE_SP, (float) ztSize);
SharedPreferencesUtils.putObject(MainActivity.this, FONT_SIZE_FILE, "FONT_SIZE_VALUE_SP_current", position);
}
});
}
@Override
public void initData() {
}
@Override
public void initListener() {
}
/**
* 改变textsize 大小
*/
private void changeTextSize(float dimension) {
textView.setTextSize(DensityUtils.px2sp(this, defaultTextSize) * dimension);
}
@Override
public void onClick(View v) {
}
}
xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_weight="1"
android:gravity="center|left"
android:id="@+id/testView"
android:text="安卓是一种基于Linux内核(不包含GNU组件)的自由及开放源代码的操作系统。主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。"
android:layout_width="match_parent"
android:layout_height="0dp" />
<cn.mrlong.basicframework.cview.FontSizeView
android:id="@+id/fontSize"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_height="200dp"
android:minHeight="120dp"
app:circleRadius="11dp"
app:totalCount="3"
app:lineColor="@color/colorAccent"
app:standerSize="14" />
</LinearLayout>
网友评论