问题描述
我想创建自定义MultiAutoCompleteTextView,例如gmail应用程序:
I want create custom MultiAutoCompleteTextView like gmail app:
我创建了自定义视图,并扩展到MultiAutoCompleteTextView并使用Span,但是我有问题,如果文本和图像的空间不足,则span拥有(空格),然后视图将它们分割开
I create custom view and extend to MultiAutoCompleteTextView and use Span , but I have problem, if there is not enough room for text and image then and span have (space) then view split them
还有我的自定义视图代码:
and there is my code for custom view:
public class CustomMultiAutoCompleteTextView extends MultiAutoCompleteTextView {
@Override
public void setTokenizer(Tokenizer t) {
super.setTokenizer(t);
}
public CustomMultiAutoCompleteTextView(Context context) {
super(context);
}
public CustomMultiAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomMultiAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void replaceText(CharSequence text) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.avatar);
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append("g");
builder.setSpan(new ImageSpan(getContext(), getRoundedBitmap(bitmap)),
builder.length() - 1, builder.length(), 0);
builder.append(text);
builder.setSpan(new BackgroundColorSpan(Color.GRAY), 1, builder.length(), 0);
super.replaceText(builder);
}
public static Bitmap getRoundedBitmap(Bitmap bitmap) {
final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(output);
final int color = Color.RED;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawOval(rectF, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
bitmap.recycle();
return output;
}
}
所以有人建议吗?
推荐答案
检查以下两个库:
- kpbird/chips-edittext-library
- splitwise/TokenAutoComplete
他们正在做您想完成的事情.
They are doing pretty much what you try to accomplish.
至少第一个库背后的想法-发生onTextChanged
后,您会在TextView
中创建一个带有文本并设置CompoundDrawablesWithIntrinsicBounds
的Bitmap
(淹没在Canvas
上).
The idea behind at least first library - once onTextChanged
happens, you do create a Bitmap
out of TextView
with text and setting CompoundDrawablesWithIntrinsicBounds
(drown on the Canvas
).
TextView textView = (TextView) lf.inflate(R.layout.chips_edittext, null);
textView.setText(c); // set text
int image = ((ChipsAdapter) getAdapter()).getImage(c);
textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, image, 0);
// capture bitmapt of genreated textview
int spec = MeasureSpec.makeMeasureSpec(0, 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(); // destory drawable
// create bitmap drawable for imagespan
BitmapDrawable bmpDrawable = new BitmapDrawable(viewBmp);
bmpDrawable.setBounds(0, 0,bmpDrawable.getIntrinsicWidth(),bmpDrawable.getIntrinsicHeight());
// create and set imagespan
ssb.setSpan(new ImageSpan(bmpDrawable),x ,x + c.length() , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
一旦图像+名称仅是一个位图-就不再可以将其包装,并且您的问题已得到解决.
Once the image+name is just one bitmap - it can't be no longer wrapped and your issue is solved.
这篇关于android:类似gmail的MultiAutoCompleteTextView样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!