Android使用SpannableStringBuilder在文本视图中显示图像-没有错误,但没有图像,只有纯文本
基本上我有这样一段话 导带较低能级之间的差异$ec 价带$ev的上能级称为能带 差距 现在,我正试图解析文本,获取那些$prefixed符号,并用数学公式的小PNG图像替换它们。它们只是单个/dbl字母常量符号,如Ev和Ec。它们在我的资产文件夹中。基本上这就是代码Android使用SpannableStringBuilder在文本视图中显示图像-没有错误,但没有图像,只有纯文本,android,image,textview,spannablestring,imagespan,Android,Image,Textview,Spannablestring,Imagespan,基本上我有这样一段话 导带较低能级之间的差异$ec 价带$ev的上能级称为能带 差距 现在,我正试图解析文本,获取那些$prefixed符号,并用数学公式的小PNG图像替换它们。它们只是单个/dbl字母常量符号,如Ev和Ec。它们在我的资产文件夹中。基本上这就是代码 public SpannableStringBuilder getConstants(String desc, String constpath){ SpannableStringBuilder builder = new
public SpannableStringBuilder getConstants(String desc, String constpath){
SpannableStringBuilder builder = new SpannableStringBuilder();
if (desc.contains("$")){
Matcher matcher = Pattern.compile("\\$\\w+").matcher(desc);
int lastEnding=0;
while (matcher.find()) {
String constName = matcher.group();
constName = constName.substring(1,constName.length());
int startIndex = matcher.start();
int endIndex = matcher.end();
String brokenDescFirstPart = desc.substring(lastEnding, startIndex - 1);
lastEnding = endIndex+1;
builder.append(brokenDescFirstPart).append(" ");
try{
InputStream imgStream = getContext().getAssets().open(constpath+constName+".png");
Drawable dconstImg = Drawable.createFromStream(imgStream, null);
imgStream.close();
builder.setSpan(new ImageSpan(dconstImg,ImageSpan.ALIGN_BOTTOM), builder.length() -3, builder.length() -1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(" ");
}catch (Exception e){
builder.append(" ");
}
}
String brokenDescLastPart = desc.substring(lastEnding, desc.length() - 1);
builder.append(brokenDescLastPart);
}else{
builder = SpannableStringBuilder.valueOf(desc);
}
return builder;
}
代码的快速总结是desc是我想要解析的字符串。然后,我使用regex获取$word模式,并使用matcher.match方法迭代文本。我使用一些int变量来跟踪这些符号之间的起点和终点,以仔细地重建嵌入图像的原始字符串。现在是代码
try{
InputStream imgStream = getContext().getAssets().open(constpath+constName+".png");
Drawable dconstImg = Drawable.createFromStream(imgStream, null);
imgStream.close();
builder.setSpan(new ImageSpan(dconstImg,ImageSpan.ALIGN_BOTTOM), builder.length() -3, builder.length() -1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(" ");
}catch (Exception e){
builder.append(" ");
}
这是我问题的关键。我是从关于这个问题的其他问题上得到的。他们说它有效。但我的车里没有。我在代码中设置了断点,并逐行执行。没有错误,什么都没有。还是不行
文本在textview中加载了额外的空格,我故意使用这些空格来标识它必须注入图像的位置。“我的文本视图”是Android Studio中的小文本视图小部件。当您可以使用SetComputendDrawbleSwithinInstructBounds时,为什么要启用文本?将段落拆分并附加一个唯一的ID。 下面的例子
InputStream imgStream = getContext().getAssets().open(constpath+constName+".png");
Drawable dconstImg = Drawable.createFromStream(imgStream, null);
if ( Build.VERSION.SDK_INT >= 21 )
{
text_you_want_to_add_drawable_to.setCompoundDrawablesWithIntrinsicBounds( dconstImg, null ), null, null, null );
} else
{
text_you_want_to_add_drawable_to.setCompoundDrawablesWithIntrinsicBounds( dconstImg, null, null, null );
}
我终于弄明白了这个问题。事实证明,所有可绘图项都必须使用setBounds()方法初始化。这是我的最终代码
InputStream imgStream = getContext().getAssets().open(constpath + constName + ".png");
Drawable dconstImg = Drawable.createFromStream(imgStream, null);
imgStream.close();
float aspectRatio = (float) (1.00 * dconstImg.getIntrinsicWidth() / dconstImg.getIntrinsicHeight());
dconstImg.setBounds(0, 0, (int) Math.ceil(descTextSize * aspectRatio), (int) Math.ceil(descTextSize));
int alignment = s ? ImageSpan.ALIGN_BOTTOM : ImageSpan.ALIGN_BASELINE;
builder.setSpan(new ImageSpan(dconstImg,alignment), builder.length() -1, builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(" ");
descTextSize是TextView.getTextSize()的结果
谢谢大家帮助我解决这个问题。试试下面的内容
ImageSpan is = new ImageSpan(context, R.id.icon);
text.setSpan(is, index, index + strLength, 0);
在此代码索引中=您的$start位置。
而index+strLength=你的$end位置。什么是
TextUtils#dumpspan
显示的?同样对于测试,只需尝试ImageSpan(Context-Context,int-resourceId)
,看看我使用该构造函数尝试了什么,它就可以工作了。但问题是我想从assets文件夹加载图像。我意识到我必须在可拉丝上打个电话,现在它可以工作了!(它是以默认大小0x0 px渲染图像)我只需要知道如何设置宽度和高度,使图像与文本完全一致,而不会突出。请参见getIntrinsicWidth
/getIntrinsicHeight
我理解“您想要添加的文本”指的是TextView小部件。从文档中可以看出,“SetComputendDrawablesWithinInstincBounds”用于设置文本视图四面的图像。但我想要的是内联注入小PNG。比如在键入whatsApp消息时使用表情符号/表情符号。使用Spanable似乎是唯一的方法。