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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
using System;
using Android.Content;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.OS;
using Android.Util;
using Android.Views;
 
namespace Shared
{
    //已经全面检查了代码
    /// <summary>
    /// Button 按键
    /// </summary>
    public class ImageView : View
    {
        /// <summary>
        ///  当前视图
        /// </summary>
        /// <value>The android button.</value>
        AndroidImageView currentButton
        {
            get
            {
                return AndroidView as AndroidImageView;
            }
            set
            {
                AndroidView = value;
            }
        }
 
        /// <summary>
        /// 构造函数
        /// </summary>
        public ImageView()
        {
            currentButton = new AndroidImageView(Application.Activity, this);
        }
 
        internal string imagePath, tempImagePath;
        /// <summary>
        /// 非选中状态的背景图路径
        /// </summary>
        /// <value>The un selected image path.</value>
        public string ImagePath
        {
            get
            {
                return imagePath;
            }
            set
            {
                tempImagePath = value;
                if (!IsCanRefresh)
                {
                    return;
                }
                if (imageBytes != null)
                {
                    (AndroidView as AndroidImageView).SetImageDrawable(Bytes2Drawable(imageBytes));
                    return;
                }
                if (ImagePath == value)
                {
                    return;
                }
                imagePath = value;
                Background(value);
            }
        }
 
        byte[] imageBytes;
        public byte[] ImageBytes
        {
            get
            {
                return imageBytes;
            }
            set
            {
                imageBytes = value;
                ImagePath = tempImagePath;
            }
        }
 
        public override uint Radius
        {
            get
            {
                return base.Radius;
            }
            set
            {
                base.Radius = value;
                if (!IsCanRefresh && Radius == value)
                {
                    return;
                }
                currentButton.Invalidate();
            }
        }
 
        public override void Refresh()
        {
            base.Refresh();
            ImagePath = tempImagePath;
        }
    }
    public class AndroidImageView : Android.Widget.ImageView
    {
        public override bool OnTouchEvent(MotionEvent e)
        {
            base.OnTouchEvent(e);
            if (view != null)
            {
                view.TouchEvent(e);
            }
            return true;
        }
        /**
        * 图片的类型,圆形or圆角
        */
        private int type = TYPE_ROUND;
        public static int TYPE_CIRCLE = 0;
        public static int TYPE_ROUND = 1;
        /**
         * 圆角大小的默认值
         */
        private static int BODER_RADIUS_DEFAULT = 10;
 
        /**
         * 绘图的Paint
         */
        private Paint mBitmapPaint;
        /**
         * 圆角的半径
         */
        private int mRadius;
        /**
         * 3x3 矩阵,主要用于缩小放大
         */
        private Matrix mMatrix;
        /**
         * 渲染图像,使用图像为绘制图形着色
         */
        private BitmapShader mBitmapShader;
        /**
         * view的宽度
         */
        private int mWidth;
        private RectF mRoundRect;
 
        View view;
        public AndroidImageView(Context context, View view)
                : base(context)
        {
            this.view = view;
            mMatrix = new Matrix();
            mBitmapPaint = new Paint();
            mBitmapPaint.AntiAlias = true;
 
            //TypedArray a = context.obtainStyledAttributes(attrs,
            //      R.styleable.RoundImageView);
 
            //mBorderRadius = a.getDimensionPixelSize(
            //      R.styleable.RoundImageView_borderRadius, (int)TypedValue
            //              .applyDimension(TypedValue.COMPLEX_UNIT_DIP,
            //                      BODER_RADIUS_DEFAULT, getResources()
            //                              .getDisplayMetrics()));// 默认为10dp
            //type = a.getInt(R.styleable.RoundImageView_type, TYPE_CIRCLE);// 默认为Circle
 
            //a.recycle();
        }
 
        protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
 
