gpt4 book ai didi

android - ImageSpan未显示图像

转载 作者:行者123 更新时间:2023-12-02 13:39:55 25 4
gpt4 key购买 nike

我想在自定义TextView的段落末尾放置一个小的动画图像,但是当我使用以下代码时,它不会显示该图像:

cmptDiag.setSpan(
ImageSpan(context, R.drawable.diagpause_anim_gif, DynamicDrawableSpan.ALIGN_BASELINE),
cmptDiag.length-2,
cmptDiag.length-1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
我怀疑“上下文”会有更好的选择,但是找不到合适的选择。 getContext()不起作用,我无法输入“this”或“this @ theCustomTextView”。

最佳答案

检查这个例子。

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val text = ("Lorem Ipsum is simply dummy text of the printing and" +
" typesetting industry. Lorem Ipsum has been the industry's" +
" standard dummy text ever since the 1500s, when an unknown" +
" printer took a galley of type and scrambled it" +
" to make a type specimen book.").toSpannable()

// Get icon from drawable resource
var icon:Bitmap = BitmapFactory.decodeResource(resources,R.drawable.cfsuman)

// Scale bitmap using android kotlin core ktx function
icon = icon.scale(100,100,false)

// Image span from drawable icon
text[49..50] = ImageSpan(this,R.drawable.ic_weekend)

// Another image span from bitmap
text[89..90] = ImageSpan(this,icon)

textView.text = text
}
}
此示例将以文本形式显示图像。
如果要添加边界,请选中此选项。
Drawable image = ContextCompat.getDrawable(mContext, android.R.drawable.presence_offline);
image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
// Replace blank spaces with image icon
String myText = "myText";
int textLength = myText.length();
SpannableString sb = new SpannableString(myText + " " + "This is another text");
ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
sb.setSpan(imageSpan, textLength, textLength + 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
这个由Java编写的“setBound”示例。但我认为您可以改用Kotlin

关于android - ImageSpan未显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64568970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com