图片加载12:Glide之TransformationUtils

图片加载12:Glide之TransformationUtils,第1张

Glide已经为我们封装了TransformationUtils工具类,那么这个工具类到底有什么用呢?

图片.png

如图所示,这个工具类为我们提供了许多方便的接口:

[接口一]

Bitmap centerCrop(@NonNull BitmapPool pool, @NonNull Bitmap inBitmap, int width, int height)

代码如下:

    Glide.with(this)
            .asBitmap()
            .load(R.mipmap.chongge2)
            .diskCacheStrategy(DiskCacheStrategy.DATA)
            .transform(new BitmapTransformation() {
                @Override
                protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap source, int outWidth, int outHeight) {
                    return TransformationUtils.centerCrop(pool, source, outWidth, outHeight);
                }

                @Override
                public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {

                }
            })
            .into(image_view);

我们可以看到,图片的转换 *** 作只有一句话而已:

TransformationUtils.centerCrop(pool, source, outWidth, outHeight);

centerCrop接口类似于

    android:scaleType="centerCrop"

好吧,我承认,这个接口意义不大。

[接口二]

Bitmap centerInside(@NonNull BitmapPool pool, @NonNull Bitmap inBitmap, int width, int height)

这个接口相当于

    android:scaleType="centerInside"

[接口三]

Bitmap fitCenter(@NonNull BitmapPool pool, @NonNull Bitmap inBitmap, int width, int height)

这个接口相当于

    android:scaleType="fitCenter"

[接口四] 圆形图片

Bitmap circleCrop(@NonNull BitmapPool pool, @NonNull Bitmap inBitmap, int destWidth, int destHeight)

实现圆形centerCrop效果,效果如下:

图片.png

[接口五] 旋转图片

Bitmap rotateImage(@NonNull Bitmap imageToOrient, int degreesToRotate)

 

TransformationUtils.rotateImage(Bitmap.createScaledBitmap(source, source.getWidth() / 2, source.getHeight() / 2, false), 30);

效果如下:

图片.png

[接口六] 旋转或反转图片

Bitmap rotateImageExif(@NonNull BitmapPool pool, @NonNull Bitmap inBitmap, int exifOrientation)

其中,exifOrientation代表方向,取值范围是1~8

取值为1时效果如下:

图片.png

取值为2时的效果如下:

图片.png

取值为3时的效果如下:

图片.png

取值为4时的效果如下:

图片.png

取值为5时的效果如下:

图片.png

取值为6时的效果如下:

图片.png

取值为7时的效果如下:

图片.png

取值为8时的效果如下:

图片.png

[接口七] 圆角图片

BitmapPool pool, @NonNull Bitmap inBitmap, int roundingRadius)

其中,第三个参数为圆角半径。

效果如下:

图片.png

[接口八] 设置透明度的可用性

 setAlpha(Bitmap inBitmap, Bitmap outBitmap)

以上并不是设置透明度,而是设置透明度的是否可用。

其源码如下:

    /**
     * Sets the alpha of the Bitmap we're going to re-use to the alpha of the Bitmap we're going to
     * transform. This keeps {@link android.graphics.Bitmap#hasAlpha()}} consistent before and after
     * the transformation for transformations that don't add or remove transparent pixels.
     *
     * @param inBitmap The {@link android.graphics.Bitmap} that will be transformed.
     * @param outBitmap   The {@link android.graphics.Bitmap} that will be returned from the
     *                    transformation.
     */
    public static void setAlpha(Bitmap inBitmap, Bitmap outBitmap) {
        outBitmap.setHasAlpha(inBitmap.hasAlpha());
    }

也就是说,TransformationUtils.setAlpha(inBitmap, outBitmap)其实是调用了outBitmap.setHasAlpha(inBitmap.hasAlpha()),首先获取inBitmap是否含有alpha(true or false),并将这个标志设置到outBitmap中。

如下代码:

Bitmap.createBitmap(pixels, source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);

当我们创建一个新的Bitmap时,最后一个参数就是设置Bitmap的配置,就拿Bitmap.Config.RGB_565Bitmap.Config.ARGB_8888来说吧,前者没有alpha后者有alpha,所以如果调用以下代码:

outBitmap.hasAlpha()

的话,前者返回值为true,后者返回值为false。

当然,如果我们需要在已有bitmap上变换bitmap的话,我们新建一个bitmap一般都是这样的:

Bitmap outBitmap = Bitmap.createBitmap(pixels, inBitmap.getWidth(), inBitmap.getHeight(), inBitmap.getConfig());

你们看,以上的创建方式不就等于:

TransformationUtils.setAlpha(inBitmap, outBitmap)

吗。

所以,TransformationUtils.setAlpha(inBitmap, outBitmap)其实用处不是很大,这里做过多描述也没有任何意义了。

如果Bitmap想要设置透明度的话,具体实现代码如下:

    Glide.with(this)
            .asBitmap()
            .load(R.mipmap.chongge2)
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .transform(new BitmapTransformation() {
                @Override
                protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap source, int outWidth, int outHeight) {

                    //像素数组
                    int[] pixels = new int[source.getWidth() * source.getHeight()];
                    //int[] pixels, int offset, int stride, int x, int y, int width, int height
                    source.getPixels(pixels, 0, source.getWidth(), 0, 0, source.getWidth(), source.getHeight());

                    int number = 50 * 255 / 100;

                    for(int i=0;i

效果如下:

图片.png

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/926444.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-16
下一篇2022-05-16

发表评论

登录后才能评论

评论列表(0条)