Android设置图片圆角的方法

Android中经常会遇到对图片进行二次处理,例如加圆角,或者显示圆形图片

实现的效果图:

方法一:

通过第三方框架Glide实现图片显示有圆角,有三种写法如下:

1.1、第一种实现:

RequestOptions options = new RequestOptions().error(R.drawable.img_load_failure).bitmapTransform(new RoundedCorners(30));//图片圆角为30
Glide.with(this).load(URL) //图片地址
    .apply(options)
    .into(ImagView);

1.2、第二种实现:

RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.ic_launcher_background);
requestOptions.circleCropTransform();
requestOptions.transforms( new RoundedCorners(30));
Glide.with(this).load(URL) //图片地址
    .apply(options)
    .into(ImagView);

1.3、第三种实现:

RequestOptions options = new RequestOptions().centerCrop() .transform(new RoundTransform(this,30));
Glide.with(this).load(URL) //图片地址
    .apply(options)
    .into(ImagView);
public class RoundTransform extends BitmapTransformation {
 private static float radius = 0f;
 public RoundTransform(Context context) {
  this(context, 4);
 } 

 public RoundTransform(Context context, int dp) {
  super(context);
  this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
 } 

 @Override
 protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
  Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
  return roundCrop(pool, bitmap);
 } 

 private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
  if (source == null) return null;
  Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
  if (result == null) {
   result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
  } 

  Canvas canvas = new Canvas(result);
  Paint paint = new Paint();
  paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
  paint.setAntiAlias(true);
  RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
  canvas.drawRoundRect(rectF, radius, radius, paint);
  return result;
 } 

 public String getId() {
  return getClass().getName() + Math.round(radius);
 } 

 @Override
 public void updateDiskCacheKey(MessageDigest messageDigest) { 

 }
}

方法二:

自定义ImageView:

<ImageView
  android:id="@+id/iv"
  android:layout_width="300dp"
  android:layout_height="300dp"
  android:layout_centerHorizontal="true"
  />
ImageView iv = findViewById(R.id.iv);
Bitmap bitmap =BitmapFactory.decodeResource(getResources(), R.drawable.fengjing);
  Bitmap outBitmap =getRoundBitmapByShader(bitmap, 500,300,20, 3);
  iv.setImageBitmap(outBitmap);
public class RoundRectImageView extends ImageView{

 private Paint paint;

 public RoundRectImageView(Context context) {
  this(context,null);
 }

 public RoundRectImageView(Context context, AttributeSet attrs) {
  this(context, attrs,0);
 }

 public RoundRectImageView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  paint = new Paint();
 }

 /**
  * 绘制圆角矩形图片
  * @author caizhiming
  */
 @Override
 protected void onDraw(Canvas canvas) {
  Drawable drawable = getDrawable();
  if (null != drawable) {
   Bitmap bitmap = getBitmapFromDrawable(drawable);
//   Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
   Bitmap b = getRoundBitmapByShader(bitmap,getWidth(),getHeight(), 50,0);
   final Rect rectSrc = new Rect(0, 0, b.getWidth(), b.getHeight());
   final Rect rectDest = new Rect(0,0,getWidth(),getHeight());
   paint.reset();
   canvas.drawBitmap(b, rectSrc, rectDest, paint);

  } else {
   super.onDraw(canvas);
  }
 }

 /**
  * 把资源图片转换成Bitmap
  * @param drawable
  * 资源图片
  * @return 位图
  */
 public static Bitmap getBitmapFromDrawable(Drawable drawable) {
  int width = drawable.getIntrinsicWidth();
  int height = drawable.getIntrinsicHeight();
  Bitmap bitmap = Bitmap.createBitmap(width, height, drawable
    .getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
    : Bitmap.Config.RGB_565);
  Canvas canvas = new Canvas(bitmap);
  //drawable.setBounds(-4, -4, width + 4, height + 4);
  drawable.draw(canvas);
  return bitmap;
 }

 public static Bitmap getRoundBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int radius, int boarder) {
  if (bitmap == null) {
   return null;
  }
  int width = bitmap.getWidth();
  int height = bitmap.getHeight();
  float widthScale = outWidth * 1f / width;
  float heightScale = outHeight * 1f / height;

  Matrix matrix = new Matrix();
  matrix.setScale(widthScale, heightScale);
  //创建输出的bitmap
  Bitmap desBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
  //创建canvas并传入desBitmap,这样绘制的内容都会在desBitmap上
  Canvas canvas = new Canvas(desBitmap);
  Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  //创建着色器
  BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
  //给着色器配置matrix
  bitmapShader.setLocalMatrix(matrix);
  paint.setShader(bitmapShader);
  //创建矩形区域并且预留出border
  RectF rect = new RectF(boarder, boarder, outWidth - boarder, outHeight - boarder);
  //把传入的bitmap绘制到圆角矩形区域内
  canvas.drawRoundRect(rect, radius, radius, paint);

  if (boarder > 0) {
   //绘制boarder
   Paint boarderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
   boarderPaint.setColor(Color.GREEN);
   boarderPaint.setStyle(Paint.Style.STROKE);
   boarderPaint.setStrokeWidth(boarder);
   canvas.drawRoundRect(rect, radius, radius, boarderPaint);
  }
  return desBitmap;
 }

}

