forked from Redth/MonoDroid.UrlImageViewHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoundedImage.cs
113 lines (97 loc) · 2.52 KB
/
RoundedImage.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Graphics.Drawables;
using Android.Graphics;
namespace UrlImageViewHelper
{
public class RoundedImage : BitmapDrawable
{
Bitmap mBitmap;
Paint mPaint;
RectF mRectF;
int mBitmapWidth;
int mBitmapHeight;
public RoundedImage(Bitmap bitmap)
{
mBitmap = bitmap;
mRectF = new RectF();
mPaint = new Paint();
mPaint.AntiAlias = true;
mPaint.Dither = true;
BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp);
mPaint.SetShader(shader);
// NOTE: we assume bitmap is properly scaled to current density
mBitmapWidth = mBitmap.Width;
mBitmapHeight = mBitmap.Height;
}
public override void Draw(Android.Graphics.Canvas canvas)
{
canvas.DrawOval(mRectF, mPaint);
}
protected override void OnBoundsChange(Rect bounds)
{
base.OnBoundsChange(bounds);
mRectF.Set(bounds);
}
public override void SetAlpha(int alpha)
{
if (mPaint.Alpha != alpha)
{
mPaint.Alpha = alpha;
InvalidateSelf();
}
}
public override void SetColorFilter(ColorFilter cf)
{
mPaint.SetColorFilter(cf);
}
public override int Opacity
{
get
{
return (int)Format.Translucent;
}
}
public override int IntrinsicWidth
{
get
{
return mBitmapWidth;
}
}
public override int IntrinsicHeight
{
get
{
return mBitmapHeight;
}
}
public void SetAntiAlias(bool aa)
{
mPaint.AntiAlias = aa;
InvalidateSelf();
}
public override void SetFilterBitmap(bool filter)
{
mPaint.FilterBitmap = filter;
InvalidateSelf();
}
public override void SetDither(bool dither)
{
mPaint.Dither = dither;
InvalidateSelf();
}
public Bitmap Bitmap
{
get{ return mBitmap; }
}
}
}