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

android图片处理之让图片变成圆形

程序员文章站 2024-03-07 21:23:45
这几天做项目,有些地方的图片需要用到圆形图片,所以百度了一下,在github上找到一个开源项目,处理很简单,效果如下:   使用起来特别简单,一共三步...

这几天做项目,有些地方的图片需要用到圆形图片,所以百度了一下,在github上找到一个开源项目,处理很简单,效果如下:

 android图片处理之让图片变成圆形

使用起来特别简单,一共三步,具体如下:

 1、创建一个java类circleimageview把下面的代码复制过去,报错的导包

package de.hdodenhof.circleimageview;
import edu.njupt.zhb.main.r;
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);
    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、在values文件夹下面创建一个xlm文件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>


3、接下来就是使用了,直接在布局文件中使用,具体使用如下:

直接创建一个自定义的circleimageview   src属性设置你要改变成圆形的图片

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#ff00ff"
   >

<com.example.circleimageview.circleimageview 
  android:id="@+id/imageview"
  android:layout_width="100dp"
  android:layout_height="100dp"
  android:layout_centerinparent="true"
  android:src="@drawable/control_image"
  />
</relativelayout>

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