- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
默认情况下,通过glide裁剪的图像没有边框我需要在圆形图像中有边框。
最佳答案
使用撰写
记得给build.gradle添加依赖:
implementation ‘androidx.constraintlayout:constraintlayout-compose:1.0.0-beta02’
在你的 Activity 中
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ShapeImageApplicationTheme {
Surface(color = MaterialTheme.colors.background) {
RoundedCornerShapeDemo()
}
}
}
}
}
@Composable
fun RoundedCornerShapeDemo() {
ImageResource(shape = RoundedCornerShape(10.dp))
}
@Composable
fun ImageResource(shape: Shape) {
ConstraintLayout(modifier = Modifier.fillMaxSize()) {
val (redBox, imageBox) = createRefs()
Box(
modifier = Modifier
.size(100.dp)
.clip(shape)
.background(Color.Red)
.constrainAs(redBox) {})
val image: Painter = painterResource(id = R.drawable.ic_launcher_background)
Image(painter = image, contentDescription = "", modifier = Modifier
.size(90.dp)
.clip(shape)
.constrainAs(imageBox) {
top.linkTo(redBox.top)
start.linkTo(redBox.start)
end.linkTo(redBox.end)
bottom.linkTo(redBox.bottom)
})
}
}
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
ShapeImageApplicationTheme {
RoundedCornerShapeDemo()
}
}
第 4 版
我是这样做的,圆角类:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
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 java.security.MessageDigest;
public class RoundedCornersTransformation implements Transformation<Bitmap> {
public enum CornerType {
ALL,
TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT,
TOP, BOTTOM, LEFT, RIGHT,
OTHER_TOP_LEFT, OTHER_TOP_RIGHT, OTHER_BOTTOM_LEFT, OTHER_BOTTOM_RIGHT,
DIAGONAL_FROM_TOP_LEFT, DIAGONAL_FROM_TOP_RIGHT, BORDER
}
private BitmapPool mBitmapPool;
private int mRadius;
private int mDiameter;
private int mMargin;
private CornerType mCornerType;
private String mColor;
private int mBorder;
public RoundedCornersTransformation(Context context, int radius, int margin) {
this(context, radius, margin, CornerType.ALL);
}
public RoundedCornersTransformation(Context context, int radius, int margin, String color, int border) {
this(context, radius, margin, CornerType.BORDER);
mColor = color;
mBorder = border;
}
public RoundedCornersTransformation(BitmapPool pool, int radius, int margin) {
this(pool, radius, margin, CornerType.ALL);
}
public RoundedCornersTransformation(Context context, int radius, int margin,
CornerType cornerType) {
this(Glide.get(context).getBitmapPool(), radius, margin, cornerType);
}
public RoundedCornersTransformation(BitmapPool pool, int radius, int margin,
CornerType cornerType) {
mBitmapPool = pool;
mRadius = radius;
mDiameter = mRadius * 2;
mMargin = margin;
mCornerType = cornerType;
}
@Override
public Resource<Bitmap> transform(Context context, 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) {
float right = width - mMargin;
float bottom = height - mMargin;
switch (mCornerType) {
case ALL:
canvas.drawRoundRect(new RectF(mMargin, mMargin, right, bottom), mRadius, mRadius, paint);
break;
case TOP_LEFT:
drawTopLeftRoundRect(canvas, paint, right, bottom);
break;
case TOP_RIGHT:
drawTopRightRoundRect(canvas, paint, right, bottom);
break;
case BOTTOM_LEFT:
drawBottomLeftRoundRect(canvas, paint, right, bottom);
break;
case BOTTOM_RIGHT:
drawBottomRightRoundRect(canvas, paint, right, bottom);
break;
case TOP:
drawTopRoundRect(canvas, paint, right, bottom);
break;
case BOTTOM:
drawBottomRoundRect(canvas, paint, right, bottom);
break;
case LEFT:
drawLeftRoundRect(canvas, paint, right, bottom);
break;
case RIGHT:
drawRightRoundRect(canvas, paint, right, bottom);
break;
case OTHER_TOP_LEFT:
drawOtherTopLeftRoundRect(canvas, paint, right, bottom);
break;
case OTHER_TOP_RIGHT:
drawOtherTopRightRoundRect(canvas, paint, right, bottom);
break;
case OTHER_BOTTOM_LEFT:
drawOtherBottomLeftRoundRect(canvas, paint, right, bottom);
break;
case OTHER_BOTTOM_RIGHT:
drawOtherBottomRightRoundRect(canvas, paint, right, bottom);
break;
case DIAGONAL_FROM_TOP_LEFT:
drawDiagonalFromTopLeftRoundRect(canvas, paint, right, bottom);
break;
case DIAGONAL_FROM_TOP_RIGHT:
drawDiagonalFromTopRightRoundRect(canvas, paint, right, bottom);
break;
case BORDER:
drawBorder(canvas, paint, right, bottom);
break;
default:
canvas.drawRoundRect(new RectF(mMargin, mMargin, right, bottom), mRadius, mRadius, paint);
break;
}
}
private void drawTopLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, mMargin + mDiameter),
mRadius, mRadius, paint);
canvas.drawRect(new RectF(mMargin, mMargin + mRadius, mMargin + mRadius, bottom), paint);
canvas.drawRect(new RectF(mMargin + mRadius, mMargin, right, bottom), paint);
}
private void drawTopRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, mMargin + mDiameter), mRadius,
mRadius, paint);
canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom), paint);
canvas.drawRect(new RectF(right - mRadius, mMargin + mRadius, right, bottom), paint);
}
private void drawBottomLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, mMargin + mDiameter, bottom),
mRadius, mRadius, paint);
canvas.drawRect(new RectF(mMargin, mMargin, mMargin + mDiameter, bottom - mRadius), paint);
canvas.drawRect(new RectF(mMargin + mRadius, mMargin, right, bottom), paint);
}
private void drawBottomRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
canvas.drawRoundRect(new RectF(right - mDiameter, bottom - mDiameter, right, bottom), mRadius,
mRadius, paint);
canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom), paint);
canvas.drawRect(new RectF(right - mRadius, mMargin, right, bottom - mRadius), paint);
}
private void drawTopRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
canvas.drawRoundRect(new RectF(mMargin, mMargin, right, mMargin + mDiameter), mRadius, mRadius,
paint);
canvas.drawRect(new RectF(mMargin, mMargin + mRadius, right, bottom), paint);
}
private void drawBottomRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, right, bottom), mRadius, mRadius,
paint);
canvas.drawRect(new RectF(mMargin, mMargin, right, bottom - mRadius), paint);
}
private void drawLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, bottom), mRadius, mRadius,
paint);
canvas.drawRect(new RectF(mMargin + mRadius, mMargin, right, bottom), paint);
}
private void drawRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, bottom), mRadius, mRadius,
paint);
canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom), paint);
}
private void drawOtherTopLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, right, bottom), mRadius, mRadius,
paint);
canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, bottom), mRadius, mRadius,
paint);
canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom - mRadius), paint);
}
private void drawOtherTopRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, bottom), mRadius, mRadius,
paint);
canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, right, bottom), mRadius, mRadius,
paint);
canvas.drawRect(new RectF(mMargin + mRadius, mMargin, right, bottom - mRadius), paint);
}
private void drawOtherBottomLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
canvas.drawRoundRect(new RectF(mMargin, mMargin, right, mMargin + mDiameter), mRadius, mRadius,
paint);
canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, bottom), mRadius, mRadius,
paint);
canvas.drawRect(new RectF(mMargin, mMargin + mRadius, right - mRadius, bottom), paint);
}
private void drawOtherBottomRightRoundRect(Canvas canvas, Paint paint, float right,
float bottom) {
canvas.drawRoundRect(new RectF(mMargin, mMargin, right, mMargin + mDiameter), mRadius, mRadius,
paint);
canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, bottom), mRadius, mRadius,
paint);
canvas.drawRect(new RectF(mMargin + mRadius, mMargin + mRadius, right, bottom), paint);
}
private void drawDiagonalFromTopLeftRoundRect(Canvas canvas, Paint paint, float right,
float bottom) {
canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, mMargin + mDiameter),
mRadius, mRadius, paint);
canvas.drawRoundRect(new RectF(right - mDiameter, bottom - mDiameter, right, bottom), mRadius,
mRadius, paint);
canvas.drawRect(new RectF(mMargin, mMargin + mRadius, right - mDiameter, bottom), paint);
canvas.drawRect(new RectF(mMargin + mDiameter, mMargin, right, bottom - mRadius), paint);
}
private void drawDiagonalFromTopRightRoundRect(Canvas canvas, Paint paint, float right,
float bottom) {
canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, mMargin + mDiameter), mRadius,
mRadius, paint);
canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, mMargin + mDiameter, bottom),
mRadius, mRadius, paint);
canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom - mRadius), paint);
canvas.drawRect(new RectF(mMargin + mRadius, mMargin + mRadius, right, bottom), paint);
}
private void drawBorder(Canvas canvas, Paint paint, float right,
float bottom) {
// stroke
Paint strokePaint = new Paint();
strokePaint.setStyle(Paint.Style.STROKE);
if (mColor != null) {
strokePaint.setColor(Color.parseColor(mColor));
} else {
strokePaint.setColor(Color.BLACK);
}
strokePaint.setStrokeWidth(mBorder);
canvas.drawRoundRect(new RectF(mMargin, mMargin, right, bottom), mRadius, mRadius, paint);
// stroke
canvas.drawRoundRect(new RectF(mMargin, mMargin, right, bottom), mRadius, mRadius, strokePaint);
}
@Override
public void updateDiskCacheKey(MessageDigest messageDigest) {
}
public String getId() {
return "RoundedTransformation(radius=" + mRadius + ", margin=" + mMargin + ", diameter="
+ mDiameter + ", cornerType=" + mCornerType.name() + ")";
}
}
现在在你的 Activity 中,你必须把这个:
public static int sCorner = 15;
public static int sMargin = 2;
public static int sBorder = 10;
public static String sColor = "#7D9067";
...
ImageView imageView = (ImageView) findViewById(R.id.activity_main_image_view);
ImageView mImageViewBorder = (ImageView) findViewById(R.id.activity_main_image_view_border);
....
// Rounded corners
Glide.with(this).load("http://scareface.jpeg")
.apply(RequestOptions.bitmapTransform(
new RoundedCornersTransformation(this, sCorner, sMargin))).into(mImageView);
// Rounded corners with border
Glide.with(this).load("http://scareface.jpeg")
.apply(RequestOptions.bitmapTransform(
new RoundedCornersTransformation(this, sCorner, sMargin, sColor, sBorder))).into(mImageViewBorder);
您可以在 github 中查看我的示例.
关于android - 如何在 Glide 中为圆形裁剪图像制作边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34037939/
从 Gilde 3.7.0 迁移到 Glide 4.9.0。 如下定义我的 MyAppGlideModule。 @GlideModule public class MyAppGlideModule
我将 Glide JS 用于我使用 ACF 和 Gutenberg Blocks 构建的幻灯片。 但是下一个按钮有问题。 ">"无法正确呈现 - 它会破坏整个 DOM。 “data-glide-dir
我到处搜索,但因为 Glide 已经发布了 4.4 版。我再也找不到应用 RoundedCornersTransformation 的方法了。我正在使用 glide-transformations甚至
我想调整 gif 文件的大小并保存它。我尝试使用一些建议的方法,但这些方法会出错,后来我才知道 Glide v4 中不推荐使用某些方法 byte[] bytes = Glide.
我正在尝试使用 firebase 在 android studio 中开发一个消息应用程序。根本没有语法错误。但是当我使用实现 'com.github.bumptech.glide:glide:4.8
这是我的代码: d.setBounds(15, 8, d.getIntrinsicWidth(), d.getIntrinsicHeight()); ImageSpan span = new Imag
我很难从 firebase 存储中获取图像 URL,我从数据库中获取了照片 URL,但据我所知,Glide 无法从这样的链接获取图片:com.google.firebase .storage.Uplo
我使用 Glide我的应用程序中的库内部自定义适配器 View 。但我有错误: "You must not call setTag() on a view Glide is targeting" 我的
我试图添加编译 应用程式 bulid.gradle compile 'com.github.bumptech.glide:glide:3.7.0' 当我尝试运行该应用程序时,它给了我错误 NoCla
我尝试了所有可能的方法,但无法弄清楚如何在我的 json 更新时更新图像以滑动 我的 android 应用程序最初使用 glide 从 json 加载图像,之后它只从缓存加载图像。它不会加载在服务器上
首先我集成了 Room 数据库并将一些信息存储到 db,其中包含一些图像 URL,现在我需要在 ImageViews 中显示这些图像。 现在尝试使用 glide,添加依赖项和注释处理器, 实现'com
我的房车有问题。我正在将 mp3 的封面艺术加载到字节数组中,然后使用 glide 将字节数组加载到图像中 (vh.coverArt)。但是,当我使用 glide 而不是仅使用 .SetImageBa
简单的 Glide.with(context).load(url).into(image) 在将项目与 同步后无法正常工作 compile 'com.github.bumptech.glide:gli
我真的为 Rails 的所有新 JS 更改而苦苦挣扎。我正在尝试实现非常简单的 slider 脚本 Glide.js 这是我的 app/javascripts/packs/front.js 文件: /
Build.gradle(模块:App) apply plugin: 'com.android.application' android { compileSdkVersion 23
我正在创建一个应用程序,让用户通过按 ImageView 来更改他/她的图像,该图像将来自他/她的图库并将上传到我的 FirebaseStorage 中。用户的个人详细信息单独上传到 Firebase
我可以通过滑动将图像成功加载到 ImageView 中,并使用 .circularCrop() 方法对其进行裁剪。 Glide.with(this) .load("https://i.img
使用 glide 在 recyclerview imageview 项目上加载图像。受阻 Glide 听众我意识到图像资源已准备好,但图像未显示在 imageview 中。当我在 Glide 监听器
我在我的应用程序中使用 glide 加载图像。它在我的情况下工作正常,比如 if(image_enabled==1){ Glide.with(getContext()).load(constant.S
是否可以使用 缓存图像? Glide 未在 imageView 中显示?.如果是那怎么办? 现在我正在做这个代码: Glide .with(getApplicationContext()) .
我是一名优秀的程序员,十分优秀!