using System;
using Android.Content;
using Android.Graphics;
using Android.Views;
namespace Shared
{
///
/// GradientMaskView
///
public class GradientMaskView : View
{
///
/// 当前视图
///
/// The android GradientMaskView.
GradientButton mGradientButton
{
get
{
return AndroidView as GradientButton;
}
set
{
AndroidView = value;
}
}
///
/// 构造函数
///
public GradientMaskView()
{
mGradientButton = new GradientButton(Application.Activity, this);
SetGradientColors(mGradientColors);
}
public bool IsShow
{
get
{
return mGradientButton.IsShow;
}
set
{
mGradientButton.IsShow = value;
}
}
///
/// 0%,25%,50%,80%
///
uint[] mGradientColors = { 0x00000000, 0x40000000, 0x80000000, 0xCC000000 };
public uint[] GradientColors
{
get
{
return mGradientColors;
}
set
{
mGradientColors = value;
SetGradientColors(mGradientColors);
}
}
void SetGradientColors(uint[] colors)
{
if (colors == null || colors.Length == 0) return;
int[] mCGColors = new int[colors.Length];
for (int i = 0; i < colors.Length; i++)
{
mCGColors[i] = HDLUtils.GetUIColorWithUint(colors[i]);
}
mGradientButton.GradientColors = mCGColors;
}
public class GradientButton : Android.Views.View
{
View view;
public GradientButton(Context context, View view)
: base(context)
{
this.view = view;
initPaint();
}
public override bool OnTouchEvent(MotionEvent e)
{
var v = base.OnTouchEvent(e);
view?.TouchEvent(e);
return true;
}
// 初始化进度圆弧画笔
private void initPaint()
{
mPaint = new Paint(); //初始化进度圆弧画笔
mPaint.AntiAlias = true;
mPaint.SetStyle(Paint.Style.Fill);
}
///
/// 绘图的Paint
///
private Paint mPaint;
protected override void OnDraw(Canvas canvas)
{
base.OnDraw(canvas);
if (isShow)
{
canvas.Save();
DrawWithGradient(canvas);
canvas.Restore();
}
}
///
/// 绘制渐变效果
///
///
void DrawWithGradient(Canvas canvas)
{
var gradient = new LinearGradient(0, 0, 0, Height, mGradientColors, null, Shader.TileMode.Mirror);
mPaint.SetShader(gradient);
canvas.DrawRect(0, 0, Width, Height, mPaint);
}
public int[] mGradientColors = { Android.Graphics.Color.Transparent };
public int[] GradientColors
{
get
{
return mGradientColors;
}
set
{
mGradientColors = value;
PostInvalidate();
}
}
bool isShow = true;
public bool IsShow
{
get
{
return isShow;
}
set
{
isShow = value;
PostInvalidate();
}
}
}
}
}