怎么在Android中通过自定义ImageView实现一个圆角功能

  介绍

本篇文章给大家分享的是有关怎么在Android中通过自定义ImageView实现一个圆角功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

Android是什么

Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国谷歌公司和开放手机联盟领导及开发。

1。自定义属性attrs。xml

& lt; ? xml  version=?.0“,编码=皍tf-8" ?比;   & lt; resources>   & lt;才能declare-styleable  name=癛oundCornerImageView"比;   ,,,& lt; attr  name=皉adius",格式=癲imension",/比;   ,,,& lt; attr  name=發eft_top_radius",格式=癲imension",/比;   ,,,& lt; attr  name=皉ight_top_radius",格式=癲imension",/比;   ,,,& lt; attr  name=皉ight_bottom_radius",格式=癲imension",/比;   ,,,& lt; attr  name=發eft_bottom_radius",格式=癲imension",/比;   & lt;才能/declare-styleable>   & lt;/resources>

2。自定义RoundCornerImageView,继承AppCompatImageView

public  class  RoundCornerImageView  extends  AppCompatImageView  {   private 才能;float 宽度,,高度;   private 才能int  defaultRadius =, 0;   private 才能int 半径;   private 才能;int  leftTopRadius;   private 才能;int  rightTopRadius;   private 才能;int  rightBottomRadius;   private 才能;int  leftBottomRadius;         public 才能RoundCornerImageView (Context 上下文),{   ,,,这(上下文,null);   ,,,init(上下文,null);   ,,}      public 才能;RoundCornerImageView (Context 上下文,AttributeSet  attrs), {   ,,,(上下文,attrs,, 0);   ,,,init(上下文,attrs);   ,,}      public 才能;RoundCornerImageView (Context 上下文,AttributeSet  attrs,, int  defStyleAttr), {   ,,,超级(上下文,attrs, defStyleAttr);   ,,,init(上下文,attrs);   ,,}      private 才能;void  init (Context 上下文,AttributeSet  attrs), {   ,,,if  (Build.VERSION.SDK_INT  & lt;, 18), {   ,,,,,setLayerType (View.LAYER_TYPE_SOFTWARE, null);   ,,,}   ,,,//,读取配置   ,,,TypedArray  array =, context.obtainStyledAttributes (R.styleable.RoundCornerImageView attrs也);   ,,,radius =, array.getDimensionPixelOffset (defaultRadius R.styleable.RoundCornerImageView_radius也);   ,,,leftTopRadius =, array.getDimensionPixelOffset (defaultRadius R.styleable.RoundCornerImageView_left_top_radius也);   ,,,rightTopRadius =, array.getDimensionPixelOffset (defaultRadius R.styleable.RoundCornerImageView_right_top_radius也);   ,,,rightBottomRadius =, array.getDimensionPixelOffset (defaultRadius R.styleable.RoundCornerImageView_right_bottom_radius也);   ,,,leftBottomRadius =, array.getDimensionPixelOffset (defaultRadius R.styleable.RoundCornerImageView_left_bottom_radius也);         ,,,//如果四个角的值没有设置,那么就使用通用的半径的值。   ,,,if  (defaultRadius ==, leftTopRadius), {   ,,,,,leftTopRadius =,半径;   ,,,}   ,,,if  (defaultRadius ==, rightTopRadius), {   ,,,,,rightTopRadius =,半径;   ,,,}   ,,,if  (defaultRadius ==, rightBottomRadius), {   ,,,,,rightBottomRadius =,半径;   ,,,}   ,,,if  (defaultRadius ==, leftBottomRadius), {   ,,,,,leftBottomRadius =,半径;   ,,,}   ,,,array.recycle ();   ,,}         @Override才能   protected 才能;void  onLayout (boolean 改变,,int 左,int ,, int ,, int 底部),{   ,,,super.onLayout(改变,,,,,,对,,底部);   ,,,width =, getWidth ();   ,,,height =,获得();   ,,}      @Override才能   protected 才能;void  onDraw (Canvas 画布),{   ,,,//这里做下判断,只有图片的宽高大于设置的圆角距离的时候才进行裁剪   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

怎么在Android中通过自定义ImageView实现一个圆角功能