Glide真的很基本的东西

时间:2017-09-19 18:29:38

标签: android android-bitmap android-glide

所以我对Glide有一个基本的问题。我正在做一个Android测试应用程序,只想转换图像以适应屏幕。

这是旧代码:

Bitmap background, backgroundresized;

(...)

//load image
background = BitmapFactory.decodeResource(context.getResources(), R.drawable.gamebackground);

}

void onDraw(Canvas canvas) {

if(first_time){
width = canvas.getWidth();
height = canvas.getHeight();
backgroundresized = Bitmap.createScaledBitmap(background, width, height, true);
first_time=false;
}
canvas.drawBitmap(backgroundresized, 0, 0, null);
}

这不是适合屏幕的最佳方法,我知道,我使用这个和drawable-xhdpi / xxhdpi / xxxhdpi文件夹。

但是如何使用Glide来做到这一点,如果用户回到这个屏幕,希望回收图像?

1 个答案:

答案 0 :(得分:0)

好吧,有不同的方法可以做,但是保留该类的静态实例的简单方法是因为当你尝试分配给upper时,glide仍然是内部类。检查以下示例代码 公共类SomeActivity扩展了Activity {

    static Bitmap somebitmap;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int myWidth = 512;
        int myHeight = 384;

        Glide.with(this)
                .asBitmap()
                .load("URL")
                .into(new SimpleTarget<Bitmap>(myWidth, myHeight) {
                    @Override
                    public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                        somebitmap = resource;
                    }
                });
    }

}

相关问题