我在Xamarin中有一个带圆角的ImageView。我的代码适用于Android 4.4及以下版本,但Lollipop现在它添加了黑色背景 黑区应该是透明的 - http://i.stack.imgur.com/GRFnl.png
public class RoundedImageView : ImageView
{
private int _cornerRadiusPercentage;
private Path _path = null;
public RoundedImageView(Context context, IAttributeSet attrs)
: base(context, attrs)
{
this._cornerRadiusPercentage = attrs.GetAttributeIntValue("http://schemas.boomo.android/roundedImageView", "corner_radius_percentage", 10);
this.SetLayerType(LayerType.Software, null);
}
public RoundedImageView(Activity activity, int cornerRadiusPercentage)
: base(activity)
{
this._cornerRadiusPercentage = cornerRadiusPercentage;
this.SetLayerType(LayerType.Software, null);
}
private void SetPath(Canvas canvas)
{
int offsetLeft = this.PaddingLeft;
int offsetRight = this.PaddingRight;
int offsetTop = this.PaddingTop;
int offsetBottom = this.PaddingBottom;
int cw = canvas.Width - offsetLeft - offsetRight;
int ch = canvas.Height - offsetTop - offsetBottom;
int cornerRadius = (int)((this._cornerRadiusPercentage / 100f) * cw);
_path = new Path();
_path.AddRoundRect(new RectF(offsetLeft, offsetTop, cw, ch), cornerRadius, cornerRadius, Path.Direction.Cw);
_path.Close();
}
protected override void OnDraw(Canvas canvas)
{
//Paint paint = new Paint();
//paint.Color = Color.Transparent;
//paint.Alpha = 254;
//paint.AntiAlias = true;
if (_path == null)
SetPath(canvas);
canvas.ClipPath(_path);
//canvas.DrawPath(_path, paint);
base.OnDraw(canvas);
}
}
*注意注释掉的部分是我试图用来解决问题的部分