Advanced Android TextView

https://github.com/chiuki/advanced-textview
今日通过一个开源库来分析textview的一些其他玩法。。
这里写图片描述

  1. getCompoundDrawables方法可以获取这4个位置的图片。然后就可以在文字周围加drawable动画和动画集合等等(animated-rotate和animation-list)。假如要用animated-vector还要另外新建v21(因为5.0之后才支持vector)。。
  2. textview添加阴影
    android:shadowColor="@color/shadow"
    android:shadowDx="12"
    android:shadowDy="12"
    android:shadowRadius="8"

    可以修改阴影颜色,半径,xy的偏移量。。可以做出发光效果

  3. 为textview添加字体库等,常见有ttf,otf格式
    Typeface typeface = Typeface.createFromAsset(getAssets(), “Ruthie.ttf”);

 @Override
  public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
    //this would apply to all textviews in the app
    if (name.equals("TextView")) {
      TextView view = new TextView(this, attrs);
      view.setTypeface(typeface);
      return view;
    }

    return super.onCreateView(parent, name, context, attrs);
  }

  @Override
  public View onCreateView(String name, Context context, AttributeSet attrs) {
    //this would apply to all textviews in the app
    if (name.equals("TextView")) {
      TextView view = new TextView(this, attrs);
      view.setTypeface(typeface);
      return view;
    }

    return super.onCreateView(name, context, attrs);
  }

在name为textview情况下改变textview的常规字体属性。。
4. textview的加空格& # 160;符号,表示一个空格 下面是ISO Latin-1字符集对照表
https://jingyan.baidu.com/article/84b4f565e0148360f6da323e.html

5.LinearGradient添加颜色渐变

TextView textView = (TextView) findViewById(R.id.text);
Shader shader = new LinearGradient(0, 0, 0, textView.getTextSize(), Color.RED, Color.BLUE,
        Shader.TileMode.CLAMP);
textView.getPaint().setShader(shader);

6.为文字添加图案BitmapShader


Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.cheetah_tile);
Shader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.REPEAT);
textView.getPaint().setShader(shader);

7.为文字添加html注入,点击访问隐藏连接

String html = getString(R.string.from_html_text);
textView.setText(Html.fromHtml(html, new ResouroceImageGetter(this), null));
textView.setMovementMethod(LinkMovementMethod.getInstance());
private static class ResouroceImageGetter implements Html.ImageGetter {
    private final Context context;

    public ResouroceImageGetter(Context context) {
      this.context = context;
    }

    @Override
    public Drawable getDrawable(String source) {
      int path = context.getResources().getIdentifier(source, "drawable",
          BuildConfig.APPLICATION_ID);
      Drawable drawable = context.getResources().getDrawable(path);
      drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
          drawable.getIntrinsicHeight());
      return drawable;
    }
  }

主要是string 的xml同通配符的使用

<string name="from_html_text">
    <![CDATA[
    <h1>Hello World</h1>
    Here is an <img src="octopus"><i>octopus</i>.<br>
    And here is a <a href="http://developer.android.com/reference/android/text/Html.html">link</a>.
    ]]>
  </string>

  1. spannableString.setSpan来设置文字的style
    new TextAppearanceSpan(context, style)
 private static SpannableString formatString(Context context, int textId, int style) {
    String text = context.getString(textId);
    SpannableString spannableString = new SpannableString(text);
    spannableString.setSpan(new TextAppearanceSpan(context, style), 0, text.length(), 0);
    return spannableString;
  }

9.动态为文字左对齐和右对齐
其实到底还是各种span的使用https://blog.csdn.net/qq_16430735/article/details/50427978

 ## Layout.Alignment.ALIGN_OPPOSITE : Layout.Alignment.ALIGN_NORMAL
 设置左右的属性
AlignmentSpan span = new AlignmentSpan.Standard(align);
SpannableString spannableString = new SpannableString(text);
spannableString.setSpan(span, 0, text.length(), 0);

10.为textview设置彩虹渐变
主要继承CharacterStyle 和实现UpdateAppearance 。线性渐变加个matrix矩阵变成更见彩虹文字

 private static class RainbowSpan extends CharacterStyle implements UpdateAppearance {
    private final int[] colors;

    public RainbowSpan(Context context) {
      colors = context.getResources().getIntArray(R.array.rainbow);
    }

    @Override
    public void updateDrawState(TextPaint paint) {
      paint.setStyle(Paint.Style.FILL);
      Shader shader = new LinearGradient(0, 0, 0, paint.getTextSize() * colors.length, colors, null,
          Shader.TileMode.MIRROR);
      Matrix matrix = new Matrix();
      matrix.setRotate(90);
      shader.setLocalMatrix(matrix);
      paint.setShader(shader);
    }
  }

11.为textview动态彩虹渐变
To make an animated rainbow span, translate the Shader with a Matrix, and change the translation
value with an ObjectAnimator.
用 ObjectAnimator不停的改变一个值来改变矩阵Matrix,颜色渲染器Shader ,再设置这个颜色给textview的paint画笔

if (shader == null) {
        shader = new LinearGradient(0, 0, 0, width, colors, null,
            Shader.TileMode.MIRROR);
      }
      matrix.reset();
      matrix.setRotate(90);
      matrix.postTranslate(width * translateXPercentage, 0);
      shader.setLocalMatrix(matrix);
      paint.setShader(shader);

12.继承ClickableSpan,点击自定义连接

SpannableString spannableString = new SpannableString(text);
spannableString.setSpan(new GoToSettingsSpan(), start, end, 0);
textView.setText(spannableString);
textView.setMovementMethod(new LinkMovementMethod());

private static class GoToSettingsSpan extends ClickableSpan {
    @Override
    public void onClick(View view) {
      view.getContext().startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
    }
  }

13.为edictext添加下划线
主要测量edictext的padding,piant的getFontMetrics().top的留白,和字体粗度的一半。最后获取字体的高度getLineHeight

public class LinedEditText extends EditText {
  private Paint paint = new Paint();

  public LinedEditText(Context context) {
    super(context);
    init();
  }

  public LinedEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  public LinedEditText(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
  }

  private void init() {
    paint = new Paint();
    paint.setStyle(Paint.Style.STROKE);
    paint.setColor(getResources().getColor(R.color.paper_line));
    paint.setStrokeWidth(getLineHeight() / 10);
    paint.setStrokeCap(Paint.Cap.ROUND);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    float startX = getPaddingLeft();
    float stopX = getWidth() - getPaddingRight();
    float offsetY = getPaddingTop() - getPaint().getFontMetrics().top + paint.getStrokeWidth() / 2;

    for (int i = 0; i < getLineCount(); ++i) {
      float y = offsetY + getLineHeight() * i;
      canvas.drawLine(startX, y, stopX, y, paint);
    }

    super.onDraw(canvas);
  }
}

14.textview的unicode编码转化为emoji

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值