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

Android Studio实现带边框的圆形头像

程序员文章站 2023-12-17 08:20:28
本文实例为大家分享了android studio实现带边框的圆形头像的具体代码,供大家参考,具体内容如下 效果显示: (没有边框的)  ...

本文实例为大家分享了android studio实现带边框的圆形头像的具体代码,供大家参考,具体内容如下

效果显示:

(没有边框的)

Android Studio实现带边框的圆形头像 

(有边框的)

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, 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, 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 / 5.0f);//外边框的大小
  mpaintborder.setcolor(mbordercolor);//添加外边框
  float radius = viewminsize / 2.0f;
  canvas.drawcircle(radius, radius, radius - mborderwidth / 6.0f, mpaintborder);
  canvas.translate(mborderwidth, mborderwidth);
  canvas.drawcircle(radius - mborderwidth, radius - mborderwidth, radius, 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、创建页面xml代码

<chenglong.activitytest.pengintohospital.utils.imageviewplus
 android:id="@+id/mine_iv_headportrait"
 android:layout_width="150dp"
 android:layout_height="150dp"
 android:src="@mipmap/hospital" />


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

上一篇:

下一篇: