2017-09-19 84 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