package com.ss.glideimagedemo;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
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 static com.ss.glideimagedemo.GlideRoundCornersTransUtils.CornerType.ALL;
/**
* description:glide对指定角加载圆角
* Created by bss
*/
public class GlideRoundCornersTransUtils implements Transformation<Bitmap> {
private BitmapPool mBitmapPool;
private int mRadius;
private int mDiameter;
private CornerType mCornerType = ALL;
public GlideRoundCornersTransUtils(Context context, int radius, CornerType type) {
mBitmapPool = Glide.get(context).getBitmapPool();
mCornerType = type;
mRadius = radius;
mDiameter = 2 * mRadius;
}
public enum CornerType {
/** 所有角 */
ALL,
/** 左上 */
LEFT_TOP,
/** 左下 */
LEFT_BOTTOM,
/** 右上 */
RIGHT_TOP,
/** 右下 */
RIGHT_BOTTOM,
/** 左侧 */
LEFT,
/** 右侧 */
RIGHT,
/** 下侧 */
BOTTOM,
/** 上侧 */
TOP,
}
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
Bitmap source = resource.get();
int width = source.getWidth();
int height = source.getHeight();
Bitmap bitmap = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888);
if (bitmap == null) {
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
drawRoundRect(canvas, paint, width, height);
return BitmapResource.obtain(bitmap, mBitmapPool);
}
private void drawRoundRect(Canvas canvas, Paint paint, float width, float height) {
switch (mCornerType) {
case LEFT_TOP:
drawLeftTopCorner(canvas, paint, width, height);
break;
case LEFT_BOTTOM:
drawLeftBottomCorner(canvas, paint, width, height);
break;
case RIGHT_TOP:
drawRightTopCorner(canvas, paint, width, height);
break;
case RIGHT_BOTTOM:
drawRightBottomCorner(canvas, paint, width, height);
break;
case LEFT:
drawLeftCorner(canvas, paint, width, height);
break;
case RIGHT:
drawRightCorner(canvas, paint, width, height);
break;
case BOTTOM:
drawBottomCorner(canvas, paint, width, height);
break;
case TOP:
drawTopCorner(canvas, paint, width, height);
break;
case ALL:
default:
canvas.drawRoundRect(new RectF(0, 0, width, height), mRadius, mRadius, paint);
break;
}
}
/**
* 画左上角
*/
private void drawLeftTopCorner(Canvas canvas, Paint paint, float width, float height) {
canvas.drawRect(new RectF(mRadius, 0, width, height), paint);
canvas.drawRect(new RectF(0, mRadius, mRadius, height), paint);
canvas.drawArc(new RectF(0, 0, mDiameter, mDiameter), 180, 90, true, paint);
}
/**
* 画左下角
*/
private void drawLeftBottomCorner(Canvas canvas, Paint paint, float width, float height) {
canvas.drawRect(new RectF(0, 0, width, height - mRadius), paint);
canvas.drawRect(new RectF(mRadius, height - mRadius, width, height), paint);
canvas.drawArc(new RectF(0, height - mDiameter, mDiameter, height), 90, 90, true, paint);
}
/**
* 画右上角
*/
private void drawRightTopCorner(Canvas canvas, Paint paint, float width, float height) {
canvas.drawRect(new RectF(0, 0, width - mRadius, height), paint);
canvas.drawRect(new RectF(width - mRadius, mRadius, width, height), paint);
canvas.drawArc(new RectF(width - mDiameter, 0, width, mDiameter), 270, 90, true, paint);
}
/**
* 画右下角
*/
private void drawRightBottomCorner(Canvas canvas, Paint paint, float width, float height) {
canvas.drawRect(new RectF(0, 0, width, height - mRadius), paint);
canvas.drawRect(new RectF(0, height - mRadius, width - mRadius, height), paint);
canvas.drawArc(new RectF(width - mDiameter, height - mDiameter, width, height), 0, 90, true, paint);
}
/**
* 画左 角
*/
private void drawLeftCorner(Canvas canvas, Paint paint, float width, float height) {
canvas.drawRect(new RectF(mRadius, 0, width, height), paint);
canvas.drawRect(new RectF(0, mRadius, mRadius, height-mRadius), paint);
canvas.drawArc(new RectF(0, 0, mDiameter, mDiameter), 180, 90, true, paint);
canvas.drawArc(new RectF(0, height - mDiameter, mDiameter, height), 90, 90, true, paint);
}
/**
* 画右角
*/
private void drawRightCorner(Canvas canvas, Paint paint, float width, float height) {
canvas.drawRect(new RectF(0, 0, width - mRadius, height), paint);
canvas.drawRect(new RectF(width-mRadius, mRadius, width, height-mRadius), paint);
canvas.drawArc(new RectF(width - mDiameter, 0, width, mDiameter), 270, 90, true, paint);
canvas.drawArc(new RectF(width - mDiameter, height - mDiameter, width, height), 0, 90, true, paint);
}
/**
* 画上 角
*/
private void drawTopCorner(Canvas canvas, Paint paint, float width, float height) {
canvas.drawRect(new RectF(0, mRadius, width, height), paint);
canvas.drawRect(new RectF(mRadius, 0, width-mRadius, mRadius), paint);
canvas.drawArc(new RectF(0, 0, mDiameter, mDiameter), 180, 90, true, paint);
canvas.drawArc(new RectF(width - mDiameter, 0, width, mDiameter), 270, 90, true, paint);
}
/**
* 画下 角
*/
private void drawBottomCorner(Canvas canvas, Paint paint, float width, float height) {
canvas.drawRect(new RectF(0, 0, width, height - mRadius), paint);
canvas.drawRect(new RectF(mRadius, height-mRadius, width-mRadius, height), paint);
canvas.drawArc(new RectF(0, height - mDiameter, mDiameter, height), 90, 90, true, paint);
canvas.drawArc(new RectF(width - mDiameter, height - mDiameter, width, height), 0, 90, true, paint);
}
@Override
public String getId() {
return "RoundedTransformation(radius=" + mRadius + ", diameter=" + mDiameter + ")";
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
GlideImageDemo.zip (35个子文件)
GlideImageDemo
gradlew.bat 2KB
gradlew 5KB
gradle.properties 747B
app
proguard-rules.pro 684B
src
main
res
drawable
mipmap-hdpi
pretty_2.jpg 48KB
pretty_6.jpg 48KB
ic_launcher.png 3KB
pretty_4.jpg 48KB
pretty_5.jpg 48KB
pretty_3.jpg 48KB
pretty_9.jpg 48KB
pretty_8.jpg 48KB
pretty_1.jpg 48KB
pretty_7.jpg 48KB
values
colors.xml 214B
strings.xml 80B
styles.xml 394B
dimens.xml 216B
layout
activity_main.xml 5KB
values-w820dp
dimens.xml 364B
java
com
ss
glideimagedemo
GlideRoundCornersTransUtils.java 7KB
MainActivity.java 4KB
GlideCircleTransUtils.java 2KB
GlideRoundTransUtils.java 2KB
AndroidManifest.xml 747B
app.iml 10KB
.gitignore 8B
build.gradle 663B
libs
gradle
wrapper
gradle-wrapper.jar 52KB
gradle-wrapper.properties 233B
.gitignore 127B
GlideImageDemo.iml 868B
local.properties 544B
settings.gradle 16B
build.gradle 521B
共 35 条
- 1
资源评论
- wuzongan2019-05-06不错,可以安装
- huaihong198609062019-04-26应该能用,还没打开过
shan_zwq
- 粉丝: 72
- 资源: 27
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功