2017-10-13 41 views
0

如何使用Glide库获取位图,因为我想在画布上绘制该位图图像。 Glide只允许我将图像添加到ImageView。我可以使用Glide获取精致/想要的位图在画布上绘制吗?

有没有什么办法可以通过Glide获得精致的位图,以便我可以将其绘制到画布上。

目前我使用

Bitmap bitmapImage = BitmapFactory.decodeResource(getResources(), wallpaper_image_id); 

,我得到这个错误

java.lang.OutOfMemoryError: Failed to allocate a 43776012 byte allocation with 16767008 free bytes and 40MB until OOM 

回答

0

也许目标就是你所需要的。看看Here

Glide.with(yourApplicationContext)) 
    .load(youUrl) 
    .asBitmap() 
    .into(new SimpleTarget<Bitmap>(myWidth, myHeight) { 
     @Override 
     public void onResourceReady(Bitmap bitmap, GlideAnimation anim) { 
      // Do something with bitmap here. 
     } 
    }; 

里面的onResourceReady你已经准备好位图被加载,你想要的。

编辑

你的应用程序崩溃,因为你需要在UI线程中运行代码。试试这个:

// Get a handler that can be used to post to the main thread 
Handler mainHandler = new Handler(context.getMainLooper()); 

Runnable myRunnable = new Runnable() { 
    @Override 
    public void run() {....} // Your code goes inside the run method 
}; 
mainHandler.post(myRunnable); 
+0

谢谢@Samuele但现在给FATALException as。 'java.lang.IllegalArgumentException:你必须在主线程上调用这个方法' –

+0

好了,清楚的是,现在你在错误的线程中调用这个方法! ahahah 你在哪里调用代码?你应该从UI线程基本上调用它。 –

+0

从这里...'公共类SnowThread扩展线程{}'我已经创造了不断在我的壁纸上下雪。我从它的'run()'方法调用它。它需要在那里。 –