简体   繁体   English

是否可以通过一次调用Canvas.drawText()来显示多色文本?

[英]Is it possible to display multi-color text with one call to Canvas.drawText()?

I would like to use Canvas.drawText() to display multi-color text. 我想使用Canvas.drawText()来显示多色文本。 More specifically, I want to highlight a substring of the text passed to the drawText() method. 更具体地说,我想突出显示传递给drawText()方法的文本的子字符串。

The text is in the form of a SpannableString with 0 or more ForegroundColorSpan objects. 文本采用具有0个或更多ForegroundColorSpan对象的SpannableString形式。

Looking at the Canvas code, it appears that a .toString() call on the passed CharSequence, means that this is not possible. 看看Canvas代码,看来对传递的CharSequence进行.toString()调用意味着这是不可能的。

Is there an alternative way? 还有另一种方法吗?

EDIT: The text may occasionally change (total changes, not incremental). 编辑:文本可能偶尔会更改(总更改,而不是增量)。 Also, there are potentially multiple texts positioned in different unrelated locations in the custom view. 此外,在自定义视图中可能存在多个位于不同不相关位置的文本。

Yes it is possible by using one of the Layout classes. 是的,可以使用其中一个布局类。 These are helper classes for drawing text to a canvas and they support Spannables. 这些是用于将文本绘制到画布的辅助类,它们支持Spannable。 If your text doesn't change use a StaticLayout. 如果您的文本没有更改,请使用StaticLayout。

Example

Add this to your custom view class 将其添加到您的自定义视图类

private StaticLayout layout;

put this code into your onLayout or onSizeChanged 将此代码放入onLayoutonSizeChanged

Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");  

wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

TextPaint paint = new TextPaint();
paint.setTextSize(20f);
paint.setColor(Color.RED);
layout = new StaticLayout(wordtoSpan, paint, getWidth(), Alignment.ALIGN_NORMAL, 1, 0, false);

Then in your drawing method simply call 然后在你的绘图方法中简单地调用

layout.draw(canvas);

In case your text changes often you can use a DynamicLayout . 如果您的文本经常更改,您可以使用DynamicLayout

Editable.Factory fac = Editable.Factory.getInstance();
Editable edit = fac.newEditable(wordtoSpan);
DynamicLayout layout = new DynamicLayout(edit,paint,getWidth(),Alignment.ALIGN_CENTER,1,0,false);

change text by using the edit object 使用编辑对象更改文本

edit.append("hello");

i hvn't used in with Canvas. 我没有使用Canvas。 see below code how i used it in textview. 看下面的代码我是如何在textview中使用它的。

public TextView getTextClipArt1(){
    TextView textView = new TextView(context);
    Typeface tf = new MyTypeface(context, 0).getTypeface();

    Shader textShader=new LinearGradient(0, 0, 0, 30,
            new int[]{Color.GREEN,Color.BLUE},
            new float[]{0, 1}, TileMode.CLAMP);

    textView.setTypeface(tf);
    textView.getPaint().setShader(textShader);
    textView.getPaint().setStyle(Paint.Style.STROKE);
    textView.getPaint().setStrokeWidth(2);
    textView.setText("ABC");
    textView.setTextSize(30);
    textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    return textView;
}

you can now draw textview as bitmap on canvas, Although i think these methods are also exist in paint class. 你现在可以在canvas上绘制textview作为位图,虽然我认为这些方法也存在于paint类中。 Hope useful to you. 希望对你有用。

Try something like this, if you use TextView 如果您使用TextView,请尝试这样的操作

String multiColorText = "<font color=0xff0000>Multi</font><font color=0x000000>Color</font><font color=0xccffff>Text</font>";

textView.setText(Html.fromHtml(multiColorText));

Edit : For SpannableString, check if the below helps you 编辑:对于SpannableString,请检查以下内容是否对您有所帮助

Spannable WordtoSpan = new SpannableString("partial colored text"); 

WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 2, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Whenever you write that text for that view you can set thatView.setBackgroundResource(R.drawable.multicolor); 无论何时为该视图编写该文本,都可以设置thatView.setBackgroundResource(R.drawable.multicolor); and

In multicolor.xml write multicolor.xml中

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle"> 
    <gradient 
            android:startColor="@color/tabBgStart"
            android:endColor="@color/tabBgEnd"
            android:angle="270"/> 
</shape> 

Hope it will works definitely 希望它肯定会有效

To Change the text color you can use yourView.setTextColor(R.drawable.multicolor); 要更改文本颜色,可以使用yourView.setTextColor(R.drawable.multicolor);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM