我的代码:
for (int i = 0; i < selectedItems.size(); i++) { String na = selectedItems.get(i); TextVIEw tv = createContactTextVIEw(na); BitmapDrawable bd = (BitmapDrawable) convertVIEwToDrawable(tv); bd.setBounds(0,bd.getIntrinsicWIDth(),bd.getIntrinsicHeight()); sb.append(na + ","); sb.setSpan(new ImageSpan(bd),sb.length() - (na.length() + 1),sb.length() - 1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } txt.setText(sb);private Object convertVIEwToDrawable(TextVIEw vIEw) { int spec = MeasureSpec.makeMeasureSpec(0,MeasureSpec.UnspecIFIED); vIEw.measure(spec,spec); vIEw.layout(0,vIEw.getMeasureDWIDth(),vIEw.getMeasuredHeight()); Bitmap b = Bitmap.createBitmap(vIEw.getMeasureDWIDth(),vIEw.getMeasuredHeight(),Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); c.translate(-vIEw.getScrollX(),-vIEw.getScrollY()); vIEw.draw(c); vIEw.setDrawingCacheEnabled(true); Bitmap cacheBmp = vIEw.getDrawingCache(); Bitmap vIEwBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888,true); vIEw.destroyDrawingCache(); return new BitmapDrawable(vIEwBmp); } private TextVIEw createContactTextVIEw(String text) { TextVIEw tv = new TextVIEw(this); tv.setText(text); tv.setTextSize(25); tv.setBackgroundResource(R.drawable.oval_small); tv.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.close,0); return tv; }解决方法 ClickableSpan就是你想要的:
for (int i = 0; i < selectedItems.size(); i++) { String na = selectedItems.get(i); TextVIEw tv = createContactTextVIEw(na); BitmapDrawable bd = (BitmapDrawable) convertVIEwToDrawable(tv); bd.setBounds(0,bd.getIntrinsicHeight()); sb.append(na + ","); sb.setSpan(new ImageSpan(bd),sb.length() - (na.length() + 1),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); final int index = i; sb.setSpan(new ClickableSpan() { @OverrIDe public voID onClick(VIEw Widget) { // here add your code // delete your selectedItems[index] // recreate your SpannedString and set to txt } },sb.length() - (na.length() + 1),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } txt.setText(sb); txt.setMovementMethod(linkMovementMethod.getInstance()); // important
不要忘记最后一行
总结以上是内存溢出为你收集整理的在android中处理SpannableStringBuilder图像全部内容,希望文章能够帮你解决在android中处理SpannableStringBuilder图像所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)