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

Android自定义圆角ImageView控件

程序员文章站 2023-11-05 11:39:40
目前一些比较火的图片加载库虽然支持圆角加载,若你是接的别人作了一半的项目,刚好别人用的图片加载库刚好不支持圆角加载,那么这颗控件你值得拥有.(支持网络图片的加载) 1.创...

目前一些比较火的图片加载库虽然支持圆角加载,若你是接的别人作了一半的项目,刚好别人用的图片加载库刚好不支持圆角加载,那么这颗控件你值得拥有.(支持网络图片的加载)

1.创建customimageview 类在你的项目中(源码如下)

import android.content.context;
import android.content.res.typedarray;
import android.graphics.bitmap;
import android.graphics.bitmap.config;
import android.graphics.bitmapfactory;
import android.graphics.canvas;
import android.graphics.matrix;
import android.graphics.paint;
import android.graphics.porterduff;
import android.graphics.porterduffxfermode;
import android.graphics.rectf;
import android.graphics.drawable.bitmapdrawable;
import android.graphics.drawable.drawable;
import android.util.attributeset;
import android.widget.imageview;

import com.towatt.charge.towatt.r;

/**
 * @author mr.lynn
 * @version 1.0<br>
 * 图片圆角实现
 */
public class customimageview extends android.support.v7.widget.appcompatimageview {
 private paint paint;
 private paint paintborder;
 private bitmap msrcbitmap;
 /**
  * 圆角的弧度
  */
 private float mradius;
 private boolean miscircle;

 public customimageview(final context context) {
  this(context, null);
 }

 public customimageview(context context, attributeset attrs) {
  this(context, attrs, r.attr.customimageviewstyle);
 }

 public customimageview(context context, attributeset attrs, int defstyle) {
  super(context, attrs, defstyle);
  typedarray ta = context.obtainstyledattributes(attrs,
    r.styleable.customimageview, defstyle, 0);
  mradius = ta.getdimension(r.styleable.customimageview_radius, 0);
  miscircle = ta.getboolean(r.styleable.customimageview_circle, false);
  int srcresource = attrs.getattributeresourcevalue(
    "http://schemas.android.com/apk/res/android", "src", 0);
  if (srcresource != 0)
   msrcbitmap = bitmapfactory.decoderesource(getresources(),
     srcresource);
  ta.recycle();
  paint = new paint();
  paint.setantialias(true);
  paintborder = new paint();
  paintborder.setantialias(true);
 }

 @override
 public void ondraw(canvas canvas) {
  int width = canvas.getwidth() - getpaddingleft() - getpaddingright();
  int height = canvas.getheight() - getpaddingtop() - getpaddingbottom();
  bitmap image = drawabletobitmap(getdrawable());
  if (miscircle) {
   bitmap resizeimage = resizeimagec(image, width, height);
   canvas.drawbitmap(createcircleimage(resizeimage, width, height),
     getpaddingleft(), getpaddingtop(), null);

  } else {

   bitmap resizeimage = resizeimage(image, width, height);
   canvas.drawbitmap(createroundimage(resizeimage, width, height),
     getpaddingleft(), getpaddingtop(), null);
  }
 }

 /**
  * 画圆角
  *
  * @param source
  * @param width
  * @param height
  * @return
  */
 private bitmap createroundimage(bitmap source, int width, int height) {
  paint paint = new paint();
  paint.setantialias(true);
  bitmap target = bitmap.createbitmap(width, height, config.argb_8888);
  canvas canvas = new canvas(target);
  rectf rect = new rectf(0, 0, width, height);
  canvas.drawroundrect(rect, mradius, mradius, paint);
  // 核心代码取两个图片的交集部分
  paint.setxfermode(new porterduffxfermode(porterduff.mode.src_in));
  canvas.drawbitmap(source, 0, 0, paint);
  return target;
 }