方法三:

对图片进行处理,此方法还可以加边框

/**
 * 通过BitmapShader实现圆形边框
 * @param bitmap
 * @param outWidth 输出的图片宽度
 * @param outHeight 输出的图片高度
 * @param radius 圆角大小
 * @param boarder 边框宽度
 */
public static Bitmap getRoundBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int radius, int boarder) {
 if (bitmap == null) {
  return null;
 }
 int height = bitmap.getHeight();
 int width = bitmap.getWidth();

 float widthScale = outWidth * 1f / width;
 float heightScale = outHeight * 1f / height;

 Matrix matrix = new Matrix();
 matrix.setScale(widthScale, heightScale);
 //创建输出的bitmap
 Bitmap desBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
 //创建canvas并传入desBitmap,这样绘制的内容都会在desBitmap上
 Canvas canvas = new Canvas(desBitmap);
 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
 //创建着色器
 BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
 //给着色器配置matrix
 bitmapShader.setLocalMatrix(matrix);
 paint.setShader(bitmapShader);
 //创建矩形区域并且预留出border
 RectF rect = new RectF(boarder, boarder, outWidth - boarder, outHeight - boarder);
 //把传入的bitmap绘制到圆角矩形区域内
 canvas.drawRoundRect(rect, radius, radius, paint);

 if (boarder > 0) {
  //绘制boarder
  Paint boarderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  boarderPaint.setColor(Color.GREEN);
  boarderPaint.setStyle(Paint.Style.STROKE);
  boarderPaint.setStrokeWidth(boarder);
  canvas.drawRoundRect(rect, radius, radius, boarderPaint);
 }
 return desBitmap;
}

实现圆形和边框:

/**
 * 通过BitmapShader实现圆形边框
 * @param bitmap
 * @param outWidth 输出的图片宽度
 * @param outHeight 输出的图片高度
 * @param boarder 边框大小
 */
