forked from syncfusion/xamarin-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoundedImageView.cs
87 lines (72 loc) · 2.23 KB
/
RoundedImageView.cs
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
#region Copyright Syncfusion Inc. 2001 - 2021
// Copyright Syncfusion Inc. 2001 - 2021. All rights reserved.
// Use of this code is subject to the terms of our license.
// A copy of the current license can be obtained at any time by e-mailing
// [email protected]. Any infringement will be prosecuted under
// applicable laws.
#endregion
using Android.Content;
using Android.Widget;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.Util;
namespace SampleBrowser
{
public class RoundedImageView :ImageView
{
int width;
public RoundedImageView (Context context,int _width, int _height) :
base (context)
{
width = _width;
}
public RoundedImageView (Context context, IAttributeSet attrs) :
base (context, attrs)
{
}
public RoundedImageView (Context context, IAttributeSet attrs, int defStyle) :
base (context, attrs, defStyle)
{
}
protected override void OnDraw (Canvas canvas)
{
Drawable drawable = Drawable;
if (drawable == null) {
return;
}
if (Width == 0 || Height == 0) {
return;
}
Bitmap b = ((BitmapDrawable) drawable).Bitmap;
Bitmap bitmap = b.Copy(Bitmap.Config.Argb8888, true);
int w = width;
Bitmap roundBitmap = getRoundedCroppedBitmap(bitmap, w);
canvas.DrawBitmap(roundBitmap, 0, 0, null);
}
public static Bitmap getRoundedCroppedBitmap(Bitmap bitmap, int radius) {
Bitmap finalBitmap;
if (bitmap.Width != radius || bitmap.Height!= radius)
finalBitmap = Bitmap.CreateScaledBitmap(bitmap, radius, radius,
false);
else
finalBitmap = bitmap;
Bitmap output = Bitmap.CreateBitmap(finalBitmap.Width,
finalBitmap.Height, Bitmap.Config.Argb8888);
Canvas canvas = new Canvas(output);
Paint paint = new Paint();
Rect rect = new Rect(0, 0, finalBitmap.Width,
finalBitmap.Height);
paint.AntiAlias=true;
paint.FilterBitmap=true;
paint.Dither=true;
canvas.DrawARGB(0, 0, 0, 0);
paint.Color=Color.ParseColor("#BAB399");
canvas.DrawCircle(finalBitmap.Width / 2 + 0.7f,
finalBitmap.Height / 2 + 0.7f,
finalBitmap.Width / 2 + 0.1f, paint);
paint.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.SrcIn));
canvas.DrawBitmap(finalBitmap, rect, rect, paint);
return output;
}
}
}