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

在之前使用了Glide进行图片的加载,现在一个产品需求为在一个界面上显示一个用户头像,而用户头像要显示为圆形。用户头像的URL地址是服务器返回的,所以我们要根据该URL进行Glide加载。有一种方法是在Glide加载结束之后再对图片进行处理,但其实Glide中有封装好的方法供我们调用。

首先我们需要在Glide之上引入一个开源项目:glide-transformations。glide-transformations在github上的项目主页是:https://github.com/wasabeef/glide-transformations

引入之后,直接使用下面的方法就可以进行调用。

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(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, GlideAnimationsuper 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是圆角的程度,该值越小圆角程度越小。

你可能感兴趣的:(android)