Skip to content

Instantly share code, notes, and snippets.

@didikee
Last active January 18, 2021 09:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save didikee/c846adf08c2be3c5a69e633536a60e12 to your computer and use it in GitHub Desktop.
Save didikee/c846adf08c2be3c5a69e633536a60e12 to your computer and use it in GitHub Desktop.
Android_Oreo_AutoSizeTextView_Demo
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context="com.didikee.idea.demo.ScaleTextActivity">
<TextView
android:id="@+id/tv_scale"
android:layout_width="100dp"
android:layout_height="20dp"
android:layout_gravity="center_horizontal"
android:gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:text="width:"
/>
<SeekBar
android:id="@+id/sb_width"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="200"
android:paddingBottom="16dp"
android:paddingTop="8dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:text="height:"
/>
<SeekBar
android:id="@+id/sb_height"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:paddingBottom="16dp"
android:paddingTop="8dp"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="autosize_text_sizes">
<item>12</item>
<item>18</item>
<item>20</item>
<item>32</item>
<item>42</item>
</array>
</resources>
package com.didikee.idea.demo;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import com.didikee.idea.demo.utils.DisplayUtil;
public class ScaleTextActivity extends AppCompatActivity {
private TextView tv_scale;
private SeekBar sb_width;
private SeekBar sb_height;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scale_text);
tv_scale = findViewById(R.id.tv_scale);
sb_width = findViewById(R.id.sb_width);
sb_height = findViewById(R.id.sb_height);
init();
}
private void init() {
initScaleConfig();
String text = "Hello Android!";
tv_scale.setText(text);
final int defaultWidth = 100;
final int defaultHeight = 20;
/**
* height min 20 (+ 0~100)
* width min 100 (+ 0~200)
*/
sb_width.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
ViewGroup.LayoutParams layoutParams = tv_scale.getLayoutParams();
layoutParams.width = DisplayUtil.dp2px(ScaleTextActivity.this, progress + defaultWidth);
tv_scale.setLayoutParams(layoutParams);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
sb_height.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
ViewGroup.LayoutParams layoutParams = tv_scale.getLayoutParams();
layoutParams.height = DisplayUtil.dp2px(ScaleTextActivity.this, defaultHeight + progress);
tv_scale.setLayoutParams(layoutParams);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
private void initScaleConfig() {
//TODO config scale params for normal textView
/**
* android:autoSizeTextType="uniform"
* the type of auto-size. Must be one of
* {@link TextViewCompat#AUTO_SIZE_TEXT_TYPE_NONE} or
* {@link TextViewCompat#AUTO_SIZE_TEXT_TYPE_UNIFORM}
* none: 关闭缩放功能
* uniform: 垂直方向与水平方向缩放
*/
TextViewCompat.setAutoSizeTextTypeWithDefaults(tv_scale, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
/**
* 参数一: 被设置的TextView
* 参数二: 自动缩放的最小字号
* 参数三: 自动缩放的最大字号
* 参数四: 参数二与参数三所用的单位,这里因为字体大小,所以我指定为SP
*/
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(tv_scale,
12, 48, 2, TypedValue.COMPLEX_UNIT_SP);
/**
* 参数一:
* 参数二: 设置多个预制字体大小,这样在缩放时字体会根据预制的字体大小而缩放
* 参数三: 参数二里面的Int值对应的单位,这里因为字体大小,所以我指定为SP
*/
int[] autoTextSize = getResources().getIntArray(R.array.autosize_text_sizes);
TextViewCompat.setAutoSizeTextTypeUniformWithPresetSizes(tv_scale, autoTextSize, TypedValue.COMPLEX_UNIT_SP);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment