前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android 原生 ImageSpan 垂直局中及左右边距问题解决

Android 原生 ImageSpan 垂直局中及左右边距问题解决

作者头像
萬物並作吾以觀復
发布2021-05-06 15:31:01
1.5K0
发布2021-05-06 15:31:01
举报
文章被收录于专栏:指尖下的Android指尖下的Android

关于Android 原生 ImageSpan 无法设置垂直局中及左右边距问题,找了很多博客,均无效或者有 bug,只能自己动手了,代码经测试有效。

代码语言:javascript
复制
package com.example.myapplication;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.style.ImageSpan;
import androidx.annotation.NonNull;

public class VerticalImageSpan extends ImageSpan {

    private final int marginLeft;
    private final int marginRight;

    public VerticalImageSpan(Drawable drawable) {
        this(drawable, 0, 0);
    }

    public VerticalImageSpan(@NonNull Drawable drawable, int marginLeft, int marginRight) {
        super(drawable);
        this.marginLeft = marginLeft;
        this.marginRight = marginRight;
    }

    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end,
                       Paint.FontMetricsInt fontMetricsInt) {
        Drawable drawable = getDrawable();
        Rect rect = drawable.getBounds();
        if (null != fontMetricsInt) {
            Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
            int fontHeight = fmPaint.descent - fmPaint.ascent;
            int drHeight = rect.bottom - rect.top;
            int centerY = fmPaint.ascent + fontHeight / 2;
            fontMetricsInt.ascent = centerY - drHeight / 2;
            fontMetricsInt.top = fontMetricsInt.ascent;
            fontMetricsInt.bottom = centerY + drHeight / 2;
            fontMetricsInt.descent = fontMetricsInt.bottom;
        }
        return marginLeft + rect.right + marginRight;
    }

    @Override
    public void draw(Canvas canvas, CharSequence text, int start, int end,
                     float x, int top, int y, int bottom, Paint paint) {
        Drawable drawable = getDrawable();
        canvas.save();
        Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
        x = marginLeft + x;
        int fontHeight = fmPaint.descent - fmPaint.ascent;
        int centerY = y + fmPaint.descent - fontHeight / 2;
        int transY = centerY - (drawable.getBounds().bottom - drawable.getBounds().top) / 2;
        canvas.translate(x, transY);
        drawable.draw(canvas);
        canvas.restore();
    }

}
代码语言:javascript
复制
package com.example.myapplication;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.util.AttributeSet;
import androidx.appcompat.widget.AppCompatTextView;

public class SimpleTextView extends AppCompatTextView {

    private static final String TAG = "*";
    private int margin;

    public SimpleTextView(Context context) {
        super(context);
    }

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

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

    private void init(Context context, AttributeSet attrs){
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SimpleTextView);
        margin = DpUtils.dip2px(context, 4);
        setTextContent(typedArray.getString(R.styleable.SimpleTextView_setText));
        typedArray.recycle();
    }

    public void setTextContent(Editable editable){
        if(!TextUtils.isEmpty(editable)){
            setTextContent(editable.toString());
        }
    }

    public void setTextContent(String content){
        if(!TextUtils.isEmpty(content)){
            SpannableString spannableString = new SpannableString(content + TAG);
            Drawable drawable = getResources().getDrawable(R.mipmap.icon_tips);
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            VerticalImageSpan imageSpan = new VerticalImageSpan(drawable, margin, 0);
            spannableString.setSpan(imageSpan, content.length(), content.length() + TAG.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            setText(spannableString);
        }
    }



}
代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="SimpleTextView">
        <attr name="setText" format="string" />
    </declare-styleable>
</resources>
代码语言:javascript
复制
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;

import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private AppCompatEditText editText;
    private SimpleTextView simpleTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = findViewById(R.id.editInput);
        simpleTextView = findViewById(R.id.textView);
        findViewById(R.id.btnTest).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btnTest){
            onBtnTestClick();
        }
    }

    private void onBtnTestClick() {
        simpleTextView.setTextContent(editText.getText());
    }

}
代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

   <androidx.appcompat.widget.AppCompatEditText
       android:id="@+id/editInput"
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       app:layout_constraintLeft_toLeftOf="parent"
       app:layout_constraintRight_toRightOf="parent"
       app:layout_constraintTop_toTopOf="parent" />
    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/btnTest"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/editInput"
        android:text="测试按钮"/>

    <com.example.myapplication.SimpleTextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:setText="Hello Worldsdfdsfsdfdsfdsdsfdsfsddsfdsdsfsdfdssdff!"
        android:textSize="16sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/btnTest" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/btnTest01"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView"
        android:text="按钮01"/>
    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/btnTest02"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/btnTest01"
        android:text="按钮02"/>
    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/btnTest03"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/btnTest02"
        android:text="按钮03"/>
    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/btnTest04"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/btnTest03"
        android:text="按钮04"/>

</androidx.constraintlayout.widget.ConstraintLayout>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
腾讯云服务器利旧
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档