android中的ImageView只能显示矩形的图片,这样一来不能满足我们其他的需求,比如要显示圆角矩形的图片,这个时候,我们就需要自定义ImageView了,其原理就是首先获取到图片的位图,然后进行裁剪对应的圆角矩形的位图,然后在onDraw()进行绘制圆角矩形图片输出。
效果图如下:
自定义的圆形的ImageView类的实现代码如下:
包com.xc.xcskin.view; 进口android.content.Context; 进口android.graphics.Bitmap; 进口android.graphics.Bitmap.Config; 进口android.graphics.Canvas; 进口android.graphics.Paint; 进口android.graphics.RectF; 进口android.graphics.PorterDuff.Mode; 进口android.graphics.PorterDuffXfermode; 进口android.graphics.Rect; 进口android.graphics.drawable.BitmapDrawable; 进口android.graphics.drawable.Drawable; 进口android.util.AttributeSet; 进口android.widget.ImageView;/* * *自定义的圆角矩形ImageView,可以直接当组件在布局中使用。 * @author caizhiming * */公开课XCRoundRectImageView延伸ImageView { 私人油漆涂料; 公共XCRoundRectImageView(上下文语境){ 这(上下文,null); } 公共XCRoundRectImageView(上下文语境,AttributeSet attrs) { 这(上下文、attrs 0); } 公共XCRoundRectImageView(上下文语境、AttributeSet attrs int defStyle) { 超级(上下文、attrs defStyle); 涂=new (); }/* * *绘制圆角矩形图片 * @author caizhiming */@Override 保护空白> & lt; RelativeLayout xmlns: android=" http://schemas.android.com/apk/res/android " xmlns:工具=" http://schemas.android.com/tools " android: layout_width=" match_parent " android: layout_height=癿atch_parent”比; & lt; com.xc.xcskin.view.XCRoundRectImageView android: id=癅 + id/roundRectImageView” android: layout_centerInParent=" true " android: layout_width=" 200 dp” android: layout_height=" 200 dp” android: src=" https://www.yisu.com/zixun/@drawable roundimageview "/比; & lt;/RelativeLayout> >之前以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。
Android实现自定义ImageView的圆角矩形图片效果