Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to display an ImageSpan from Vector in Xamarin.Android

How to display an ImageSpan from Vector in Xamarin.Android

Scheduled Pinned Locked Moved C#
graphicsandroidmobiletutoriallearning
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Exoskeletor
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups