目前業務是RecyclerView嵌套RecyclerView,子RecyclerView裏面顯示圖片,圖片顯示方式又分爲 多圖和單圖顯示方式(這個是已經調試好的效果)java
測試顯示結果只有單張圖片不顯示圓角,直覺告訴我多是圖片太大了(260*100)那就改小點 好比200*160 測試顯示仍是同樣的效果,沒有任何反應。android
索性直接把單個圖片的大小改爲和多圖同樣的,運行測試我去奇蹟出現了 竟然有圓角了 有圓角了 這是什麼狀況 難道是圖片只能是正文形?想着glide不能這麼變態吧(哈哈)git
又在想網找了幾個圓角的公共類 發現沒啥不同,在找到 https://github.com/wildma/GlideRoundImage/blob/master/GlideRoundImage.java 看到寫前4.0以上代碼問題github
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
return roundCrop(pool, bitmap);
}
完整代碼
import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.RectF; import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; import com.bumptech.glide.load.resource.bitmap.BitmapTransformation; import com.bumptech.glide.load.resource.bitmap.TransformationUtils; import java.security.MessageDigest; public class GlideRoundTransform extends BitmapTransformation { private static float radius = 0f; public GlideRoundTransform(Context context) { this(context, 3); } public GlideRoundTransform(Context context, int dp) { 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; } @Override public void updateDiskCacheKey(MessageDigest messageDigest) { } }
APK安裝包下載地址canvas