How to display an ImageSpan from Vector in Xamarin.Android
-
Im trying to create a spannable text that contains an ImageSpan, but the vector drawables has infinite size so we have to scale it some how, i have try this code with no luck, in some forums they say it should work:
var icon = AppCompatResources.GetDrawable(Activity, Resource.Drawable.ic_info);
icon.SetBounds(0, 0, 20, 20);
DrawableCompat.SetTint(icon, Color.White);
var textView = layout.FindViewById(Resource.Id.FeaturesContentTextView);
var imageSpan = new ImageSpan(icon, SpanAlign.Baseline); //Find your drawable.var spannableString = new SpannableString(textView.Text); //Set text of SpannableString from TextView spannableString.SetSpan(imageSpan, textView.Text.Length - 1, textView.Text.Length, SpanTypes.InclusiveInclusive); textView.TextFormatted = spannableString;
Now this code display the icon with the maximum width and height allowed from the parent layout I also try to convert the vector drawable to bitmap with this code with no luck (i dont get any icon)
public static Bitmap GetBitmapFromVectorDrawable(Context context, int drawableId)
{
Drawable drawable = AppCompatResources.GetDrawable(context, drawableId);Bitmap bitmap = Bitmap.CreateBitmap(drawable.IntrinsicWidth, drawable.IntrinsicHeight, Bitmap.Config.Argb8888); Canvas canvas = new Canvas(bitmap); drawable.SetBounds(0, 0, canvas.Width, canvas.Height); drawable.Draw(canvas); return bitmap; }
Bitmap bitmap = GetBitmapFromVectorDrawable(Activity, Resource.Drawable.ic_info);
Drawable d = new BitmapDrawable(Resources, Bitmap.CreateScaledBitmap(bitmap, 80, 80, true));