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
using System;
using Android.Content;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.Util;
using Android.Views;
 
namespace Shared
{
    /// <summary>
    /// 色板
    /// </summary>
    public class ColorPicker : View
    {
        MyColorPickerView myColorPickerView {
            get {
                return AndroidView as MyColorPickerView;
            }
            set {
                AndroidView = value;
            }
        }
 
        /// <summary>
        /// Initializes a new instance of the <see cref="Shared.Button"/> class.
        /// </summary>
        public ColorPicker()
        {
            myColorPickerView = new MyColorPickerView(Application.Activity,this);
        }
 
 
        string colorImagePath;
        /// <summary>
        /// 色板图
        /// </summary>
        /// <value>The color image path.</value>
        public string ColorImagePath {
            get {
                return colorImagePath;
            }
            set {
                colorImagePath = value;
                Background (colorImagePath);
            }
        }
 
        /// <summary>
        /// 颜色变化
        /// </summary>
        public Action<View, byte[]> ColorChaged;
 
        public class MyColorPickerView : Android.Views.View
        {
            Bitmap bitmapTemp;
 
            View _view;
            public MyColorPickerView(Context context, View view)
                : base(context)
            {
                _view = view;
            }
 
            public override Android.Graphics.Drawables.Drawable Background
            {
                get
                {
                    return base.Background;
                }
                set
                {
                    base.Background = value;
 
                    bitmapTemp = drawableToBitmap(value);
                }
            }
 
            static Bitmap drawableToBitmap(Drawable drawable)
            {
                if (drawable == null)
                {
                    return null;
                }
                var bitmap = Bitmap.CreateBitmap(drawable.IntrinsicWidth,drawable.IntrinsicHeight,Bitmap.Config.Rgb565);
 
                Canvas canvas = new Canvas(bitmap);
                drawable.SetBounds(0, 0, drawable.IntrinsicWidth, drawable.IntrinsicHeight);
                drawable.Draw(canvas);
                return bitmap;
            }
            /// <summary>
            /// 重写点击事件
            /// </summary>
            /// <returns><c>true</c>, if touch event was oned, <c>false</c> otherwise.</returns>
            /// <param name="e">E.</param>
            public override bool OnTouchEvent(MotionEvent e)
            {
                if (_view != null && _view.Enable)
                {
                    _view.TouchEvent( e);
                }
                if (bitmapTemp != null && (_view as ColorPicker).ColorChaged != null)
                {
                    int x = Math.Abs((int)(e.GetX() * bitmapTemp.Width / Width));
                    int y = Math.Abs((int)(e.GetY() * bitmapTemp.Height / Height));
                    if (bitmapTemp.Width <= x) {
                        x = bitmapTemp.Width - 1;
                    }
 
                    if (bitmapTemp.Height <= y) {
                        y = bitmapTemp.Height - 1;
                    }
                    var color = bitmapTemp.GetPixel(x,y);
                    (_view as ColorPicker).ColorChaged(_view, new byte[] { (byte)((color >> 16) & 0xFF), (byte)((color >> 8) & 0xFF), (byte)(color & 0xFF) });
                    //Shared.HDLUtils.WriteLine("ColorChaged color:" + color);
                }
 
                //return base.OnTouchEvent(e);
                return true;//2019-09-09 改为  return true
            }
        }
    }
}