using System;
|
using Android.Content;
|
using Android.Graphics;
|
using Android.Views;
|
|
namespace Shared
|
{
|
|
/// <summary>
|
/// GradientMaskView
|
/// </summary>
|
public class GradientMaskView : View
|
{
|
/// <summary>
|
/// 当前视图
|
/// </summary>
|
/// <value>The android GradientMaskView.</value>
|
GradientButton mGradientButton
|
{
|
get
|
{
|
return AndroidView as GradientButton;
|
}
|
set
|
{
|
AndroidView = value;
|
}
|
}
|
|
/// <summary>
|
/// 构造函数
|
/// </summary>
|
public GradientMaskView()
|
{
|
mGradientButton = new GradientButton(Application.Activity, this);
|
SetGradientColors(mGradientColors);
|
|
}
|
|
public bool IsShow
|
{
|
get
|
{
|
return mGradientButton.IsShow;
|
|
}
|
set
|
{
|
mGradientButton.IsShow = value;
|
|
}
|
|
}
|
|
/// <summary>
|
/// 0%,25%,50%,80%
|
/// </summary>
|
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);
|
|
}
|
|
/// <summary>
|
/// 绘图的Paint
|
/// </summary>
|
private Paint mPaint;
|
|
protected override void OnDraw(Canvas canvas)
|
{
|
base.OnDraw(canvas);
|
|
if (isShow)
|
{
|
canvas.Save();
|
DrawWithGradient(canvas);
|
canvas.Restore();
|
}
|
}
|
|
/// <summary>
|
/// 绘制渐变效果
|
/// </summary>
|
/// <param name="canvas"></param>
|
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();
|
}
|
|
}
|
|
|
|
}
|
}
|
|
}
|