如何将文本放入可绘制对象中?

问题描述 投票:0回答:6

我正在尝试动态创建一个可绘制对象,以用作自定义线性布局的背景。它需要有哈希标记等(没什么大不了的),但也有数字标记哈希标记是什么(就像一把尺子)。我知道我可以创建文本元素并将它们放入线性布局中,然后将哈希标记放入可绘制对象中,但我希望也将它们放入可绘制对象中,这样我就不必进行两次测量计算。

android drawable
6个回答
104
投票

这是

TextDrawable
的一个简短示例,它的工作方式与普通可绘制对象类似,但允许您将文本指定为唯一的构造函数变量:

public class TextDrawable extends Drawable {

    private final String text;
    private final Paint paint;

    public TextDrawable(String text) {

        this.text = text;

        this.paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setTextSize(22f);
        paint.setAntiAlias(true);
        paint.setFakeBoldText(true);
        paint.setShadowLayer(6f, 0, 0, Color.BLACK);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextAlign(Paint.Align.LEFT);
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.drawText(text, 0, 0, paint);
    }

    @Override
    public void setAlpha(int alpha) {
        paint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        paint.setColorFilter(cf);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }
}

16
投票

我读过《Professional Android 2 Application Development》一书(Reto Meier 着)。其中,它包含一个示例项目,您可以在其中创建一个简单的指南针应用程序,在其中“绘制”文本、标记等。

简单的解释是,您创建一个扩展

android.view.View
类并重写
onDraw(Canvas)
方法的类。

本书的所有源代码都可以在这里下载:http://www.wrox.com/WileyCDA/WroxTitle/Professional-Android-2-Application-Development.productCd-0470565527,descCd-DOWNLOAD.html。如果您下载代码并查看名为“Chapter 4 Compass”的项目,我相信您会找到您要找的东西:)


12
投票

查看 Plowman 的答案并尝试根据我的需要进行调整,我偶然发现了一个用于此链接中相机的类

这是来自 TextDrawable Class 的代码。看起来与 Plowmans 非常相似,但对我来说效果更好:

import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;

public class TextDrawable extends Drawable {
    private static final int DEFAULT_COLOR = Color.WHITE;
    private static final int DEFAULT_TEXTSIZE = 15;
    private Paint mPaint;
    private CharSequence mText;
    private int mIntrinsicWidth;
    private int mIntrinsicHeight;

    public TextDrawable(Resources res, CharSequence text) {
        mText = text;
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(DEFAULT_COLOR);
        mPaint.setTextAlign(Align.CENTER);
        float textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                DEFAULT_TEXTSIZE, res.getDisplayMetrics());
        mPaint.setTextSize(textSize);
        mIntrinsicWidth = (int) (mPaint.measureText(mText, 0, mText.length()) + .5);
        mIntrinsicHeight = mPaint.getFontMetricsInt(null);
    }
    @Override
    public void draw(Canvas canvas) {
        Rect bounds = getBounds();
        canvas.drawText(mText, 0, mText.length(),
                bounds.centerX(), bounds.centerY(), mPaint);
    }
    @Override
    public int getOpacity() {
        return mPaint.getAlpha();
    }
    @Override
    public int getIntrinsicWidth() {
        return mIntrinsicWidth;
    }
    @Override
    public int getIntrinsicHeight() {
        return mIntrinsicHeight;
    }
    @Override
    public void setAlpha(int alpha) {
        mPaint.setAlpha(alpha);
    }
    @Override
    public void setColorFilter(ColorFilter filter) {
        mPaint.setColorFilter(filter);
    }
}

7
投票

回答上面有关如何使文本居中的评论:

mPaint.textAlign = Align.CENTER
...
// Centering for mixed case letters
canvas.drawText(mText, 0, mText.length,
        bounds.centerX().toFloat(), bounds.centerY().toFloat() - ((mPaint.descent() + mPaint.ascent()) / 2), mPaint)

// Centering for all uppercase letters
canvas.drawText(mText, 0, mText.length,
            bounds.centerX().toFloat(), bounds.centerY().toFloat() - mPaint.ascent() / 2, mPaint)

4
投票

这允许您将任何视图放入可绘制对象中,包括文本视图。您甚至可以在 XML 布局中使用样式。

public class ViewDrawable extends Drawable {
    final View mView;

    public ViewDrawable(final Context context, final @LayoutRes int layoutId) {
        this(LayoutInflater.from(context).inflate(layoutId, null));
    }

    public ViewDrawable(final @NonNull View view) {
        mView = view;
    }

    public View getView() {
        return mView;
    }

    @Override
    public void setBounds(int left, int top, int right, int bottom) {
        super.setBounds(left, top, right, bottom);
        final int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(right - left, View.MeasureSpec.EXACTLY);
        final int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(bottom - top, View.MeasureSpec.EXACTLY);
        mView.measure(widthMeasureSpec, heightMeasureSpec);
        mView.layout(left, top, right, bottom);
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        mView.draw(canvas);
    }

    @Override
    public void setAlpha(int alpha) {
        mView.setAlpha(alpha/255f);
    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {

    }

    @Override
    public int getOpacity() {
        return PixelFormat.UNKNOWN;
    }
}

0
投票

这是基于 Plowman 答案的改进解决方案,但修复了布局测量不正确的问题。现在也已经在 Kotlin 中了。

class TextDrawable(text: String, color: Int, size: Float) : Drawable() {
    private val paint = TextPaint().also {
        it.color = color
        it.textSize = size
        it.isAntiAlias = true
        it.isFakeBoldText = true
        it.setShadowLayer(6f, 0f, 0f, Color.BLACK)
        it.style = Paint.Style.FILL
         it.textAlign = Paint.Align.LEFT
    }

    private val textWidth = Rect().let {
        paint.getTextBounds(text, 0, when (text.contains("\n")) {
            true -> text.indexOf("\n")
            false -> text.length
        }, it)

        it.right
    }

    private var staticLayout = when {
        Build.VERSION.SDK_INT < Build.VERSION_CODES.M -> {
            StaticLayout(text, 0, text.length, paint, textWidth,
                Layout.Alignment.ALIGN_NORMAL, StaticLayout.DEFAULT_LINESPACING_MULTIPLIER, StaticLayout.DEFAULT_LINESPACING_ADDITION,
                true
            )
        }
        else -> {
            StaticLayout.Builder
                .obtain(text, 0, text.length, paint, textWidth)
                .build()
        }
    }

    override fun draw(canvas: Canvas) {
         staticLayout.draw(canvas)
    }

    override fun getIntrinsicWidth(): Int {
        return staticLayout.width
    }

    override fun getIntrinsicHeight(): Int {
        return staticLayout.height
    }

    override fun setAlpha(alpha: Int) {
        paint.alpha = alpha
    }

    override fun setColorFilter(cf: ColorFilter?) {
        paint.setColorFilter(cf)
    }

    @Deprecated("Deprecated in Java",
        ReplaceWith("PixelFormat.TRANSLUCENT", "android.graphics.PixelFormat")
    )
    override fun getOpacity(): Int {
        return PixelFormat.TRANSLUCENT
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.