wxr
2020-06-15 b8e94316e41eba72d927d5ca7d931b26139ee8ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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();
                }
 
            }
 
          
 
        }
    }
 
}