android在文本框中输入和显示表情

public void inputFace(EditText contentEdt, int position, Context context) {

int index = contentEdt.getSelectionStart();

int drawId = ToolUtils.faceId[position];

String string = ToolUtils.stringId[position];

contentEdt.getText().insert(index, string);

SpannableString spannableString = new SpannableString(contentEdt.getText());

Drawable d = context.getResources().getDrawable(drawId);

//设置图片大小    

d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());

//插入的位置    

spannableString.setSpan(new ImageSpan(d, ImageSpan.ALIGN_BOTTOM), index, index + string.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

// spannableString.setSpan(new ImageSpan(context, drawId), index, index + string.length(),

// Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

contentEdt.setText(spannableString, TextView.BufferType.SPANNABLE);

contentEdt.setSelection(index + string.length());

contentEdt.setOnClickListener(new OnClickListener() {


@Override

public void onClick(View v) {

if (rootView != null) {

rootView.setVisibility(View.GONE);

}

}

});

}

public void showFace(TextView text, String content, ArrayList<String> indexText, String colorContent) {

if (StringUtils.isNullOrEmpty(content)) {

return;

}

if (null != indexText && !indexText.isEmpty()) {

String[] temp = null;

int start = 0;

int end = 0;

SpannableString spanText = new SpannableString(colorContent);

// LogUtils.d(TAG, "showFace: " + colorContent + "  " + Arrays.asList(indexText));

for (String string : indexText) {

temp = string.split("#");

start = Integer.valueOf(temp[0]);

end = Integer.valueOf(temp[1]);

spanText.setSpan(new ForegroundColorSpan(Color.parseColor("#0178b0")), start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

}

text.append(spanText);

}

if (content.contains("/")) {

// LogUtils.d(TAG, "showFace() contain/");

Pattern pattern = null;

pattern = Pattern.compile("/[a-z]{2}");

Matcher matcher = pattern.matcher(content);

int size = ToolUtils.dip2px(text.getContext(), 20);

Integer drawableSrc = null;

SpannableString spannableString = new SpannableString(content);

while (matcher.find()) {

// LogUtils.d(TAG, "showFace() finded " + matcher.group());

int start = matcher.start();

int end = matcher.end();

drawableSrc = ToolUtils.getFaceId(matcher.group());

// LogUtils.d(TAG, "showFace() drawableSrc " + drawableSrc);

if (drawableSrc > 0) {

 //以下代码是在指定位置插入图片   

final Drawable d = text.getContext().getResources().getDrawable(drawableSrc);   

//设置图片大小    

d.setBounds(0, 0, size, size);

spannableString.setSpan(getDynamicSpan(d), start, end,

Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

}

}

text.append(spannableString);

} else {

text.append(content);

}

}

private DynamicDrawableSpan getDynamicSpan(final Drawable d) {

DynamicDrawableSpan dynamicDraw = new DynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BOTTOM) {


@Override

public Drawable getDrawable() {

return d;

}

@Override

   public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {

       Drawable d = getDrawable();

       Rect rect = d.getBounds();


       if (fm != null) {

           fm.ascent = -rect.bottom; 

           fm.descent = 0; 


           fm.top = fm.ascent;

           fm.bottom = 0;

       }


       return rect.right;

   }


   /*

    * Copy-paste of super.draw(...) but use getDrawable() to get the image/frame to draw, in stead of

    * the cached drawable.

    */

   @Override

   public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {

       Drawable b = getDrawable();

       canvas.save();


       int transY = bottom - b.getBounds().bottom;

       if (mVerticalAlignment == ALIGN_BASELINE) {

           transY -= paint.getFontMetricsInt().descent;

       }


       canvas.translate(x, transY);

       b.draw(canvas);

       canvas.restore();


   }

};

return dynamicDraw;

}


你可能感兴趣的:(android在文本框中输入和显示表情)