Android之头像图片变圆形显示
程序员文章站
2022-05-07 11:52:07
一:先上效果图
二:实现步骤
1.自定义一个转换工具类
package com.common.base.util;
import android.con...
一:先上效果图
二:实现步骤
1.自定义一个转换工具类
package com.common.base.util; 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.common.base.r; public class circleimageview extends imageview { /** * 头像转换工具类 */ private static final scaletype scale_type = scaletype.center_crop; private static final bitmap.config bitmap_config = bitmap.config.argb_8888; 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; 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, attributeset attrs) { this(context, attrs, 0); } public circleimageview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); super.setscaletype(scale_type); typedarray a = context.obtainstyledattributes(attrs, r.styleable.circleimageview, defstyle, 0); 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; } } @override public scaletype getscaletype() { return scale_type; } @override public void setscaletype(scaletype scaletype) { if (scaletype != scale_type) { throw new illegalargumentexception(string.format("scaletype %s not supported.", scaletype)); } } @override protected void ondraw(canvas canvas) { if (getdrawable() == null) { return; } canvas.drawcircle(getwidth() / 2, getheight() / 2, mdrawableradius, mbitmappaint); canvas.drawcircle(getwidth() / 2, getheight() / 2, mborderradius, mborderpaint); } @override protected void onsizechanged(int w, int h, int oldw, int oldh) { super.onsizechanged(w, h, oldw, oldh); setup(); } public int getbordercolor() { return mbordercolor; } public void setbordercolor(int bordercolor) { if (bordercolor == mbordercolor) { return; } mbordercolor = bordercolor; mborderpaint.setcolor(mbordercolor); invalidate(); } public int getborderwidth() { return mborderwidth; } public void setborderwidth(int borderwidth) { if (borderwidth == mborderwidth) { return; } mborderwidth = borderwidth; setup(); } @override public void setimagebitmap(bitmap bm) { super.setimagebitmap(bm); mbitmap = bm; setup(); } @override public void setimagedrawable(drawable drawable) { super.setimagedrawable(drawable); mbitmap = getbitmapfromdrawable(drawable); setup(); } @override public void setimageresource(int resid) { super.setimageresource(resid); mbitmap = getbitmapfromdrawable(getdrawable()); setup(); } 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 { bitmap = bitmap.createbitmap(drawable.getintrinsicwidth(), drawable.getintrinsicheight(), bitmap_config); } canvas canvas = new canvas(bitmap); drawable.setbounds(0, 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; } 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, 0, getwidth(), getheight()); mborderradius = math.min((mborderrect.height() - mborderwidth) / 2, (mborderrect.width() - mborderwidth) / 2); mdrawablerect.set(mborderwidth, mborderwidth, mborderrect.width() - mborderwidth, mborderrect.height() - mborderwidth); mdrawableradius = math.min(mdrawablerect.height() / 2, mdrawablerect.width() / 2); updateshadermatrix(); invalidate(); } private void updateshadermatrix() { float scale; float dx = 0; float dy = 0; mshadermatrix.set(null); if (mbitmapwidth * mdrawablerect.height() > mdrawablerect.width() * mbitmapheight) { scale = mdrawablerect.height() / (float) mbitmapheight; dx = (mdrawablerect.width() - mbitmapwidth * scale) * 0.5f; } else { scale = mdrawablerect.width() / (float) mbitmapwidth; dy = (mdrawablerect.height() - mbitmapheight * scale) * 0.5f; } mshadermatrix.setscale(scale, scale); mshadermatrix.posttranslate((int) (dx + 0.5f) + mborderwidth, (int) (dy + 0.5f) + mborderwidth); mbitmapshader.setlocalmatrix(mshadermatrix); } }
2.xml的引用
<com.common.base.util.circleimageview android:id="@+id/touxian_id" android:layout_width="75dp" android:layout_height="75dp" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:src="@drawable/yonghu_touxiang" />
3.activity的实现
private circleimageview touxiang_id; touxiang_id = (circleimageview)findviewbyid(r.id.touxian_id) bitmaphelp.displayonimageview(mcontext, holder.touxiang_id, mlisty .getuser().getpic(), r.drawable.ic_launcher, r.drawable.ic_launcher);
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!