Skip to content

Instantly share code, notes, and snippets.

@jamesmundy
Created November 18, 2014 17:07
Show Gist options
  • Save jamesmundy/838b8e217c1134fd29c2 to your computer and use it in GitHub Desktop.
Save jamesmundy/838b8e217c1134fd29c2 to your computer and use it in GitHub Desktop.
An SVG image control for Xamarin.Android.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Com.Caverock.Androidsvg;
namespace Foundbite.GameAndroid.CustomViews
{
/// <summary>
/// Custom control for showing SVG images.
/// </summary>
public class ImageControl : RelativeLayout
{
private Context _context;
private string _source;
//Set source image of control.
public string Source
{
get
{
return _source;
}
set
{
_source = value;
Init();
}
}
public ImageControl(IntPtr handle, Android.Runtime.JniHandleOwnership transfer) : base (handle, transfer)
{
}
public ImageControl(Context context) : base(context)
{
_context = context;
}
public ImageControl(Context context, IAttributeSet attrs) : base(context, attrs)
{
_context = context;
var ta = context.ObtainStyledAttributes(attrs, Resource.Styleable.ImageControl);
try
{
Source = ta.GetString(Resource.Styleable.ImageControl_SVGSource);
}
finally
{
ta.Recycle();
}
}
public ImageControl(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
{
_context = context;
var ta = context.ObtainStyledAttributes(attrs, Resource.Styleable.ImageControl);
try
{
Source = ta.GetString(Resource.Styleable.ImageControl_SVGSource);
}
finally
{
ta.Recycle();
}
}
/// <summary>
/// Initialise control.
/// </summary>
public void Init()
{
//Clear view, in case another image was shown.
this.RemoveAllViews();
//Load SVG and add to view.
SVGImageView svgImageView = new SVGImageView(_context);
svgImageView.LayoutParameters = new ViewGroup.LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent);
svgImageView.SetImageAsset(Source);
this.AddView(svgImageView);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment