新增 /values/Attrs.xml 文件

<?xml version="1.0" encoding="utf-8" ?>
<resources>
<declare-styleable name="RoundImageView">
<attr name="border_width" format="dimension" />
<attr name="border_color" format="color" />
</declare-styleable>
</resources>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using Android.App;
using Android.Content;
using Android.Graphics;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Util;
using Java.Util.Jar;
using Android.Graphics.Drawables;
using static Android.Graphics.PorterDuff;
using Android.Content.Res; namespace Dorid.UI
{
/// <summary>
/// 圆角
/// </summary>
public class RoundImageView: ImageView
{
private int mBorderWidth = ; private Bitmap mask;
private Paint paint;
private Color mBorderColor = Color.ParseColor("#FFFFFF"); private Context mContext;
public RoundImageView(Context context):base(context)
{
mContext = context;
} public RoundImageView(Context context, IAttributeSet attrs) : base(context, attrs)
{
mContext = context;
GetAttributes(context, attrs);
} public RoundImageView(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
{
mContext = context;
GetAttributes(context, attrs);
} /// <summary>
/// 获取自定义属性
/// </summary>
/// <param name="context"></param>
/// <param name="attrs"></param>
private void GetAttributes(Context context, IAttributeSet attrs)
{
TypedArray t_attrs = context.ObtainStyledAttributes(attrs, Resource.Styleable.RoundImageView);
mBorderColor = t_attrs.GetColor(Resource.Styleable.RoundImageView_border_color, mBorderColor);
int defalut = (int)( * context.Resources.DisplayMetrics.Density + 0.5f);
mBorderWidth = t_attrs.GetDimensionPixelOffset(Resource.Styleable.RoundImageView_border_width, defalut);
t_attrs.Recycle();
} protected override void OnDraw(Canvas canvas)
{
Drawable localDrawable = Drawable;
if (localDrawable == null)
return;
if (localDrawable is NinePatchDrawable)
return; if (this.paint == null)
{
PorterDuff.Mode localMode = PorterDuff.Mode.DstIn; Paint localPaint = new Paint();
localPaint.FilterBitmap = false;
localPaint.AntiAlias = true;
localPaint.SetXfermode(new PorterDuffXfermode(localMode));
this.paint = localPaint;
} int width = Width;
int height = Height;
/** 保存layer */
int layer = canvas.SaveLayer(0.0F, 0.0F, width, height, null, SaveFlags.All);
/** 设置drawable的大小 */
localDrawable.SetBounds(, , width, height);
/** 将drawable绑定到bitmap(this.mask)上面(drawable只能通过bitmap显示出来) */
localDrawable.Draw(canvas);
if ((this.mask == null) || (this.mask.IsRecycled))
{
this.mask = CreateOvalBitmap(width, height);
}
/** 将bitmap画到canvas上面 */
canvas.DrawBitmap(this.mask, 0.0F, 0.0F, this.paint);
/** 将画布复制到layer上 */
canvas.RestoreToCount(layer);
DrawBorder(canvas, width, height);
} private void DrawBorder(Canvas canvas, int width, int height)
{
if (mBorderWidth == )
return; Paint mBorderPaint = new Paint();
mBorderPaint.SetStyle(Paint.Style.Stroke);
mBorderPaint.AntiAlias = true;
mBorderPaint.Color= mBorderColor;
mBorderPaint.StrokeWidth = mBorderWidth;
/**
* 坐标x:view宽度的一般 坐标y:view高度的一般 半径r:因为是view的宽度-border的一半
*/
canvas.DrawCircle(width >> , height >> , (width - mBorderWidth) >> , mBorderPaint);
canvas = null;
} /// <summary>
/// 获取一个bitmap,目的是用来承载drawable;
/// 将这个bitmap放在canvas上面承载,并在其上面画一个椭圆(其实也是一个圆,因为width=height)来固定显示区域
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public Bitmap CreateOvalBitmap(int width, int height)
{
Bitmap.Config localConfig = Bitmap.Config.Argb8888;
Bitmap localBitmap = Bitmap.CreateBitmap(width, height, localConfig);
Canvas localCanvas = new Canvas(localBitmap);
Paint localPaint = new Paint();
int padding = mBorderWidth - ;
/**
* 设置椭圆的大小(因为椭圆的最外边会和border的最外边重合的,如果图片最外边的颜色很深,有看出有棱边的效果,所以为了让体验更加好,
* 让其缩进padding px)
*/
RectF localRectF = new RectF(padding, padding, width - padding, height - padding);
localCanvas.DrawOval(localRectF, localPaint);
return localBitmap;
}
}
}

axml 使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:controls="http://schemas.android.com/apk/res-auto"

android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px"
android:background="@color/activity_bg_color">
<Dorid.UI.RoundImageView
android:src="@drawable/icon"
android:layout_height="40dp"
android:layout_alignParentRight="true"
controls:border_width="3dp"
controls:border_color="#CCCCCC"

android:id="@+id/iv_userphoto_mycenter_myprofile"
android:layout_width="40dp"
android:layout_marginRight="20dp" />
</LinearLayout>

xamarin.Android ImageView 图片圆角(自定义属性、扩展控件)的更多相关文章

  1. Xamarin.Android ImageView 图片圆角显示

    第一步:在 values 文件夹下新增 Attrs.xml 文件 <?xml version="1.0" encoding="utf-8" ?> & ...

  2. [置顶] xamarin android自定义标题栏(自定义属性、回调事件)

    自定义控件的基本要求 这篇文章就当是自定义控件入门,看了几篇android关于自定义控件的文章,了解了一下,android自定义控件主要有3种方式: 自绘控件:继承View类,所展示的内容在OnDra ...

  3. Android CardView卡片布局 标签: 控件

    CardView介绍 CardView是Android 5.0系统引入的控件,相当于FragmentLayout布局控件然后添加圆角及阴影的效果:CardView被包装为一种布局,并且经常在ListV ...

  4. Android support library支持包常用控件介绍(二)

    谷歌官方推出Material Design 设计理念已经有段时间了,为支持更方便的实现 Material Design设计效果,官方给出了Android support design library ...

  5. [APP] Android 开发笔记 004-Android常用基本控件使用说明

    TextView 文本框 EditText控件 Button 与 ImageButton ImageView RadioButton CheckBox复选框 TextView 文本框 ,用于显示文本的 ...

  6. (转载)Android UI设计之AlertDialog弹窗控件

    Android UI设计之AlertDialog弹窗控件 作者:qq_27630169 字体:[增加 减小] 类型:转载 时间:2016-08-18我要评论 这篇文章主要为大家详细介绍了Android ...

  7. JavaFX的扩展控件库ControlsFX介绍

    声明:   本博客文章原创类别的均为个人原创,版权所有.转载请注明出处: http://blog.csdn.net/ml3947,另外本人的个人博客:http://www.wjfxgame.com. ...

  8. WPF自定义控件(三)の扩展控件

    扩展控件,顾名思义就是对已有的控件进行扩展,一般继承于已有的原生控件,不排除继承于自定义的控件,不过这样做意义不大,因为既然都自定义了,为什么不一步到位呢,有些不同的需求也可以通过此来完成,不过类似于 ...

  9. (转载) Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框

    Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框 标签: Android清除功能EditText仿IOS的输入框 2013-09-04 17:33 70865人阅读  ...

随机推荐

  1. Rancher 2.0 简单使用 重要部分截取

    学习地址 : https://rancher.com/docs/rancher/v2.x/en/quick-start-guide/ Install Rancher sudo docker run - ...

  2. [leetcode]8. String to Integer (atoi)字符串转整数

    Implement atoi which converts a string to an integer. The function first discards as many whitespace ...

  3. [leetcode]41. First Missing Positive第一个未出现的正数

    Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...

  4. laravel简书(1)

    Laravel的社区生态 中文社区(http://laravel-china.org) 5.4中文文档(http://d.laravel-china.org/docs/5.4) Laravel源码地址 ...

  5. Spark官方文档翻译(一)~Overview

    Spark官方文档翻译,有问题请及时指正,谢谢. Overview页 http://spark.apache.org/docs/latest/index.html Spark概述 Apache Spa ...

  6. Failed to connect to 127.0.0.1 port 1080: Connection refused package 问题解决方法

    错误: fatal: unable to access 'https://github.com/******': Failed to connect to 127.0.0.1 port 1080: C ...

  7. Netsharp总体介绍

    作者:秋时   日期:2014年02月05日   转载须说明出处  Netsharp交流群:338963050(请有详细的请求说明) Netsharp系列文章目录结构 Netsharp是一款免费的基于 ...

  8. chrome升级后出现滚动条无法滚动

    最近升级chrome最新版本后,导致项目中功能页面的局部滚动条无法滚动(心里暗骂了很久),无论怎么滚动都是最外层的滚动条响应... 1.猜想:尼玛google应该不会干事件流混乱这种事,pass: 2 ...

  9. Axure RP Extension for Chrome 插件安装

    描述 我的chmod浏览器上不去谷歌商店,我用的是蓝灯,登上商店后搜索Axure RP Extension for Chrome,下载安装,完成后进入这个插件的详细信息: 使用 打开用axure生成的 ...

  10. Java集合:ArrayList的实现原理

    Java集合---ArrayList的实现原理   目录: 一. ArrayList概述 二. ArrayList的实现 1) 私有属性 2) 构造方法 3) 元素存储 4) 元素读取 5) 元素删除 ...