必威体育Betway必威体育官网
当前位置:首页 > IT技术

java.lang.AbstractMethodError: abstract method "void com.bumptech.glide.load.Key.updateDiskCacheKe

时间:2019-08-25 00:41:05来源:IT技术作者:seo实验室小编阅读:82次「手机版」
 

abstractmethoderror

使用glide4.8加载网络地址实现高斯模糊  首先  导入glide

   implementation 'com.github.bumptech.glide:glide:4.8.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'

网上查资料发现Glide实现高斯模糊还要引入

    implementation 'jp.wasabeef:glide-transformations:2.0.1'

然后在项目中使用   根据根据网上的资料开始使用

GlideAPP.with(getActivity()).asBitmap().load(ownerUrl).apply(new requestOptions().transform(new BlurTransformation(getActivity(),23,4))).into(mIvGaussianBlur);

发现使用的时候报错  java.lang.AbstractMethodERROR: abstract method "void com.bumptech.glide.load.Key.updateDiskcacheKe

然后百度没找到想要的答案  翻墙搜了一下  找到一个答案 

自定义一个BlurTransformation类


import Android.content.context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.build;
import android.renderscript.RSruntimeexception;
import android.support.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

import java.nio.charset.Charset;
import java.security.messageDigest;

import jp.wasabeef.glide.transformations.internal.FastBlur;
import jp.wasabeef.glide.transformations.internal.RSBlur;

/**
 * Created by me on 2018-11-4.
 */

public class BlurTransformation extends BitmapTransformation {

    private static String STRING_CHARSET_NAME = "UTF-8";
    private static final String ID = "com.kevin.glidetest.BlurTransformation";
    private static Charset CHARSET = Charset.forName(STRING_CHARSET_NAME);
    private static final byte[] ID_BYTES = ID.getBytes(CHARSET);

    private static int MAX_RADIUS = 25;
    private static int DEFAULT_DOWN_SAMPLING = 1;

    private Context mContext;
    private BitmapPool mBitmapPool;

    private int mRadius;
    private int mSampling;

    public BlurTransformation(Context context) {
        this(context, Glide.get(context).getBitmapPool(), MAX_RADIUS, DEFAULT_DOWN_SAMPLING);
    }

    public BlurTransformation(Context context, BitmapPool pool) {
        this(context, pool, MAX_RADIUS, DEFAULT_DOWN_SAMPLING);
    }

    public BlurTransformation(Context context, BitmapPool pool, int radius) {
        this(context, pool, radius, DEFAULT_DOWN_SAMPLING);
    }

    public BlurTransformation(Context context, int radius) {
        this(context, Glide.get(context).getBitmapPool(), radius, DEFAULT_DOWN_SAMPLING);
    }

    public BlurTransformation(Context context, int radius, int sampling) {
        this(context, Glide.get(context).getBitmapPool(), radius, sampling);
    }

    public BlurTransformation(Context context, BitmapPool pool, int radius, int sampling) {
        mContext = context.getApplicationContext();
        mBitmapPool = pool;
        mRadius = radius;
        mSampling = sampling;
    }



    @Override
    protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int
            outWidth, int outHeight) {
        Bitmap source = toTransform;

        int width = source.getWidth();
        int height = source.getHeight();
        int scaledWidth = width / mSampling;
        int scaledHeight = height / mSampling;

        Bitmap bitmap = mBitmapPool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
        if (bitmap == null) {
            bitmap = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(bitmap);
        canvas.scale(1 / (float) mSampling, 1 / (float) mSampling);
        Paint paint = new Paint();
        paint.setFlags(Paint.FILTER_BITMAP_FLAG);
        canvas.drawbitmap(source, 0, 0, paint);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            try {
                bitmap = RSBlur.blur(mContext, bitmap, mRadius);
            } catch (RSRuntimeException e) {
                bitmap = FastBlur.blur(bitmap, mRadius, true);
            }
        } else {
            bitmap = FastBlur.blur(bitmap, mRadius, true);
        }

        //return BitmapResource.obtain(bitmap, mBitmapPool);
        return bitmap;
    }

    @Override
    public int hashCode() {
        return ID.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return obj instanceof BlurTransformation;
    }

    @Override
    public void updateDiskCacheKey(MessageDigest messageDigest) {
        messageDigest.update(ID_BYTES);
    }
}

替换掉原来jp.wasabeef:glide-transformations自带的BlurTransformation类就OK了   

大家使用的时候千万注意别导包导错了,用自定义的BlurTransformation类而不要用transformation库自带的

希望能给大家一点帮助,少踩一点坑

另外  使用Glide实现圆形图片效果   into()方法里面都是ImageView

另附效果图:

参考文章:https://github.com/wasabeef/glide-transformations/issues/73

相关阅读

分享到:

栏目导航

推荐阅读

热门阅读