feature: Android renderers
This commit is contained in:
70
Pixiview.Android/Renderers/RoundLabelRenderer.cs
Normal file
70
Pixiview.Android/Renderers/RoundLabelRenderer.cs
Normal file
@ -0,0 +1,70 @@
|
||||
using Android.Content;
|
||||
using Android.Graphics;
|
||||
using Android.Graphics.Drawables;
|
||||
using Pixiview.Droid.Renderers;
|
||||
using Pixiview.UI;
|
||||
using Xamarin.Forms;
|
||||
using Xamarin.Forms.Platform.Android;
|
||||
|
||||
[assembly: ExportRenderer(typeof(RoundLabel), typeof(RoundLabelRenderer))]
|
||||
namespace Pixiview.Droid.Renderers
|
||||
{
|
||||
public class RoundLabelRenderer : Xamarin.Forms.Platform.Android.FastRenderers.LabelRenderer
|
||||
{
|
||||
public RoundLabelRenderer(Context context) : base(context)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
|
||||
{
|
||||
base.OnElementChanged(e);
|
||||
|
||||
if (e.NewElement is RoundLabel label)
|
||||
{
|
||||
var density = Resources.DisplayMetrics.Density;
|
||||
var drawable = new RoundCornerDrawable(label.CornerRadius * density);
|
||||
drawable.SetColorFilter(label.BackgroundColor.ToAndroid(), PorterDuff.Mode.Src);
|
||||
Control.SetBackground(drawable);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class RoundCornerDrawable : Drawable
|
||||
{
|
||||
private readonly Paint paint;
|
||||
private readonly float radius;
|
||||
private RectF rect;
|
||||
|
||||
public override int Opacity => (int)Format.Translucent;
|
||||
|
||||
public RoundCornerDrawable(float radius)
|
||||
{
|
||||
paint = new Paint
|
||||
{
|
||||
AntiAlias = true
|
||||
};
|
||||
this.radius = radius;
|
||||
}
|
||||
|
||||
public override void SetBounds(int left, int top, int right, int bottom)
|
||||
{
|
||||
base.SetBounds(left, top, right, bottom);
|
||||
rect = new RectF(left, top, right, bottom);
|
||||
}
|
||||
|
||||
public override void Draw(Canvas canvas)
|
||||
{
|
||||
canvas.DrawRoundRect(rect, radius, radius, paint);
|
||||
}
|
||||
|
||||
public override void SetAlpha(int alpha)
|
||||
{
|
||||
paint.Alpha = alpha;
|
||||
}
|
||||
|
||||
public override void SetColorFilter(ColorFilter colorFilter)
|
||||
{
|
||||
paint.SetColorFilter(colorFilter);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user