在AndroID开发中我们常常用到圆形的头像,如果每次加载之后再进行圆形裁剪特别麻烦。所以在这里写一个自定义圆形ImageVIEw,直接去加载网络图片,这样的话就特别的方便。
先上效果图
主要的方法
1.让自定义 circleimageVIEw 继承ImageVIEw
/*** 自定义圆形头像* Created by Dylan on 2015/11/26 0026.*/public class circleimageVIEw extends ImageVIEw {}
2.在构造方法中获取在xml中设置的值
public circleimageVIEw(Context context) {super(context);}public circleimageVIEw(Context context,AttributeSet attrs) {this(context,attrs,0);}public circleimageVIEw(Context context,AttributeSet attrs,int defStyle) {super(context,defStyle);super.setScaleType(SCALE_TYPE);/*** 获取在xml中声明的属性*/TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.circleimageVIEw,defStyle,0);//获取xml中的属性mborderWIDth = a.getDimensionPixelSize(R.styleable.circleimageVIEw_border_wIDth,DEFAulT_border_WIDTH);mbordercolor = a.getcolor(R.styleable.circleimageVIEw_border_color,DEFAulT_border_color);a.recycle();mReady = true;if (mSetupPending) {setup();mSetupPending = false;}}
3.初始化各种参数设置
/*** 画圆形图的初始化工作*/private voID setup() {if (!mReady) {mSetupPending = true;return;}if (mBitmap == null) {return;}/***调用这个方法来产生一个画有一个位图的渲染器(Shader)。bitmap 在渲染器内使用的位图tileX The tiling mode for x to draw the bitmap in. 在位图上X方向花砖模式tileY The tiling mode for y to draw the bitmap in. 在位图上Y方向花砖模式TileMode:(一共有三种)CLAMP :如果渲染器超出原始边界范围,会复制范围内边缘染色。REPEAT :横向和纵向的重复渲染器图片,平铺。MIRROR :横向和纵向的重复渲染器图片,这个和REPEAT 重复方式不一样,他是以镜像方式平铺。*/mBitmapShader = new BitmapShader(mBitmap,Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);/*** 设置画圆形的画笔*/mBitmapPaint.setAntiAlias(true);//设置抗锯齿mBitmapPaint.setShader(mBitmapShader);//绘制图形时的图形变换mborderPaint.setStyle(Paint.Style.stroke);mborderPaint.setAntiAlias(true);mborderPaint.setcolor(mbordercolor);mborderPaint.setstrokeWIDth(mborderWIDth);mBitmapHeight = mBitmap.getHeight();mBitmapWIDth = mBitmap.getWIDth();/*** 设置边框矩形的坐标*/mborderRect.set(0,getWIDth(),getHeight());/*** 设置边框圆形的半径为图片的宽度和高度的一半的最大值*/mborderRadius = Math.max((mborderRect.height() - mborderWIDth) / 2,(mborderRect.wIDth() - mborderWIDth) / 2);/*** 设置图片矩形的坐标*/mDrawableRect.set(mborderWIDth,mborderWIDth,mborderRect.wIDth() - mborderWIDth,mborderRect.height() - mborderWIDth);/*** 设置图片圆形的半径为图片的宽度和高度的一半的最大值*/mDrawableRadius = Math.max(mDrawableRect.height() / 2,mDrawableRect.wIDth() / 2);updateShaderMatrix();/*** 调用onDraw()方法进行绘画*/invalIDate();}/*** 更新位图渲染*/private voID updateShaderMatrix() {float scale;float dx = 0;float dy = 0;/*** 重置*/mShaderMatrix.set(null);/***计算缩放比,因为如果图片的尺寸超过屏幕,那么就会自动匹配到屏幕的尺寸去显示。* 确定移动的xy坐标**/if (mBitmapWIDth * mDrawableRect.height() > mDrawableRect.wIDth() * mBitmapHeight) {scale = mDrawableRect.wIDth() / (float) mBitmapWIDth;dy = (mDrawableRect.height() - mBitmapHeight * scale) * 0.5f;} else {scale = mDrawableRect.height() / (float) mBitmapHeight;dx = (mDrawableRect.wIDth() - mBitmapWIDth * scale) * 0.5f;}mShaderMatrix.setScale(scale,scale);mShaderMatrix.postTranslate((int) (dx + 0.5f) + mborderWIDth,(int) (dy + 0.5f) + mborderWIDth);/*** 设置shader的本地矩阵*/mBitmapShader.setLocalMatrix(mShaderMatrix);}
4.画圆
@OverrIDeprotected voID onDraw(Canvas canvas) {if (getDrawable() == null) {return;}/*** 画圆形图片*/canvas.drawCircle(getWIDth() / 2,getHeight() / 2,mDrawableRadius,mBitmapPaint);/*** 画圆形边框*/canvas.drawCircle(getWIDth() / 2,mborderRadius,mborderPaint);}
完整代码,有完整的注释
1.circleimageVIEw 主类
import androID.content.Context;import androID.content.res.TypedArray;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.RectF;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;import com.kejiang.yuandl.R;import com.kejiang.yuandl.utils.ImageSizeUtil;/*** 自定义圆形头像* Created by Dylan on 2015/11/26 0026.*/public class circleimageVIEw extends ImageVIEw {/*** 圆形头像默认,CENTER_CROP!=系统默认的CENTER_CROP;* 将图片等比例缩放,让图像的长边边与ImageVIEw的边长度相同,短边不够的留空白,缩放后截取圆形部分进行显示。*/private static final ScaleType SCALE_TYPE = ScaleType.CENTER_CROP;/*** 图片的压缩质量* Alpha_8就是Alpha由8位组成,------Alpha_8 代表8位Alpha位图* ARGB_4444就是由4个4位组成即16位,------ARGB_4444 代表16位ARGB位图* ARGB_8888就是由4个8位组成即32位,------ARGB_8888 代表32位ARGB位图* RGB_565就是R为5位,G为6位,B为5位共16位,------ARGB_565 代表8位RGB位图*/private static final Bitmap.Config BITMAP_CONfig = Bitmap.Config.ARGB_8888;/*** 默认colorDrawable的宽和高*/private static final int colorDRAWABLE_DIMENSION = 1;/*** 默认边框宽度*/private static final int DEFAulT_border_WIDTH = 0;/*** 默认边框颜色*/private static final int DEFAulT_border_color = color.BLACK;/*** 画图片的矩形*/private final RectF mDrawableRect = new RectF();/*** 画边框的矩形*/private final RectF mborderRect = new RectF();/*** 对图片进行缩放和移动的矩阵*/private final Matrix mShaderMatrix = new Matrix();/*** 画图片的画笔*/private final Paint mBitmapPaint = new Paint();/*** 画边框的画笔*/private final Paint mborderPaint = new Paint();/*** 默认边框颜色*/private int mbordercolor = DEFAulT_border_color;/*** 默认边框宽度*/private int mborderWIDth = DEFAulT_border_WIDTH;private Bitmap mBitmap;/*** 产生一个画有一个位图的渲染器(Shader)*/private BitmapShader mBitmapShader;/*** 图片的实际宽度*/private int mBitmapWIDth;/*** 图片实际高度*/private int mBitmapHeight;/*** 图片半径*/private float mDrawableRadius;/*** 边框半径*/private float mborderRadius;/*** 是否初始化准备好*/private boolean mReady;/*** 内边距*/private boolean mSetupPending;public circleimageVIEw(Context context) {super(context);}public circleimageVIEw(Context context,DEFAulT_border_color);a.recycle();mReady = true;if (mSetupPending) {setup();mSetupPending = false;}}@OverrIDepublic ScaleType getScaleType() {return SCALE_TYPE;}@OverrIDeprotected voID onDraw(Canvas canvas) {if (getDrawable() == null) {return;}/*** 画圆形图片*/canvas.drawCircle(getWIDth() / 2,mborderPaint);}@OverrIDeprotected voID onSizeChanged(int w,int h,int olDW,int oldh) {super.onSizeChanged(w,h,olDW,oldh);setup();}/*** 获取边框颜色** @return*/public int getbordercolor() {return mbordercolor;}/*** 设置边框颜色** @param bordercolor*/public voID setbordercolor(int bordercolor) {if (bordercolor == mbordercolor) {return;}mbordercolor = bordercolor;mborderPaint.setcolor(mbordercolor);invalIDate();}/*** 获取边框宽度** @return*/public int getborderWIDth() {return mborderWIDth;}/*** 设置边框宽度** @param borderWIDth*/public voID setborderWIDth(int borderWIDth) {if (borderWIDth == mborderWIDth) {return;}mborderWIDth = borderWIDth;setup();}/*** 设置资源图片** @param bm*/@OverrIDepublic voID setimageBitmap(Bitmap bm) {super.setimageBitmap(bm);mBitmap = bm;setup();}/*** 设置资源图片** @param drawable*/@OverrIDepublic voID setimageDrawable(Drawable drawable) {super.setimageDrawable(drawable);mBitmap = getBitmapFromDrawable(drawable);setup();}/*** 设置资源ID** @param resID*/@OverrIDepublic voID setimageResource(int resID) {super.setimageResource(resID);mBitmap = getBitmapFromDrawable(getDrawable());setup();}/*** 获取资源图片** @param drawable* @return*/private Bitmap getBitmapFromDrawable(Drawable drawable) {if (drawable == null) {return null;}if (drawable instanceof BitmapDrawable) {return ((BitmapDrawable) drawable).getBitmap();}try {Bitmap bitmap;if (drawable instanceof colorDrawable) {bitmap = Bitmap.createBitmap(colorDRAWABLE_DIMENSION,colorDRAWABLE_DIMENSION,BITMAP_CONfig);} else {ImageSizeUtil.ImageSize imageSize = ImageSizeUtil.getimageVIEwSize(this);bitmap = Bitmap.createBitmap(imageSize.wIDth,imageSize.height,BITMAP_CONfig);}Canvas canvas = new Canvas(bitmap);drawable.setBounds(0,canvas.getWIDth(),canvas.getHeight());drawable.draw(canvas);return bitmap;} catch (OutOfMemoryError e) {return null;}}/*** 画圆形图的方法*/private voID setup() {if (!mReady) {mSetupPending = true;return;}if (mBitmap == null) {return;}/***调用这个方法来产生一个画有一个位图的渲染器(Shader)。bitmap 在渲染器内使用的位图tileX The tiling mode for x to draw the bitmap in. 在位图上X方向花砖模式tileY The tiling mode for y to draw the bitmap in. 在位图上Y方向花砖模式TileMode:(一共有三种)CLAMP :如果渲染器超出原始边界范围,会复制范围内边缘染色。REPEAT :横向和纵向的重复渲染器图片,平铺。MIRROR :横向和纵向的重复渲染器图片,这个和REPEAT 重复方式不一样,他是以镜像方式平铺。*/mBitmapShader = new BitmapShader(mBitmap,(int) (dy + 0.5f) + mborderWIDth);/*** 设置shader的本地矩阵*/mBitmapShader.setLocalMatrix(mShaderMatrix);}}
2.里面所使用到的ImageSizeUtil
public class ImageSizeUtil{/*** 根据ImageVIEw获适当的压缩的宽和高* * @param imageVIEw* @return*/public static ImageSize getimageVIEwSize(ImageVIEw imageVIEw){ImageSize imageSize = new ImageSize();displayMetrics displayMetrics = imageVIEw.getContext().getResources().getdisplayMetrics();LayoutParams lp = imageVIEw.getLayoutParams();int wIDth = imageVIEw.getWIDth();// 获取imagevIEw的实际宽度if (wIDth <= 0){if(lp!=null){wIDth = lp.wIDth;// 获取imagevIEw在layout中声明的宽度}}if (wIDth <= 0){//wIDth = imageVIEw.getMaxWIDth();// 检查最大值wIDth = getimageVIEwFIEldValue(imageVIEw,"mMaxWIDth");}if (wIDth <= 0){wIDth = displayMetrics.wIDthPixels;}int height = imageVIEw.getHeight();// 获取imagevIEw的实际高度if (height <= 0){if(lp!=null){height = lp.height;// 获取imagevIEw在layout中声明的宽度}}if (height <= 0){height = getimageVIEwFIEldValue(imageVIEw,"mMaxHeight");// 检查最大值}if (height <= 0){height = displayMetrics.heightPixels;}imageSize.wIDth = wIDth;imageSize.height = height;return imageSize;}public static class ImageSize{public int wIDth;public int height;}}
用法
布局文件
<com.bulemobi.yuandl.vIEw.circleimageVIEwandroID:ID="@+ID/ci"androID:layout_wIDth="180dp"androID:layout_height="180dp"androID:scaleType="centerCrop"androID:layout_gravity="center_horizontal"app:border_wIDth="1dp"app:border_color="#FF0000"/>
Activity中的代码,用xutils3加载图片
circleimageVIEw circleimageVIEw= (circleimageVIEw) findVIEwByID(R.ID.ci);x.image().bind(circleimageVIEw,url,new ImageOptions.Builder().setimageScaleType(ImageVIEw.ScaleType.CENTER_CROP).build()
以上所述是小编给大家介绍的AndroID 自定义圆形头像circleimageVIEw支持加载网络图片的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
总结以上是内存溢出为你收集整理的Android 自定义圆形头像CircleImageView支持加载网络图片的实现代码全部内容,希望文章能够帮你解决Android 自定义圆形头像CircleImageView支持加载网络图片的实现代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)