Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
- }
Add Comment
Please, Sign In to add comment