视频直播系统源码,加载圆角图片

    科技2025-09-09  59

    视频直播系统源码,加载圆角图片的相关代码 导包

    implementation 'com.github.bumptech.glide:glide:3.6.1' public class GlideRoundCornersTransUtils implements Transformation<Bitmap> { private BitmapPool mBitmapPool; private int mRadius; private int mDiameter; private CornerType mCornerType = ALL; private DisplayMetrics metrics; public GlideRoundCornersTransUtils(Context context, int radius, CornerType type) { mBitmapPool = Glide.get(context).getBitmapPool(); metrics = context.getResources().getDisplayMetrics(); mRadius = (int) (radius * (metrics.densityDpi / 160f)); mCornerType = type; 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 + ")"; } }

    用法

    Glide.with(this).load(R.mipmap.pretty_1).bitmapTransform( new GlideRoundCornersTransUtils(this,25,GlideRoundCornersTransUtils.CornerType.TOP)).into(iv_Round2);

    如果要加载圆角如下

    public class GlideCircleTransUtils extends BitmapTransformation { public GlideCircleTransUtils(Context context) { super(context); } @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { return circleCrop(pool, toTransform); } private static Bitmap circleCrop(BitmapPool pool, Bitmap source) { if (source == null) return null; //获取资源的长宽,获取最小值 子位图的像素个数 int size = Math.min(source.getWidth(), source.getHeight()); // 子位图第一个像素在源位图的X坐标 int x = (source.getWidth() - size) / 2; //子位图第一个像素在源位图的y坐标 int y = (source.getHeight() - size) / 2; //创建新位图 source 源位图 Bitmap squared = Bitmap.createBitmap(source, x, y, size, size); //返回一个正好匹配给定宽、高和配置的只包含透明像素的Bitmap // 如果BitmapPool中找不到这样的Bitmap,就返回null Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888); //当返回null 时,创建给定宽、高和配置的新位图 if (result == null) { result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); } //画图 Canvas canvas = new Canvas(result); Paint paint = new Paint(); // 设置shader paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); //抗锯齿 paint.setAntiAlias(true); float r = size / 2f; // 用设置好的画笔绘制一个圆 canvas.drawCircle(r, r, r, paint); return result; } @Override public String getId() { return getClass().getName(); } }

    用法如下

    Glide.with(this).load(imgurl).transform(new GlideCircleTransUtils(this)).into(iv_circle);

    以上就是关于视频直播系统源码,加载圆角图片用到的相关代码,更多内容欢迎关注之后的文章 本文转载自网络,转载仅为分享干货知识,如有侵权欢迎联系云豹科技进行删除处理

    Processed: 0.011, SQL: 8