自定义圆形或圆角imageview

来源:互联网 发布:如何用excel编程 编辑:程序博客网 时间:2024/06/05 11:23


package com.yelong.medicalscience.widgets;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.widget.ImageView;

public class ShapeImageView extends ImageView {
 public static final String TAG = "ShapeImageView";

 public static final int RECTANGLE = 0;
 public static final int ROUND_RECTANGLE = 1;
 public static final int CIRCLE = 2;

 /** 圆角矩形的四角弧度,单位是像素 */
 private static final int RADIUS_X = 10;
 private static final int RADIUS_Y = 10;

 private Bitmap mShapeBmp = null;
 private BitmapDrawable mBmpDrawable = null;
 private int mShape = ROUND_RECTANGLE;
 private int mRadiusX = RADIUS_X;
 private int mRadiusY = RADIUS_Y;

 private int mWidth;
 private int mHeight;

 // paint的遮罩层,canvas在绘制原图和下一张图时,两者的交互绘制模式(相交、合并、异或等)
 private PorterDuffXfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
 // Paint的标识绘制过滤器,用于设置图片绘制的抗锯齿效果
 private PaintFlagsDrawFilter pdf = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);

 public ShapeImageView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
 }

 public ShapeImageView(Context context, AttributeSet attrs) {
  super(context, attrs);
 }

 public ShapeImageView(Context context) {
  super(context);
 }

 public int getShape() {
  return mShape;
 }

 public void setShape(int mShape) {
  this.mShape = mShape;
 }

 public int getRadiusX() {
  if (mShape == ROUND_RECTANGLE)
   return mRadiusX;
  else
   return 0;
 }

 public int getRadiusY() {
  if (mShape == ROUND_RECTANGLE)
   return mRadiusY;
  else
   return 0;
 }

 /**
  * 设置圆角矩形的弧度(当shape=ROUND_TANGLE才有效)
  *
  * @param mRadiusX
  */
 public void setRadius(int radiusX, int radiusY) {
  if (mShape == ROUND_RECTANGLE) {
   this.mRadiusX = radiusX;
   this.mRadiusY = radiusY;
  }
 }

 /**
  * 生成几何蒙版bitmap,即创建图片想要的几何形状(圆角、圆形等)的底板,用于与原图相交绘制
  *
  * @param w
  * @param h
  * @return
  */
 private Bitmap getShapeBitmap(int width, int height, Bitmap bitmapSrc, int shape) {
  float widthScaleRatio, heightScaleRatio;// 缩放比例,用于缩放圆角,使显示的圆角总是XY半径相等
  int bmpSrcWidth = bitmapSrc.getWidth();
  int bmpSrcHeight = bitmapSrc.getHeight();
  Bitmap bmpDst = Bitmap.createBitmap(bmpSrcWidth, bmpSrcHeight, Config.ARGB_8888);// 创建蒙板bitmap
  Canvas canvas = new Canvas(bmpDst);// 创建蒙板画布
  Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);// 创建透明抗锯齿画笔
  p.setColor(Color.parseColor("#ffffffff"));

  widthScaleRatio = (1.0f * bmpSrcWidth) / (1.0f * width);// 原图与目标bounds的缩放比例
  heightScaleRatio = (1.0f * bmpSrcHeight) / (1.0f * height);
  canvas.setDrawFilter(pdf);

  // 根据shape值绘制不同的几何蒙版
  switch (shape) {
  case ROUND_RECTANGLE:
   canvas.drawRoundRect(new RectF(0, 0, bmpSrcWidth, bmpSrcHeight), mRadiusX * widthScaleRatio, mRadiusY * heightScaleRatio, p);// 绘制蒙版bitmap(PS:圆角的半径的单位是像素)
   break;
  case CIRCLE:
   canvas.drawOval(new RectF(0, 0, bmpSrcWidth, bmpSrcHeight), p);// 绘制蒙版bitmap(PS:圆角的半径的单位是像素)
   break;
  default:
   break;
  }

  p.setXfermode(xfermode);
  canvas.drawBitmap(bitmapSrc, null, new RectF(0, 0, bmpSrcWidth, bmpSrcHeight), p);// 绘制原图并相交得出最终圆角图片
  p.setXfermode(null);

  return bmpDst;
 }

 @SuppressLint("DrawAllocation")
 @SuppressWarnings("deprecation")
 @Override
 protected void onDraw(Canvas canvas) {

  if (getDrawable() instanceof BitmapDrawable && getDrawable() != null) {
   mBmpDrawable = (BitmapDrawable) getDrawable();
   if (!mBmpDrawable.getBitmap().equals(mShapeBmp)) {
    mWidth = getWidth();
    mHeight = getHeight();
    mShapeBmp = getShapeBitmap(mWidth, mHeight, mBmpDrawable.getBitmap(), mShape);
    BitmapDrawable drawable = new BitmapDrawable(mShapeBmp);
    setImageDrawable(drawable);
    return;
   }

  }

  super.onDraw(canvas);
 }

}


0 0
原创粉丝点击