gpt4 book ai didi

android: 像 gmail 一样的 MultiAutoCompleteTextView 风格

转载 作者:太空狗 更新时间:2023-10-29 14:01:55 24 4
gpt4 key购买 nike

我想创建自定义的 MultiAutoCompleteTextView,比如 gmail 应用程序: enter image description here

我创建自定义 View 并扩展到 MultiAutoCompleteTextView 并使用 Span ,但我有问题,如果没有足够的空间容纳文本和图像,然后 span 有(空间)然后 View 拆分它们

enter image description here

还有我的自定义 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;
}


}

所以有人有建议吗?

最佳答案

检查这两个库:

他们所做的几乎就是您想要完成的事情。

至少第一个库背后的想法 - 一旦 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);

一旦图像+名称只是一个位图 - 它就不能再被包裹,您的问题就解决了。

关于android: 像 gmail 一样的 MultiAutoCompleteTextView 风格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35014640/

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