使用Glide加载图片时转换为圆形等其他效果

package zhangphil.app;  

import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;  
import android.widget.ImageView;  

import com.bumptech.glide.Glide;  

import jp.wasabeef.glide.transformations.BlurTransformation;  
import jp.wasabeef.glide.transformations.CropCircleTransformation;  
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;  

public class MainActivity extends AppCompatActivity {  

    //我csdn博客头像 
    String url = "your img's URL";  

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  

        //原图
        ImageView image1 = (ImageView) findViewById(R.id.image1);  
        Glide.with(this).load(url).crossFade(1000).into(image1);  

        //原图 -> 圆图 
        ImageView image2 = (ImageView) findViewById(R.id.image2);  
        Glide.with(this).load(url).bitmapTransform(new CropCircleTransformation(this)).crossFade(1000).into(image2);  

        //原图的毛玻璃、高斯模糊效果 
        ImageView image3 = (ImageView) findViewById(R.id.image3);  
        Glide.with(this).load(url).bitmapTransform(new BlurTransformation(this, 25)).crossFade(1000).into(image3);  

        //原图基础上复合变换成圆图 +毛玻璃(高斯模糊) 
        ImageView image4 = (ImageView) findViewById(R.id.image4);  
        Glide.with(this).load(url).bitmapTransform(new BlurTransformation(this, 25), new CropCircleTransformation(this)).crossFade(1000).into(image4);  

        //原图处理成圆角,如果是四周都是圆角则是RoundedCornersTransformation.CornerType.ALL 
        ImageView image5 = (ImageView) findViewById(R.id.image5);  
        Glide.with(this).load(url).bitmapTransform(new RoundedCornersTransformation(this, 30, 0, RoundedCornersTransformation.CornerType.BOTTOM)).crossFade(1000).into(image5);  
    }  
}  

还有一种方法是在程序中自己创建一个Glide的加载图片的帮助类,用来把图片圆角,以下为我在追影APP开发使用的方法,下面贴出GlideRoundTransform帮助类。

public class GlideRoundTransform extends BitmapTransformation {

    private static float radius = 0f;

    public GlideRoundTransform(Context context) {
        this(context, 4);
    }

    public GlideRoundTransform(Context context, int dp) {
        super(context);
        this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
    }

    @Override 
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        return roundCrop(pool, toTransform);
    }

    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 String getId() {
        return getClass().getName() + Math.round(radius);
    }
}

然后在使用Glide加载的地方直接按以下方法调用:

public void loadCardRoundedImage(Activity activity, ImageView iv, final View loadingView, final String url, final boolean isShowLoadWhenStarted) {
    Glide.with(activity)
            .load(url)
            .dontAnimate()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .transform(new GlideRoundTransform(context, 50))
            .into(new ImageViewTarget<GlideDrawable>(iv) {

                @Override
                public void onLoadStarted(Drawable placeholder) {
                    super.onLoadStarted(placeholder);
                    if (isShowLoadWhenStarted && loadingView != null) {
                        loadingView.setVisibility(View.VISIBLE);
                    }
                }

                @Override
                public void onLoadFailed(Exception e, Drawable errorDrawable) {
                    super.onLoadFailed(e, errorDrawable);
                    if (loadingView != null) {
                        loadingView.setVisibility(View.GONE);
                    }
                }

                @Override
                public void onLoadCleared(Drawable placeholder) {
                    super.onLoadCleared(placeholder);
                }

                @Override
                public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                    super.onResourceReady(resource, glideAnimation);
                    if (loadingView != null) {
                        loadingView.setVisibility(View.GONE);
                    }
                }

                @Override
                protected void setResource(GlideDrawable resource) {
                    getView().setImageDrawable(resource);
                }
            });
}

其中的.transform(new GlideRoundTransform(context, 50))是关键所在,调用了刚才创建好的帮助类方法,参数中给定的50是圆角的程度,该值越小圆角程度越小。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

春哥111

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值