public static Bitmap getCircleBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int boarder) {
int radius;
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float widthScale = outWidth * 1f / width;
float heightScale = outHeight * 1f / height;

Bitmap desBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
if (outHeight > outWidth) {
 radius = outWidth / 2;
} else {
 radius = outHeight / 2;
}
//创建canvas
Canvas canvas = new Canvas(desBitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Matrix matrix = new Matrix();
matrix.setScale(widthScale, heightScale);
bitmapShader.setLocalMatrix(matrix);
paint.setShader(bitmapShader);
canvas.drawCircle(outWidth / 2, outHeight / 2, radius - boarder, paint);
if (boarder > 0) {
 //绘制boarder
 Paint boarderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 boarderPaint.setColor(Color.GREEN);
 boarderPaint.setStyle(Paint.Style.STROKE);
 boarderPaint.setStrokeWidth(boarder);
 canvas.drawCircle(outWidth / 2, outHeight / 2, radius - boarder, boarderPaint);
}
return desBitmap;
}

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

(0)

相关推荐

  • Android中实现圆角图片的几种方法

    Android中实现圆角图片有多种姿势,不知你解锁了几种? 方法一:setXfermode法 此种方式就是再new一个相同尺寸的bitmap,然后使用paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));先画圆角矩形,再画原始bitmap,然后就得到了一个圆角的bitmap了. public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) { Bitmap

  • android 实现圆角图片解决方案

    现在我们就来看看怎么样把图片的四角都变成圆形的,为什么要这样做那,如果要是这样界面就会非常的美观,下面我们就来看看代码吧. java代码: 复制代码 代码如下: public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canv

  • Android如何设置圆角图片

    在开发过程中有时需要将图片显示成圆角图片,一般我们可以通过在xml中设置drawable shape即可,但今天我给出另一种方法,用java代码动态去设置圆角,顺便做个简单的笔记. 主要原理是使用系统自带api: RoundedBitmapDrawableFactory 先上效果图: 由于比较简单,直接给出实现方式: public class MainActivity extends AppCompatActivity { private ImageView mImgRectRound; pri

  • Android中Glide加载圆形图片和圆角图片实例代码

    一.简介: 介绍两种使用 BitmapTransformation 来实现 Glide 加载圆形图片和圆角图片的方法.Glide 并不能直接支持 Round Pictures ,需要使用 BitmapTransformation 来进行处理. 二.网上的实现方式 这里介绍下网上常见的方式和使用 RoundedBitmapDrawable 两种方法,本质上是差不多的: 使用 Canvas 和 Paint 来绘制 使用 Android.support.v4.graphics.drawable.Rou

  • android 设置圆角图片实现代码

    复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" android:orientation="vertical" android:layout_wi

  • Android图片特效:黑白特效、圆角效果、高斯模糊

    1.黑白效果 复制代码 代码如下: /**     * 将彩色图转换为黑白图     *      * @param 位图     * @return 返回转换好的位图     */    public static Bitmap convertToBlackWhite(Bitmap bmp) {        int width = bmp.getWidth(); // 获取位图的宽        int height = bmp.getHeight(); // 获取位图的高 int[] pi

  • android图片圆角、图片去色处理示例

    Android中图片处理 用来对Android中的项目图片进行处理 复制代码 代码如下: package com.zhanggeng.contact.tools; import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException; import android

  • Android生成带圆角的Bitmap图片

    本文实例讲述了Android生成带圆角的Bitmap图片.分享给大家供大家参考.具体如下: 有时候我们在开发Android应用时,会遇到圆角图片的问题,那么,我们如何在Android中用代码来生成圆角Bitmap图片呢?下面这段代码也许能够帮到你. 该方法主要用到了drawRoundRect来画圆角矩形,然后通过drawBitmap来画图片. //生成圆角图片 public static Bitmap GetRoundedCornerBitmap(Bitmap bitmap) { try { B

  • Android关于Glide的使用(高斯模糊、加载监听、圆角图片)

    高斯模糊.加载监听.圆角图片这些相信大家都很熟悉,那如何实现这些效果,请大家参考本文进行学习. 1.引用 compile 'com.github.bumptech.glide:glide:3.7.0' 2.加载图片 2.1 基本加载 Glide.with(context)     .load(url)     .into(imageView); 2.2 设置加载中和加载失败的情况 Glide.with(context) .load(url) .placeholder(R.drawable.loa

  • Android将Glide动态加载不同大小的图片切圆角与圆形的方法

    Glide加载动态图片 首先我们先要去依赖一个githup:bumptech:glide:glide:3.7.0包: 使用Glide结合列表的样式进行图片加载: 1) 如果使用的是ListView,可以直接在Adapter的getView方法中使用: @Override public View getView(int position, View convertView, ViewGroup parent) { if (null == convertView) { //..... } Glide

随机推荐