Android:如何像Gmail一样设置MultiAutoCompleteTextView的样式

3
我想创建类似Gmail应用的自定义MultiAutoCompleteTextView: enter image description here 我创建了自定义视图并扩展到MultiAutoCompleteTextView并使用Span,但我有问题,如果没有足够的空间放置文本和图像,则会分割视图。

enter image description here

这是我的自定义视图代码:

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;
}


}

那么有人有建议吗?

1个回答

1

请查看以下两个库:

它们基本上实现了您要完成的功能。

至少第一个库背后的想法是,一旦发生onTextChanged事件,您就可以使用带有文本的TextView创建一个Bitmap,并设置CompoundDrawablesWithIntrinsicBounds(在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);
一旦图像和名称合成为一个位图,它就不能再被包裹,您的问题就解决了。

@Kaonstantin 感谢您的建议,我以前看过这些库,但是它们对我来说不太适合。 - Mehrdad Faraji
@MehrdadFaraji 我并不是要使用它们,我只是建议使用相同的原则:在画布上绘制您的图像+文本,然后将其转换为位图,并用作ImageSpan。很容易! - Konstantin Loginov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接