 /**
  * 画圆
  *
  * @param source
  * @param width
  * @param height
  * @return
  */
 private bitmap createcircleimage(bitmap source, int width, int height) {

  paint paint = new paint();
  paint.setantialias(true);
  bitmap target = bitmap.createbitmap(width, height, config.argb_8888);
  canvas canvas = new canvas(target);
  canvas.drawcircle(width / 2, height / 2, math.min(width, height) / 2,
    paint);
  // 核心代码取两个图片的交集部分
  paint.setxfermode(new porterduffxfermode(porterduff.mode.src_in));
  canvas.drawbitmap(source, (width - source.getwidth()) / 2,
    (height - source.getheight()) / 2, paint);
  return target;

 }

 @override
 protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
  int width = measurespec.getsize(widthmeasurespec);
  int height = measurespec.getsize(heightmeasurespec);
  setmeasureddimension(width, height);
 }

 /**
  * drawable转bitmap
  *
  * @param drawable
  * @return
  */
 private bitmap drawabletobitmap(drawable drawable) {
  if (drawable == null) {
   if (msrcbitmap != null) {
    return msrcbitmap;
   } else {
    return null;
   }
  } else if (drawable instanceof bitmapdrawable) {
   return ((bitmapdrawable) drawable).getbitmap();
  }
  bitmap bitmap = bitmap.createbitmap(drawable.getintrinsicwidth(),
    drawable.getintrinsicheight(), bitmap.config.argb_8888);
  canvas canvas = new canvas(bitmap);
  drawable.setbounds(0, 0, canvas.getwidth(), canvas.getheight());
  drawable.draw(canvas);
  return bitmap;
 }

 /**
  * 重设bitmap的宽高
  *
  * @param bitmap
  * @param newwidth
  * @param newheight
  * @return
  */
 private bitmap resizeimage(bitmap bitmap, int newwidth, int newheight) {
  int width = bitmap.getwidth();
  int height = bitmap.getheight();
  // 计算出缩放比
  float scalewidth = ((float) newwidth) / width;
  float scaleheight = ((float) newheight) / height;
  // 矩阵缩放bitmap
  matrix matrix = new matrix();

  matrix.postscale(scalewidth, scaleheight);
  return bitmap.createbitmap(bitmap, 0, 0, width, height, matrix, true);
 }

 /**
  * 重设bitmap的宽高
  *
  * @param bitmap
  * @param newwidth
  * @param newheight
  * @return
  */
 private bitmap resizeimagec(bitmap bitmap, int newwidth, int newheight) {
  int width = bitmap.getwidth();
  int height = bitmap.getheight();
  int x = (newwidth - width) / 2;
  int y = (newheight - height) / 2;
  if (x > 0 && y > 0) {
   return bitmap.createbitmap(bitmap, 0, 0, width, height, null, true);
  }

  float scale = 1;

  if (width > height) {
   // 按照宽度进行等比缩放
   scale = ((float) newwidth) / width;

  } else {
   // 按照高度进行等比缩放
   // 计算出缩放比
   scale = ((float) newheight) / height;
  }
  matrix matrix = new matrix();
  matrix.postscale(scale, scale);
  return bitmap.createbitmap(bitmap, 0, 0, width, height, matrix, true);
 }
}


2.在values目录下创建attrs.xml(若是已存在该文件)直接复制如下代码既可

<resources>
 <declare-styleable name="theme">
  <attr name="customimageviewstyle" format="reference" />
 </declare-styleable>
 <!-- 自定义圆角imageview -->
 <declare-styleable name="customimageview">
  <attr name="circle" format="boolean" />
  <attr name="radius" format="dimension" />
 </declare-styleable>
</resources>


3.正确的使用方式(在布局文件中)

注意此路径是你控件所在包

 <com.xxx.xxx.view.customimageview
  android:layout_margintop="5dp"
  android:layout_width="77dp"
  android:layout_height="77dp"
  lynn:radius="@dimen/size_10dp"
  android:src="@drawable/position_icon"
  android:id="@+id/iv_build_icon" />

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