本文实例为大家分享了AndroID Studio实现带边框的圆形头像的具体代码,供大家参考,具体内容如下
效果显示:
(没有边框的)
(有边框的)
1、创建自定义ImagVIEw控件
(1)、没有边框的
package chenglong.activitytest.pengintohospital.utils;import androID.content.Context;import androID.graphics.Bitmap;import androID.graphics.BitmapShader;import androID.graphics.Canvas;import androID.graphics.color;import androID.graphics.Matrix;import androID.graphics.Paint;import androID.graphics.Rect;import androID.graphics.Shader;import androID.graphics.drawable.BitmapDrawable;import androID.graphics.drawable.colorDrawable;import androID.graphics.drawable.Drawable;import androID.util.AttributeSet;import androID.Widget.ImageVIEw;/** * * 圆形图片 * Created by liCHENGLONG on 2017-10-09. */public class mine_ImageVIEwPlus extends ImageVIEw{ private Paint mPaintBitmap = new Paint(Paint.ANTI_AliAS_FLAG); private Paint mPaintborder = new Paint(Paint.ANTI_AliAS_FLAG); private Bitmap mRawBitmap; private BitmapShader mShader; private Matrix mMatrix = new Matrix(); private float mborderWIDth = dip2px(15); private int mbordercolor = 0x80bebebe; public mine_ImageVIEwPlus(Context context,AttributeSet attrs) { super(context,attrs); } @OverrIDe protected voID onDraw(Canvas canvas) { Bitmap rawBitmap = getBitmap(getDrawable()); if (rawBitmap != null){ int vIEwWIDth = getWIDth(); int vIEwHeight = getHeight(); int vIEwMinSize = Math.min(vIEwWIDth,vIEwHeight); float dstWIDth = vIEwMinSize; float dstHeight = vIEwMinSize; if (mShader == null || !rawBitmap.equals(mRawBitmap)){ mRawBitmap = rawBitmap; mShader = new BitmapShader(mRawBitmap,Shader.TileMode.CLAMP,Shader.TileMode.CLAMP); } if (mShader != null){ mMatrix.setScale((dstWIDth - mborderWIDth * 2) / rawBitmap.getWIDth(),(dstHeight - mborderWIDth * 2) / rawBitmap.getHeight()); mShader.setLocalMatrix(mMatrix); } mPaintBitmap.setShader(mShader); mPaintborder.setStyle(Paint.Style.stroke); mPaintborder.setstrokeWIDth(mborderWIDth); mPaintborder.setcolor(mbordercolor); float radius = vIEwMinSize / 2.0f; canvas.drawCircle(radius,radius,radius - mborderWIDth / 2.0f,mPaintborder); canvas.translate(mborderWIDth,mborderWIDth); canvas.drawCircle(radius - mborderWIDth,radius - mborderWIDth,mPaintBitmap); } else { super.onDraw(canvas); } } private Bitmap getBitmap(Drawable drawable){ if (drawable instanceof BitmapDrawable){ return ((BitmapDrawable)drawable).getBitmap(); } else if (drawable instanceof colorDrawable){ Rect rect = drawable.getBounds(); int wIDth = rect.right - rect.left; int height = rect.bottom - rect.top; int color = ((colorDrawable)drawable).getcolor(); Bitmap bitmap = Bitmap.createBitmap(wIDth,height,Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); canvas.drawARGB(color.Alpha(color),color.red(color),color.green(color),color.blue(color)); return bitmap; } else { return null; } } private int dip2px(int dipVal) { float scale = getResources().getdisplayMetrics().density; return (int)(dipVal * scale + 0.5f); }}
(2)、有边框的
package chenglong.activitytest.pengintohospital.utils;import androID.content.Context;import androID.graphics.Bitmap;import androID.graphics.BitmapShader;import androID.graphics.Canvas;import androID.graphics.color;import androID.graphics.Matrix;import androID.graphics.Paint;import androID.graphics.Rect;import androID.graphics.Shader;import androID.graphics.drawable.BitmapDrawable;import androID.graphics.drawable.colorDrawable;import androID.graphics.drawable.Drawable;import androID.util.AttributeSet;import androID.Widget.ImageVIEw;/** * * 带边框的圆形图片 * Created by liCHENGLONG on 2017-10-09. */public class ImageVIEwPlus extends ImageVIEw{ private Paint mPaintBitmap = new Paint(Paint.ANTI_AliAS_FLAG); private Paint mPaintborder = new Paint(Paint.ANTI_AliAS_FLAG);// private Bitmap mRawBitmap; private BitmapShader mShader; private Matrix mMatrix = new Matrix(); private float mborderWIDth = dip2px(15); private int mbordercolor = 0xFF0080FF;//外边框的颜色 public ImageVIEwPlus(Context context,(dstHeight - mborderWIDth * 2) / rawBitmap.getHeight()); mShader.setLocalMatrix(mMatrix); } mPaintBitmap.setShader(mShader); mPaintborder.setStyle(Paint.Style.stroke); mPaintborder.setstrokeWIDth(mborderWIDth / 5.0f);//外边框的大小 mPaintborder.setcolor(mbordercolor);//添加外边框 float radius = vIEwMinSize / 2.0f; canvas.drawCircle(radius,radius - mborderWIDth / 6.0f,color.blue(color)); return bitmap; } else { return null; } } private int dip2px(int dipVal) { float scale = getResources().getdisplayMetrics().density; return (int)(dipVal * scale + 0.5f); }}
2、创建页面xml代码
<chenglong.activitytest.pengintohospital.utils.ImageVIEwPlus androID:ID="@+ID/mine_iv_headportrait" androID:layout_wIDth="150dp" androID:layout_height="150dp" androID:src="@mipmap/hospital" />
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android Studio实现带边框的圆形头像全部内容,希望文章能够帮你解决Android Studio实现带边框的圆形头像所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)