本文整理汇总了Java中android.text.style.ImageSpan类的典型用法代码示例。如果您正苦于以下问题:Java ImageSpan类的具体用法?Java ImageSpan怎么用?Java ImageSpan使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ImageSpan类属于android.text.style包,在下文中一共展示了ImageSpan类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: replace
import android.text.style.ImageSpan; //导入依赖的package包/类
boolean replace(ImageSpan by) {
int start = builder.getSpanStart(span);
int end = builder.getSpanEnd(span);
if (start == -1)
return true;
builder.removeSpan(span);
builder.setSpan(
by,
start,
end,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE
);
target.setText(builder);
return true;
}
示例2: setText
import android.text.style.ImageSpan; //导入依赖的package包/类
/** 显示文本和表情 */
public static void setText(TextView textView, String text) {
Context context = textView.getContext();
Resources resources = context.getResources();
SpannableString ss = new SpannableString(text);
// 正则表达式: [高兴]
Pattern p = Pattern.compile("\\[([A-Za-z\u4E00-\u9FA5]+)\\]");
Matcher matcher = p.matcher(ss);
while (matcher.find()) {
// 匹配到一个表情字符串
String emoji = matcher.group();
// 过滤非表情符,比如: [xxx]
if (EMOJI_DATAS.containsKey(emoji)) { // 是表情才处理
// System.out.println("----------" + emoji);
// 指定了一张图片
Bitmap bitmap = BitmapFactory.decodeResource(resources, EMOJI_DATAS.get(emoji));
bitmap = Global.createBitmap(bitmap, Global.dp2px(20)); // 图片的宽高为20dp
ImageSpan span = new ImageSpan(context, bitmap, ImageSpan.ALIGN_BOTTOM);
int start = matcher.start();
int end = matcher.end();
ss.setSpan(span, start, end, 0);
}
}
textView.setText(ss);
}
示例3: handleTag
import android.text.style.ImageSpan; //导入依赖的package包/类
@Override
public void handleTag(boolean opening, String tag, Editable output,
XMLReader xmlReader) {
// TODO Auto-generated method stub
// �����ǩ<img>
if (tag.toLowerCase().equals("img")) {
// ��ȡ����
int len = output.length();
// ��ȡͼƬ��ַ
ImageSpan[] images = output.getSpans(len-1, len, ImageSpan.class);
String imgURL = images[0].getSource();
// ʹͼƬ�ɵ������������¼�
output.setSpan(new ImageClick(context, imgURL), len-1, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
示例4: emoticonDisplay
import android.text.style.ImageSpan; //导入依赖的package包/类
public static void emoticonDisplay(Context context, Spannable spannable, String emoticonName, int fontSize, int start, int end) {
Drawable drawable = getDrawableFromAssets(context, emoticonName);
if (drawable != null) {
int itemHeight;
int itemWidth;
if (fontSize == WRAP_DRAWABLE) {
itemHeight = drawable.getIntrinsicHeight();
itemWidth = drawable.getIntrinsicWidth();
} else {
itemHeight = fontSize;
itemWidth = fontSize;
}
drawable.setBounds(0, 0, itemHeight, itemWidth);
ImageSpan imageSpan = new ImageSpan(drawable);
spannable.setSpan(imageSpan, start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
}
}
示例5: createAndPutChipForUser
import android.text.style.ImageSpan; //导入依赖的package包/类
private ChipSpan createAndPutChipForUser(TLRPC.User user) {
LayoutInflater lf = (LayoutInflater) ApplicationLoader.applicationContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View textView = lf.inflate(R.layout.group_create_bubble, null);
TextView text = (TextView)textView.findViewById(R.id.bubble_text_view);
String name = UserObject.getUserName(user);
if (name.length() == 0 && user.phone != null && user.phone.length() != 0) {
name = PhoneFormat.getInstance().format("+" + user.phone);
}
text.setText(name + ", ");
int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
textView.measure(spec, spec);
textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap(textView.getWidth(), textView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(b);
canvas.translate(-textView.getScrollX(), -textView.getScrollY());
textView.draw(canvas);
textView.setDrawingCacheEnabled(true);
Bitmap cacheBmp = textView.getDrawingCache();
Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
textView.destroyDrawingCache();
final BitmapDrawable bmpDrawable = new BitmapDrawable(b);
bmpDrawable.setBounds(0, 0, b.getWidth(), b.getHeight());
SpannableStringBuilder ssb = new SpannableStringBuilder("");
ChipSpan span = new ChipSpan(bmpDrawable, ImageSpan.ALIGN_BASELINE);
allSpans.add(span);
selectedContacts.put(user.id, span);
for (ImageSpan sp : allSpans) {
ssb.append("<<");
ssb.setSpan(sp, ssb.length() - 2, ssb.length(), SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
}
userSelectEditText.setText(ssb);
userSelectEditText.setSelection(ssb.length());
return span;
}
示例6: getTextWithBackground
import android.text.style.ImageSpan; //导入依赖的package包/类
public static ImageSpan getTextWithBackground(Drawable background, final int textSize
, final int padding, final int color){
return new ImageSpan(background){
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
paint.setTextSize(textSize + padding);
int len = Math.round(paint.measureText(text, start, end));
getDrawable().setBounds(0, 0, len, (int) (1.5f * getTextMaxHeight(paint,textSize)));
Drawable b = getDrawable();
canvas.save();
int transY = ((bottom-top) - b.getBounds().bottom)/2+top;
canvas.translate(x , transY);
b.draw(canvas);
canvas.restore();
paint.setColor(color);
paint.setTextSize(textSize);
canvas.drawText(text.subSequence(start, end).toString(), x+ padding ,y, paint);
}
};
}
示例7: replace
import android.text.style.ImageSpan; //导入依赖的package包/类
private CharSequence replace(CharSequence text) {
try {
SpannableStringBuilder builder = new SpannableStringBuilder(text);
Pattern pattern = buildPattern();
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
if (BaseApplication.mEmoticonsId.containsKey(matcher.group())) {
int id = BaseApplication.mEmoticonsId.get(matcher.group());
Bitmap bitmap = BitmapFactory.decodeResource(
getResources(), id);
if (bitmap != null) {
ImageSpan span = new ImageSpan(getContext(), bitmap);
builder.setSpan(span, matcher.start(), matcher.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
return builder;
} catch (Exception e) {
return text;
}
}
示例8: handleTagNode
import android.text.style.ImageSpan; //导入依赖的package包/类
@Override
public void handleTagNode(TagNode node, SpannableStringBuilder builder,
int start, int end, SpanStack stack) {
String src = node.getAttributeByName("src");
builder.append("\uFFFC");
Bitmap bitmap = loadBitmap(src);
if (bitmap != null) {
Drawable drawable = new BitmapDrawable(bitmap);
drawable.setBounds(0, 0, bitmap.getWidth() - 1,
bitmap.getHeight() - 1);
stack.pushSpan( new ImageSpan(drawable), start, builder.length() );
}
}
示例9: getSpannable
import android.text.style.ImageSpan; //导入依赖的package包/类
@NonNull
private SpannableStringBuilder getSpannable() {
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(getResources().getString(R.string.content_cn));
Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
drawable.setBounds(0, 0, 35, 35);
spannableStringBuilder.setSpan(new ImageSpan(drawable)
, 36, 37, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableStringBuilder.setSpan(new ImageSpan(drawable)
, 37, 38, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableStringBuilder.setSpan(new ImageSpan(drawable)
, 38, 39, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableStringBuilder.setSpan(new ImageSpan(drawable)
, 39, 40, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
// ItalicReplacementSpan italicSpan = new ItalicReplacementSpan(-0.25f);
// StrokeSpan strokeSpan = new StrokeSpan(Color.BLUE, Color.YELLOW, 20);
// spannableStringBuilder.setSpan(strokeSpan, 0, spannableStringBuilder.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
return spannableStringBuilder;
}
示例10: parseEmoji
import android.text.style.ImageSpan; //导入依赖的package包/类
public static SpannableStringBuilder parseEmoji(Context context,String content){
int size = UIUtils.dip2Px(25,context);
//里[smiley_00]ff[smiley_78]
SpannableStringBuilder builder = new SpannableStringBuilder(content);
//正则表达式
Pattern pattern = Pattern.compile("\\[smiley_(.*?)\\]");
Matcher matcher = pattern.matcher(content);
while(matcher.find()){
String emo = matcher.group();//[smiley_00]
emo = emo.substring(1, emo.length()-1);
//获取表情的id
int id = context.getResources().getIdentifier(emo, "drawable", context.getPackageName());
if(id != 0 ){
Drawable drawable = context.getResources().getDrawable(id);
// 必须设置图片大小
drawable.setBounds(0, 0, size, size);
// 包装一下图片
ImageSpan span = new ImageSpan(drawable);
// 把文字替换成图片
builder.setSpan(span, matcher.start(), matcher.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
return builder;
}
示例11: render
import android.text.style.ImageSpan; //导入依赖的package包/类
public void render(final TextView textView, final Context context, String line)
{
final Handler handler = new Handler(Looper.getMainLooper());
final SpannableString text = new SpannableString(" " + line + "\n");
handler.post(new Runnable()
{
@Override
public void run()
{
//set the image tag behind (left of) the text
text.setSpan(new ImageSpan(context, IMAGE_TAG), 0, 1, 0);
textView.append(text);
}
});
}
示例12: getEmotionText
import android.text.style.ImageSpan; //导入依赖的package包/类
public static SpannableString getEmotionText(Context context, String source, int
emotionSizeDp) {
SpannableString spannableString = new SpannableString(source);
Matcher matcher = Pattern.compile(REGEX_GROUP).matcher(spannableString);
if (matcher.find()) {
matcher.reset();
}
while (matcher.find()) {
String emojiStr = matcher.group(1);
if (emojiStr != null) {
ImageSpan imageSpan = getImageSpan(context, emojiStr, emotionSizeDp);
if (imageSpan != null) {
int start = matcher.start(1);
spannableString.setSpan(imageSpan, start, emojiStr.length() + start, 33);
}
}
}
return spannableString;
}
示例13: startImg
import android.text.style.ImageSpan; //导入依赖的package包/类
private void startImg(Editable text, Attributes attributes, HtmlCompat.ImageGetter img) {
String src = attributes.getValue("", "src");
Drawable d = null;
if (img != null) {
d = img.getDrawable(src, attributes);
}
if (d == null) {
Resources res = mContext.getResources();
d = res.getDrawable(R.drawable.unknown_image);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
}
int len = text.length();
text.append("\uFFFC");
text.setSpan(new ImageSpan(d, src), len, text.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
示例14: getFace
import android.text.style.ImageSpan; //导入依赖的package包/类
public SpannableStringBuilder getFace(Context mContext, String content) {
SpannableStringBuilder sb = new SpannableStringBuilder();
try {
/**
* 经过测试,虽然这里tempText被替换为png显示,但是但我单击发送按钮时,获取到輸入框的内容是tempText的值而不是png
* 所以这里对这个tempText值做特殊处理
* 格式:[face/png/f_static_000.png],以方便判斷當前圖片是哪一個
* */
String tempText = "[" + content + "]";
sb.append(tempText);
sb.setSpan(
new ImageSpan(mContext, BitmapFactory.decodeStream(mContext.getAssets().open(content))),
sb.length() - tempText.length(),
sb.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} catch (Exception e) {
e.printStackTrace();
}
return sb;
}
示例15: bindContentView
import android.text.style.ImageSpan; //导入依赖的package包/类
@Override
protected void bindContentView() {
layoutDirection();
TextView bodyTextView = findViewById(R.id.nim_message_item_text_body);
bodyTextView.setTextColor(isReceivedMessage() ? Color.BLACK : Color.WHITE);
bodyTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClick();
}
});
MoonUtil.identifyFaceExpression(NimUIKit.getContext(), bodyTextView, getDisplayText(), ImageSpan.ALIGN_BOTTOM);
bodyTextView.setMovementMethod(LinkMovementMethod.getInstance());
bodyTextView.setOnLongClickListener(longClickListener);
}