第一个方法是通过在外层添加一个CardView
<android.support.v7.widget.CardView android:id="@+id/cardview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dp" app:cardBackgroundColor="@color/C2_5" app:cardCornerRadius="10dp" app:cardElevation="0dp" app:contentPadding="0.5dp"> <ImageView android:id="@+id/food_img" android:layout_width="85dp" android:layout_height="85dp" android:scaleType="centerCrop" android:src="@drawable/noinfo_big" /> </android.support.v7.widget.CardView>
但是如果要在图片外层加一个带颜色的边框该如何处理呢,这样的话,第一种方法就用不成了,圆角会很难看,那么就需要用到第二种自定义
package XXX.view; // // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.RectF; import android.graphics.Shader.TileMode; import android.support.annotation.NonNull; import com.bumptech.glide.Glide; import com.bumptech.glide.load.Transformation; import com.bumptech.glide.load.engine.Resource; import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; import com.bumptech.glide.load.resource.bitmap.BitmapResource; import java.security.MessageDigest; /** * 集圆角/圆形/蒙层一体 */ public class GlideCircleTransform implements Transformation<Bitmap> { private BitmapPool mBitmapPool; private int coverColor = -1; private float radius; private boolean exceptLeftTop, exceptRightTop, exceptLeftBottom, exceptRightBotoom; /** * 除了那几个角不需要圆角的 * * @param leftTop * @param rightTop * @param leftBottom * @param rightBottom */ public void setExceptCorner(boolean leftTop, boolean rightTop, boolean leftBottom, boolean rightBottom) { this.exceptLeftTop = leftTop; this.exceptRightTop = rightTop; this.exceptLeftBottom = leftBottom; this.exceptRightBotoom = rightBottom; } public GlideCircleTransform(Context context) { this(Glide.get(context).getBitmapPool()); } public GlideCircleTransform(Context context, int coverColor) { this(Glide.get(context).getBitmapPool()); this.coverColor = coverColor; } public GlideCircleTransform(Context context, int coverColor, float radius) { this(Glide.get(context).getBitmapPool()); this.radius = radius; this.coverColor = coverColor; } public GlideCircleTransform(BitmapPool pool) { this.radius = 0.0F; this.mBitmapPool = pool; } public Resource<Bitmap> transform(Context context,Resource<Bitmap> resource, int outWidth, int outHeight) { Bitmap source = (Bitmap)resource.get(); Bitmap bitmap = null; int width; int height; int afterWidth; if(this.radius > 0.0F) { //设置圆角 int afterHeight; float bili; float bili1; if(outWidth > outHeight) { bili = (float)outHeight / (float)outWidth; afterWidth = source.getWidth(); afterHeight = (int)((float)source.getWidth() * bili); if(afterHeight > source.getHeight()) { bili1 = (float)outWidth / (float)outHeight; afterHeight = source.getHeight(); afterWidth = (int)((float)source.getHeight() * bili1); } } else if(outWidth < outHeight) { bili = (float)outWidth / (float)outHeight; afterHeight = source.getHeight(); afterWidth = (int)((float)source.getHeight() * bili); if(afterWidth > source.getWidth()) { bili1 = (float)outHeight / (float)outWidth; afterWidth = source.getWidth(); afterHeight = (int)((float)source.getWidth() * bili1); } } else { afterHeight = source.getHeight(); afterWidth = afterHeight; } width = (source.getWidth() - afterWidth) / 2; height = (source.getHeight() - afterHeight) / 2; //修正圆角 this.radius *= (float)afterHeight / (float)outHeight; bitmap = this.mBitmapPool.get(afterWidth, afterHeight, Config.ARGB_8888); if(bitmap == null) { bitmap = Bitmap.createBitmap(afterWidth, afterHeight, Config.ARGB_8888); } Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); BitmapShader shader = new BitmapShader(source, TileMode.CLAMP, TileMode.CLAMP); if(width != 0 || height != 0) { Matrix matrix = new Matrix(); matrix.setTranslate((float)(-width), (float)(-height)); shader.setLocalMatrix(matrix); } paint.setShader(shader); paint.setAntiAlias(true); RectF rectF = new RectF(0.0F, 0.0F, (float)canvas.getWidth(), (float)canvas.getHeight()); canvas.drawRoundRect(rectF, this.radius, this.radius, paint); //设置蒙层 Paint coverPaint = new Paint(); coverPaint.setAntiAlias(true); coverPaint.setColor(coverColor); if(coverColor > -1) { canvas.drawRoundRect(rectF, this.radius, this.radius, coverPaint); } if (exceptLeftTop) { //左上角不为圆角 canvas.drawRect(0, 0, radius, radius, paint); if (coverColor > -1) { canvas.drawRect(0, 0, radius, radius, coverPaint); } } if (exceptRightTop) {//右上角不为圆角 canvas.drawRect(canvas.getWidth() - radius, 0, canvas.getWidth(), radius, paint); if (coverColor > -1) { canvas.drawRect(canvas.getWidth() - radius, 0, canvas.getWidth(), radius, coverPaint); } } if (exceptLeftBottom) {//左下角不为圆角 canvas.drawRect(0, canvas.getHeight() - radius, radius, canvas.getHeight(), paint); if (coverColor > -1) { canvas.drawRect(0, canvas.getHeight() - radius, radius, canvas.getHeight(), coverPaint); } } if (exceptRightBotoom) {//右下角不为圆角 canvas.drawRect(canvas.getWidth() - radius, canvas.getHeight() - radius, canvas.getWidth(), canvas.getHeight(), paint); if (coverColor > -1) { canvas.drawRect(canvas.getWidth() - radius, canvas.getHeight() - radius, canvas.getWidth(), canvas.getHeight(), coverPaint); } } } else { //设置圆形 width = Math.min(source.getWidth(), source.getHeight()); height = (source.getWidth() - width) / 2; afterWidth = (source.getHeight() - width) / 2; bitmap = this.mBitmapPool.get(width, width, Config.ARGB_8888); if(bitmap == null) { bitmap = Bitmap.createBitmap(width, width, Config.ARGB_8888); } Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); BitmapShader shader = new BitmapShader(source, TileMode.CLAMP, TileMode.CLAMP); if(height != 0 || afterWidth != 0) { Matrix matrix = new Matrix(); matrix.setTranslate((float)(-height), (float)(-afterWidth)); shader.setLocalMatrix(matrix); } paint.setShader(shader); paint.setAntiAlias(true); float r = (float)width / 2.0F; canvas.drawCircle(r, r, r, paint); //设置蒙层 if(coverColor != -1) { Paint paint1 = new Paint(); paint1.setAntiAlias(true); paint1.setColor(coverColor); canvas.drawCircle(r, r, r, paint1); } } return BitmapResource.obtain(bitmap, this.mBitmapPool); } public String getId() { return this.getClass().getName(); } @Override public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) { } }
然后在需要转换的imageview的地方调用
RequestOptions options = new RequestOptions().transform(new GlideCircleTransform(mContext,mContext.getResources().getColor(R.color.C2_5),20)); Glide.with(mContext).load(item.getImage()).apply(options).into((ImageView) helper.getView(R.id.imageview));
这样就完美处理了
相关推荐
Glide加载圆形图片和自定义圆角图片,可自己设置圆角大小,可以对指定的角进行加载圆角,自定义圆角大小
Glide加载圆形图片和自定义圆角图片,可自己设置圆角大小;
介绍两种使用 BitmapTransformation 来实现 Glide 加载圆形图片和圆角图片的方法。Glide 并不能直接支持 Round Pictures ,需要使用 BitmapTransformation 来进行处理。 二、网上的实现方式 这里介绍下网上常见的...
Android中经常会遇到对图片进行二次处理,例如加圆角,或者显示圆形图片 实现的效果图: 方法一: 通过第三方框架Glide实现图片显示有圆角,有三种写法如下: 1.1、第一种实现: RequestOptions options = new ...
主要给大家介绍了关于Android如何将Glide动态加载不同大小的图片切圆角与圆形的方法,文中通过示例代码介绍的非常吸纳关系,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧。
Glide动态加载图片(transform方法不识别问题) ...(1)Glide加载图片(圆角、圆形等)采用Glide3.6.1 (2)Github上资源关于解决4.0以上Glide的参考Demo 可以参考个人博客“关于Android加载图片的问题”,有详细讲解
Google推荐的图片加载库,专注于流畅的滚动 Glide 比Picasso 加载快 但需要更大的内存来缓存 Glide 不光接受Context,还接受Activity 和 Fragment ,...加载设置圆形图片 旋转 图片转圆形 图片模糊 图片转换圆角图片
最近在项目中遇到了一个奇怪的问题,Glide和CircleImageView一起使用加载圆形头像,发现第一次死活都加载出来,出来的是一张占位图,当你刷新的时候或者第二次进入的时候才能加载出来。究其原因,CircleImageView 把...
Glide是 Google推荐的图片加载库,它可以支持来自url,Android资源,文件,Uri中的图片加载,同时还支持gif图片的加载,以及各种图片显示前的bitmap处理(例如:圆角图片,圆形图片,高斯模糊,旋转,灰度等等),缓存处理,请求...
glide加载网络资源,raw,assets,资源文件,圆形图片,圆角图片,本地相册所有图片,gif图片,sdk图片等等。
Glide的使用,使用glide加载圆形,圆角图片。
xml 的方式是不可以的, xml 只能改变背景为圆角的,没法改图片本身。 只能通过java代码改,但是java图像处理不太会用啊,网上的教程一看一大堆代码,感觉太难了,不适合本人。 用 Glide 4.x 框架可以做,但是不...
glide-transformations实现多种item效果,一个提供各种图像转换的安卓转换库,
Glide加载圆形图片和自定义圆角图片,可自己设置圆角大小,可以对指定的角进行加载圆角
于是自己自定义了个View,实现图片的圆角以及圆形效果。效果图: Android 圆角图片的实现形式,包括用第三方、也有系统的。比如makeramen:roundedimageview,系统的cardview , glide .fresco 。 compile '...
作者sfsheng0322,源码...1、通过提供的属性可以设置图片的圆角、边框。 2、可以设置点击触摸图片时的颜色、透明度。 3、一行代码加载来自网络、res、SDCard中的图片,可加载成圆形。 4、可以监听加载图片时的进度。
动态获权+Glide图片加载+缓存的BaseActivity。在BaseActivity中实现动态获权与缓存代码,集成直接调用即可。而且自定义Glide一些方法,比如圆角、圆形、高斯模糊等处理
Glide是 Google推荐的图片加载库,它可以支持来自url,Android资源,文件,Uri中的图片加载,同时还支持gif图片的加载,以及各种图片显示前的bitmap处理(例如:圆角图片,圆形图片,高斯模糊,旋转,灰度等等),缓存处理,请求...