            ///**
            // * 如果类型是圆形,则强制改变view的宽高一致,以小值为准
            // */
            if (type == TYPE_CIRCLE)
            {
                mWidth = Math.Min(MeasuredWidth, MeasuredHeight);
                mRadius = mWidth / 2;
                SetMeasuredDimension(mWidth, mWidth);
            }
        }
 
        /**
         * 初始化BitmapShader
         */
        private void setUpShader()
        {
            Drawable drawable = this.Drawable;
            if (drawable == null)
            {
                return;
            }
 
            Bitmap bmp = drawableToBitamp(drawable);
            if (bmp == null)
            {
                return;
            }
            // 将bmp作为着色器,就是在指定区域内绘制bmp
            mBitmapShader = new BitmapShader(bmp, Shader.TileMode.Clamp, Shader.TileMode.Clamp);
            float scale = 1.0f;
            // if (type == TYPE_CIRCLE)
            // {
            //    // 拿到bitmap宽或高的小值
            //    int bSize = Math.Min(bmp.Width, bmp.Height);
            //    scale = mWidth * 1.0f / bSize;
 
            // }
            // else if (type == TYPE_ROUND)
            // {
            //    //Log.e("TAG",
            //    //"b'w = " + bmp.getWidth() + " , " + "b'h = "
            //    //+ bmp.getHeight());
            //    if (!(bmp.Width == Width && bmp.Height == Height))
            //    {
            //        // 如果图片的宽或者高与view的宽高不匹配,计算出需要缩放的比例;缩放后的图片的宽高,一定要大于我们view的宽高;所以我们这里取大值;
            //        scale = Math.Max(Width * 1.0f / bmp.Width,
            //                         Height * 1.0f / bmp.Height);
            //    }
            // }
            // // shader的变换矩阵,我们这里主要用于放大或者缩小
            // mMatrix.SetScale(scale, scale);
            // // 设置变换矩阵
            // mBitmapShader.SetLocalMatrix(mMatrix);
            // // 设置shader
            // mBitmapPaint.SetShader(mBitmapShader);
 
 
            //新方法
            if (type == TYPE_CIRCLE)
            {
                // 拿到bitmap宽或高的小值
                int bSize = Math.Min(bmp.Width, bmp.Height);
                scale = mWidth * 1.0f / bSize;
                // shader的变换矩阵,我们这里主要用于放大或者缩小
                mMatrix.SetScale(scale, scale);
            }
            else if (type == TYPE_ROUND)
            {
                if (!(bmp.Width == Width && bmp.Height == Height))
                {
                    var scaleX = Width * 1.0f / bmp.Width;
                    var scaleY = Height * 1.0f / bmp.Height;
                    // shader的变换矩阵,我们这里主要用于放大或者缩小
                    mMatrix.SetScale(scaleX, scaleY);
                }
                else
                {
                    // shader的变换矩阵,我们这里主要用于放大或者缩小
                    mMatrix.SetScale(scale, scale);
                }
            }
 
            // 设置变换矩阵
            mBitmapShader.SetLocalMatrix(mMatrix);
            // 设置shader
            mBitmapPaint.SetShader(mBitmapShader);
        }
 
