xamarin.Android ImageView 图片圆角(自定义属性、扩展控件)
新增 /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 图片圆角(自定义属性、扩展控件)的更多相关文章
- Xamarin.Android ImageView 图片圆角显示
第一步:在 values 文件夹下新增 Attrs.xml 文件 <?xml version="1.0" encoding="utf-8" ?> & ...
- [置顶]
xamarin android自定义标题栏(自定义属性、回调事件)
自定义控件的基本要求 这篇文章就当是自定义控件入门,看了几篇android关于自定义控件的文章,了解了一下,android自定义控件主要有3种方式: 自绘控件:继承View类,所展示的内容在OnDra ...
- Android CardView卡片布局 标签: 控件
CardView介绍 CardView是Android 5.0系统引入的控件,相当于FragmentLayout布局控件然后添加圆角及阴影的效果:CardView被包装为一种布局,并且经常在ListV ...
- Android support library支持包常用控件介绍(二)
谷歌官方推出Material Design 设计理念已经有段时间了,为支持更方便的实现 Material Design设计效果,官方给出了Android support design library ...
- [APP] Android 开发笔记 004-Android常用基本控件使用说明
TextView 文本框 EditText控件 Button 与 ImageButton ImageView RadioButton CheckBox复选框 TextView 文本框 ,用于显示文本的 ...
- (转载)Android UI设计之AlertDialog弹窗控件
Android UI设计之AlertDialog弹窗控件 作者:qq_27630169 字体:[增加 减小] 类型:转载 时间:2016-08-18我要评论 这篇文章主要为大家详细介绍了Android ...
- JavaFX的扩展控件库ControlsFX介绍
声明: 本博客文章原创类别的均为个人原创,版权所有.转载请注明出处: http://blog.csdn.net/ml3947,另外本人的个人博客:http://www.wjfxgame.com. ...
- WPF自定义控件(三)の扩展控件
扩展控件,顾名思义就是对已有的控件进行扩展,一般继承于已有的原生控件,不排除继承于自定义的控件,不过这样做意义不大,因为既然都自定义了,为什么不一步到位呢,有些不同的需求也可以通过此来完成,不过类似于 ...
- (转载) Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框
Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框 标签: Android清除功能EditText仿IOS的输入框 2013-09-04 17:33 70865人阅读 ...
随机推荐
- Numpy三维数组的转置与交换轴
二维数组的转置应该都知道,就是行列交换 而在numpy中也可以对三维数组进行转置,np.T 默认进行的操作是将0轴与2轴交换 本文主要对三位数组轴交换的理解上发表本人的看法. a = np.array ...
- Pandas排列和随机采样
随机重排序 import pandas as pd import numpy as np from pandas import Series df = pd.DataFrame(np.arange(5 ...
- mybatis 根据参数映射对应模型
ORM 框架的优势在于能让我们利用面向对象的思维去操作数据库, hibernate 作为重量级的 ORM 框架对面向对象的支持很强大.作为半自动化的 mybatis ,对面向对象的支持也是很完备的.这 ...
- 数据库类型空间效率探索(五)- decimal/float/double/varchar
以下测试为userinfo增加一列,列类型分别为decimal.float.double.varchar.由于innodb不支持optimize,所以每次测试,都会删除表test.userinfo,重 ...
- c++ 面试题(C/C++/STL)
1,智能指针:auto_ptr(c++11 已经弃用),unique_ptr(用于取代 auto_ptr), shared_ptr, weak_ptr http://www.cnblogs.com ...
- mysql学习笔记--数据库操作
一.显示数据库 show databases; 二.创建数据库 create database [if not exists] 数据库名 [字符编码] 注意: a. 如果已经存在数据库再创建会报错 b ...
- 音频音乐播放 Service
界面效果: 界面就一个播放的Button和一个进度条SeekBar,也可以自己加上两个显示时间的TextView: 点击播放时,有音乐声音,进度条也会自动更新,Button文字变成暂停 ...
- 用java开发图形界面项目,如何实现从本地选择图片文件并以二进制流的形式保存到MySQL数据库,并重新现实到面板
- vue项目如何通过前端实现自动识别并配置服务器环境地址
前言: 一般来说,一个web项目的生产环境和测试环境的服务器地址一旦确定下来,很少会频繁变动的.那么就可以单独写一个脚本文件,通过当前访问的域名来判断当前的访问环境,然后再通过一定的规则获取对应的服务 ...
- input中用中文输入法下的全角·替换英文输入法下的句号.
核心语句 <input type="text" onkeyup="this.value=this.value.replace(/\./g, '·')" o ...