Guest User

Untitled

a guest
Nov 22nd, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. package com.didikee.idea.demo;
  2.  
  3. import android.support.v4.widget.TextViewCompat;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.util.TypedValue;
  7. import android.view.ViewGroup;
  8. import android.widget.SeekBar;
  9. import android.widget.TextView;
  10.  
  11. import com.didikee.idea.demo.utils.DisplayUtil;
  12.  
  13. public class ScaleTextActivity extends AppCompatActivity {
  14.  
  15. private TextView tv_scale;
  16. private SeekBar sb_width;
  17. private SeekBar sb_height;
  18.  
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_scale_text);
  23.  
  24. tv_scale = findViewById(R.id.tv_scale);
  25. sb_width = findViewById(R.id.sb_width);
  26. sb_height = findViewById(R.id.sb_height);
  27. init();
  28. }
  29.  
  30. private void init() {
  31. initScaleConfig();
  32. String text = "Hello Android!";
  33. tv_scale.setText(text);
  34. final int defaultWidth = 100;
  35. final int defaultHeight = 20;
  36. /**
  37. * height min 20 (+ 0~100)
  38. * width min 100 (+ 0~200)
  39. */
  40. sb_width.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  41. @Override
  42. public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  43. ViewGroup.LayoutParams layoutParams = tv_scale.getLayoutParams();
  44. layoutParams.width = DisplayUtil.dp2px(ScaleTextActivity.this, progress + defaultWidth);
  45. tv_scale.setLayoutParams(layoutParams);
  46. }
  47.  
  48. @Override
  49. public void onStartTrackingTouch(SeekBar seekBar) {
  50.  
  51. }
  52.  
  53. @Override
  54. public void onStopTrackingTouch(SeekBar seekBar) {
  55.  
  56. }
  57. });
  58.  
  59. sb_height.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  60. @Override
  61. public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  62. ViewGroup.LayoutParams layoutParams = tv_scale.getLayoutParams();
  63. layoutParams.height = DisplayUtil.dp2px(ScaleTextActivity.this, defaultHeight + progress);
  64. tv_scale.setLayoutParams(layoutParams);
  65. }
  66.  
  67. @Override
  68. public void onStartTrackingTouch(SeekBar seekBar) {
  69.  
  70. }
  71.  
  72. @Override
  73. public void onStopTrackingTouch(SeekBar seekBar) {
  74.  
  75. }
  76. });
  77. }
  78.  
  79. private void initScaleConfig() {
  80. //TODO config scale params for normal textView
  81. /**
  82. * android:autoSizeTextType="uniform"
  83. * the type of auto-size. Must be one of
  84. * {@link TextViewCompat#AUTO_SIZE_TEXT_TYPE_NONE} or
  85. * {@link TextViewCompat#AUTO_SIZE_TEXT_TYPE_UNIFORM}
  86. * none: 关闭缩放功能
  87. * uniform: 垂直方向与水平方向缩放
  88. */
  89. TextViewCompat.setAutoSizeTextTypeWithDefaults(tv_scale, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
  90.  
  91. /**
  92. * 参数一: 被设置的TextView
  93. * 参数二: 自动缩放的最小字号
  94. * 参数三: 自动缩放的最大字号
  95. * 参数四: 参数二与参数三所用的单位,这里因为字体大小,所以我指定为SP
  96. */
  97. TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(tv_scale,
  98. 12, 48, 2, TypedValue.COMPLEX_UNIT_SP);
  99.  
  100. /**
  101. * 参数一:
  102. * 参数二: 设置多个预制字体大小,这样在缩放时字体会根据预制的字体大小而缩放
  103. * 参数三: 参数二里面的Int值对应的单位,这里因为字体大小,所以我指定为SP
  104. */
  105. int[] autoTextSize = getResources().getIntArray(R.array.autosize_text_sizes);
  106. TextViewCompat.setAutoSizeTextTypeUniformWithPresetSizes(tv_scale, autoTextSize, TypedValue.COMPLEX_UNIT_SP);
  107. }
  108. }
Add Comment
Please, Sign In to add comment