        protected override void OnDraw(Canvas canvas)
        {
            // Log.e("TAG", "onDraw");
            if (Drawable == null)
            {
                return;
            }
 
            try
            {
                setUpShader();
 
                if (type == TYPE_ROUND)
                {
 
 
                    if (!view.IsSetAloneRadius)
                    {
                        canvas.DrawRoundRect(mRoundRect, view.Radius, view.Radius,
                          mBitmapPaint);
                    }
                    else
                    {
                        canvas.DrawRoundRect(mRoundRect, view.mAloneRadius, view.mAloneRadius,
                          mBitmapPaint);
                        if ((view.mRadiusId & 1) == 0)
                        {
                            canvas.DrawRect(0, 0, view.mAloneRadius, view.mAloneRadius, mBitmapPaint);
                        }
 
                        if ((view.mRadiusId >> 1 & 1) == 0)
                        {
                            canvas.DrawRect(mRoundRect.Right - view.mAloneRadius, 0, mRoundRect.Right, view.mAloneRadius, mBitmapPaint);
                        }
 
                        if ((view.mRadiusId >> 2 & 1) == 0)
                        {
                            canvas.DrawRect(0, mRoundRect.Bottom - view.mAloneRadius, view.mAloneRadius, mRoundRect.Bottom, mBitmapPaint);
                        }
 
                        if ((view.mRadiusId >> 3 & 1) == 0)
                        {
                            canvas.DrawRect(mRoundRect.Right - view.mAloneRadius, mRoundRect.Bottom - view.mAloneRadius, mRoundRect.Right, mRoundRect.Bottom, mBitmapPaint);
                        }
 
                    }
 
                }
                else
                {
                    canvas.DrawCircle(mRadius, mRadius, mRadius, mBitmapPaint);
                    // drawSomeThing(canvas);
                }
 
            }
            catch
            {
                Shared.HDLUtils.WriteLine("error:ImageView OnDraw catch");
                base.OnDraw(canvas);
 
            }
        }
 
 
        protected override void OnSizeChanged(int w, int h, int oldw, int oldh)
        {
            base.OnSizeChanged(w, h, oldw, oldh);
 
            // 圆角图片的范围
            if (type == TYPE_ROUND)
                mRoundRect = new RectF(0, 0, w, h);
        }
 
        /**
         * drawable转bitmap
         * 
         * @param drawable
         * @return
         */
        private Bitmap drawableToBitamp(Drawable drawable)
        {
            if (drawable.GetType() == typeof(BitmapDrawable))
            {
                BitmapDrawable bd = (BitmapDrawable)drawable;
                return bd.Bitmap;
            }
            int w = drawable.IntrinsicWidth;
            int h = drawable.IntrinsicHeight;
            if (w <= 0 || h <= 0)
            {
                return null;
            }
            Bitmap bitmap = Bitmap.CreateBitmap(w, h, Bitmap.Config.Argb8888);
            Canvas canvas = new Canvas(bitmap);
            drawable.SetBounds(0, 0, w, h);
            drawable.Draw(canvas);
            return bitmap;
        }
 
        private static string STATE_INSTANCE = "state_instance";
        private static string STATE_TYPE = "state_type";
        private static string STATE_BORDER_RADIUS = "state_border_radius";
 
 
 
        protected override IParcelable OnSaveInstanceState()
        {
            Bundle bundle = new Bundle();
            bundle.PutParcelable(STATE_INSTANCE, base.OnSaveInstanceState());
            bundle.PutInt(STATE_TYPE, type);
            bundle.PutInt(STATE_BORDER_RADIUS, (int)view.Radius);
            return bundle;
        }
 
        protected override void OnRestoreInstanceState(IParcelable state)
        {
            if (state.GetType() == typeof(Bundle))
            {
                Bundle bundle = (Bundle)state;
                base.OnRestoreInstanceState((IParcelable)bundle
                            .GetParcelable(STATE_INSTANCE));
                this.type = bundle.GetInt(STATE_TYPE);
                view.Radius = (uint)bundle.GetInt(STATE_BORDER_RADIUS);
            }
            else
            {
                base.OnRestoreInstanceState(state);
            }
        }
 
 
        public void setType(int type)
        {
            if (this.type != type)
            {
                this.type = type;
                if (this.type != TYPE_ROUND && this.type != TYPE_CIRCLE)
                {
                    this.type = TYPE_CIRCLE;
                }
                RequestLayout();
            }
 
        }
 
        public int dp2px(int dpVal)
        {
            return (int)TypedValue.ApplyDimension(ComplexUnitType.Dip,
                                  dpVal, Resources.DisplayMetrics);
        }
    }
}