2017-09-19 56 views
0

所以我有一个关于Glide的基本问题。我做一个android测试应用程序,只是想要转换图像以适应屏幕。Glide的真正基本的东西

这是旧代码:

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); 
} 

这并不适合屏幕最好的方法,我知道,我用这个和绘制-xhdpi/xxhdpi/xxxhdpi文件夹。

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

+0

滑翔提供覆盖的大小。阅读文档:https://futurestud.io/tutorials/glide-image-resizing-scaling –

+0

我不与ImageViews一起工作。 – csant85

+0

然后它有自定义的目标,调整工作方式相同。您可以添加像.into(SimpleTarget (宽度,高度){...}) –

回答

0

好吧,有不同的方法可以做,但容易的方法来保持该类的静态实例,因为当你试图分配给上层时,滑动保持为内部类。检查以下示例代码 公共类SomeActivity延伸活动{

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; 
        } 
       }); 
    } 

}

+0

代码尝试使用它绘制时,会在位图上引用空对象引用。 – csant85