mr.*_*fox 174 android android-glide
所以,有人知道如何使用Glide显示带圆角的图像吗?我正在使用Glide加载图像,但我不知道如何将舍入的参数传递给此库.
我需要显示图像如下例子:
jim*_*251 443
滑翔V4:
Glide.with(context)
.load(url)
.apply(RequestOptions.circleCropTransform())
.into(imageView);
Run Code Online (Sandbox Code Playgroud)
Glide V3:
您可以使用RoundedBitmapDrawable
Glide的圆形图像.无需自定义ImageView.
Glide.with(context).load(url).asBitmap().centerCrop().into(new BitmapImageViewTarget(imageView) {
@Override
protected void setResource(Bitmap resource) {
RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(context.getResources(), resource);
circularBitmapDrawable.setCircular(true);
imageView.setImageDrawable(circularBitmapDrawable);
}
});
Run Code Online (Sandbox Code Playgroud)
Har*_*han 65
看看这篇文章,glide vs picasso ...
编辑:链接的帖子没有标出库中的重要区别.Glide自动进行回收.有关更多信息,请参阅TWiStErRob的评论.
Glide.with(this).load(URL).transform(new CircleTransform(context)).into(imageView);
public static class CircleTransform extends BitmapTransformation {
public CircleTransform(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());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
// TODO this could be acquired from the pool too
Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
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();
}
}
Run Code Online (Sandbox Code Playgroud)
Rom*_*nko 35
最简单的方法(需要Glide 4.xx)
Glide.with(context).load(uri).apply(RequestOptions().circleCrop()).into(imageView)
Run Code Online (Sandbox Code Playgroud)
小智 21
试试这种方式
Glide.with(this)
.load(R.drawable.thumbnail)
.bitmapTransform(new CropCircleTransformation(this))
.into(mProfile);
Run Code Online (Sandbox Code Playgroud)
<ImageView
android:id="@+id/img_profile"
android:layout_width="76dp"
android:layout_height="76dp"
android:background="@drawable/all_circle_white_bg"
android:padding="1dp"/>
Run Code Online (Sandbox Code Playgroud)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="@android:color/white"/>
</shape>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)
它非常简单我已经看到Glide库非常好的图书馆和论文基于凌空谷歌的图书馆
使用此库来获得圆形图像视图
https://github.com/hdodenhof/CircleImageView
现在
//对于一个简单的视图:
@Override
public void onCreate(Bundle savedInstanceState) {
...
CircleImageView civProfilePic = (CircleImageView)findViewById(R.id.ivProfile);
Glide.load("http://goo.gl/h8qOq7").into(civProfilePic);
}
Run Code Online (Sandbox Code Playgroud)
//列表:
@Override
public View getView(int position, View recycled, ViewGroup container) {
final ImageView myImageView;
if (recycled == null) {
myImageView = (CircleImageView) inflater.inflate(R.layout.my_image_view,
container, false);
} else {
myImageView = (CircleImageView) recycled;
}
String url = myUrls.get(position);
Glide.load(url)
.centerCrop()
.placeholder(R.drawable.loading_spinner)
.animate(R.anim.fade_in)
.into(myImageView);
return myImageView;
}
Run Code Online (Sandbox Code Playgroud)
并以XML格式
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/ivProfile
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_centerInParent="true"
android:src="@drawable/hugh"
app:border_width="2dp"
app:border_color="@color/dark" />
Run Code Online (Sandbox Code Playgroud)
其他解决方案对我不起作用.我发现他们都有很大的缺点:
非常有趣的是,在摸索了这个之后,我找到了关于圆角和圆圈的Fresco库页面,其中列出了基本相同的限制并以声明结束:
在Android上进行四舍五入是没有真正好的解决方案,必须在上述权衡之间做出选择
令人难以置信的是,目前我们仍然没有真正的解决方案.我有一个基于我上面的链接的替代解决方案.这种方法的缺点是假设你的背景是纯色(角落不是真的透明).你会像这样使用它:
<RoundedCornerLayout ...>
<ImageView ...>
</RoundedCornerLayout>
Run Code Online (Sandbox Code Playgroud)
这里的要点是完整的代码:
public class RoundedCornerLayout extends RelativeLayout {
private Bitmap maskBitmap;
private Paint paint;
private float cornerRadius;
public RoundedCornerLayout(Context context) {
super(context);
init(context, null, 0);
}
public RoundedCornerLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}
public RoundedCornerLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs, defStyle);
}
private void init(Context context, AttributeSet attrs, int defStyle) {
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
setWillNotDraw(false);
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
if (maskBitmap == null) {
// This corner radius assumes the image width == height and you want it to be circular
// Otherwise, customize the radius as needed
cornerRadius = canvas.getWidth() / 2;
maskBitmap = createMask(canvas.getWidth(), canvas.getHeight());
}
canvas.drawBitmap(maskBitmap, 0f, 0f, paint);
}
private Bitmap createMask(int width, int height) {
Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(mask);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE); // TODO set your background color as needed
canvas.drawRect(0, 0, width, height, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.drawRoundRect(new RectF(0, 0, width, height), cornerRadius, cornerRadius, paint);
return mask;
}
}
Run Code Online (Sandbox Code Playgroud)
现在在 Glide V4 中你可以直接使用 CircleCrop()
Glide.with(fragment)
.load(url)
.circleCrop()
.into(imageView);
Run Code Online (Sandbox Code Playgroud)
内置类型
根据这个答案,两种语言中最简单的方法是:
科特林:
Glide.with(context).load(uri).apply(RequestOptions().circleCrop()).into(imageView)
Run Code Online (Sandbox Code Playgroud)
爪哇:
Glide.with(context).load(uri).apply(new RequestOptions().circleCrop()).into(imageView)
Run Code Online (Sandbox Code Playgroud)
这适用于 Glide 4.XX
小智 5
使用此转换,它将正常工作.
public class CircleTransform extends BitmapTransformation {
public CircleTransform(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 borderColor = ColorUtils.setAlphaComponent(Color.WHITE, 0xFF);
int borderRadius = 3;
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
// TODO this could be acquired from the pool too
Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
if (squared != source) {
source.recycle();
}
Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
// Prepare the background
Paint paintBg = new Paint();
paintBg.setColor(borderColor);
paintBg.setAntiAlias(true);
// Draw the background circle
canvas.drawCircle(r, r, r, paintBg);
// Draw the image smaller than the background so a little border will be seen
canvas.drawCircle(r, r, r - borderRadius, paint);
squared.recycle();
return result;
}
@Override
public String getId() {
return getClass().getName();
}}
Run Code Online (Sandbox Code Playgroud)
对于Glide 4.xx
用
Glide
.with(context)
.load(uri)
.apply(
RequestOptions()
.circleCrop())
.into(imageView)
Run Code Online (Sandbox Code Playgroud)
从doc它说
圆形图片:CircleImageView / CircularImageView / RoundedImageView已知有问题与TransitionDrawable(.crossFade()与.thumbnail()或.placeholder())和GIF动画,使用 BitmapTransformation(.circleCrop()将在V4使用)或.dontAnimate() 来解决这个问题
小智 5
Roman Samoylenko 的回答是正确的,只是功能发生了变化。正确答案是
Glide.with(context)
.load(yourImage)
.apply(RequestOptions.circleCropTransform())
.into(imageView);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
134872 次 |
最近记录: |