如何使用 Glide Library 获取位图,因为我想在 Canvas 上绘制该位图图像。 Glide 只允许我将图像添加到 ImageView。
有什么方法可以让我通过 Glide 获得精炼的位图,以便我可以将其绘制到 Canvas 上。
我目前正在使用
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
最佳答案
也许目标就是您所需要的。看看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);
关于android - 我可以使用 Glide 获得精炼/所需的位图以在 Canvas 上绘制吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46726613/