Home>

Although some popular image loading libraries currently support rounded corner loading,If you have picked up half of the project,It happens that the image loading library used by others does not support rounded corner loading.Then you deserve this control.(Supports loading of network pictures)

1. Create customimageview class in your project (source code is as follows)

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>
 * Picture rounded corners
 * /
public class customimageview extends android.support.v7.widget.appcompatimageview {
 private paint paint;
 private paint paintborder;
 private bitmap msrcbitmap;
 /**
  * Rounded radians
  * /
 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);
  }
 }
 /**
  * Draw rounded corners
  *
  * @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);
  //The core code takes the intersection of two pictures
  paint.setxfermode (new porterduffxfermode (porterduff.mode.src_in));
  canvas.drawbitmap (source, 0, 0, paint);
  return target;
 }
 /**
  * Draw a circle
  *
  * @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);
  //The core code takes the intersection of two pictures
  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 to 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;
 }
 /**
  * Reset the width and height of the 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 ();
  //Calculate the zoom ratio
  float scalewidth=((float) newwidth)/width;
  float scaleheight=((float) newheight)/height;
  //matrix scaling bitmap
  matrix matrix=new matrix ();
  matrix.postscale (scalewidth, scaleheight);
  return bitmap.createbitmap (bitmap, 0, 0, width, height, matrix, true);
 }
 /**
  * Reset the width and height of the 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 proportionally by width
   scale=((float) newwidth)/width;
  } else {
   //scale by height
   //Calculate the zoom ratio
   scale=((float) newheight)/height;
  }
  matrix matrix=new matrix ();
  matrix.postscale (scale, scale);
  return bitmap.createbitmap (bitmap, 0, 0, width, height, matrix, true);
 }
}

2. Create attrs.xml (if it already exists) in the values ​​directory and copy the following code directly

<resources>
 <declare-styleable name="theme">
  <attr name="customimageviewstyle" />
 </declare-styleable>
 <!-Custom rounded imageview->
 <declare-styleable name="customimageview">
  <attr name="circle" />
  <attr name="radius" />
 </declare-styleable>
</resources>

3. Correct usage (in the layout file)

Note that this path is the package where your control is located

<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" />
  • Previous Example of using explode () function to cut a string into an array in PHP
  • Next Summary of four methods for Android to parse XML files