欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android利用CircleImageView实现圆形头像的方法

程序员文章站 2024-03-05 14:56:42
circleimageview实现圆形头像代码分享,供大家参考,具体内容如下 一、创建属性文件(attrs.xml) 具体操作: 1、在项目的values文件底下...

circleimageview实现圆形头像代码分享,供大家参考,具体内容如下

一、创建属性文件(attrs.xml)

具体操作:
1、在项目的values文件底下创建一新的属性文件,文件名为attrs:new->xml->values xml file:

Android利用CircleImageView实现圆形头像的方法

 2、补充attrs.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <declare-styleable name="circleimageview">
 <attr name="border_width" format="dimension" />
 <attr name="border_color" format="color" />
 </declare-styleable>
</resources>


二、添加实现类(circleimageview.java)

circleimageview.java(可以直接引用,看不懂也没事):

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;
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);
 if (mborderwidth != 0) {
  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);
 }
}

三、circleimageview的使用

具体操作:

1、先把你想要的图片拷贝到项目的res->drawable底下:

Android利用CircleImageView实现圆形头像的方法

2、直接在布局文件引用: 

 <com.example.suqh.drop.circleimageview
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:src="@drawable/img3" />

3、编译程序:

引用新建的类会出现下面这种情况的问题,直接点击build去编译程序:

Android利用CircleImageView实现圆形头像的方法

 4、还是不行的话重新编译程序:

Android利用CircleImageView实现圆形头像的方法

5、效果图:

Android利用CircleImageView实现圆形头像的方法

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。