如何从 Android 应用程序中的异步任务返回位图

编程入门 行业动态 更新时间:2024-10-07 02:27:49
本文介绍了如何从 Android 应用程序中的异步任务返回位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

好的,所以这段代码就在 Android 开发者网站上,它将 ImageView 设置为 Bitmap:

Ok, so this code is right off the Android Developer site which sets an ImageView to a Bitmap:

class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> { private final WeakReference<ImageView> imageViewReference; private int data = 0; public BitmapWorkerTask(ImageView imageView) { // Use a WeakReference to ensure the ImageView can be garbage collected imageViewReference = new WeakReference<ImageView>(imageView); } // Decode image in background. @Override protected Bitmap doInBackground(Integer... params) { data = params[0]; return decodeSampledBitmapFromResource(getResources(), data, 100, 100)); } // Once complete, see if ImageView is still around and set bitmap. @Override protected void onPostExecute(Bitmap bitmap) { if (imageViewReference != null && bitmap != null) { final ImageView imageView = imageViewReference.get(); if (imageView != null) { imageView.setImageBitmap(bitmap); } } } }

基本上,我想做的不是做

And basically, what I want to do is instead of doing

imageView.setImageBitmap(bitmap); 我想返回那个 Bitmap 以便我可以在我的主 UI 中使用它.有什么办法可以做到这一点吗?对不起,我对 AsyncTask 缺乏了解,我最近才开始学习它.谢谢!

imageView.setImageBitmap(bitmap); I want to return that Bitmap so I can then use it in my Main UI. Is there any way of going about doing this? Sorry about my lack of knowledge on AsyncTask, I just started learning it recently. Thank you!

推荐答案

其实不需要返回位图,而是在主Ui线程中使用.您可以在 PostExecute 上执行此操作,因为 postExecute 在 UI 线程中运行.你的问题??是的,你可以从 AsynTask 返回位图,你可以这样做

Actually there is no need to return bitmap and than use it in main Ui Thread. You can do this on PostExecute becuase postExecute runs in UI Thread. Your question?? Yes you can return bitmap from AsynTask you can do something like this

private class myTask extends AsyncTask<Void,Void,Bitmap>{ protected Bitmap doInBackground(Void... params) { //do stuff return bitmap; } @Override protected void onPostExecute(Bitmap result) { //do stuff } }

现在你可以通过调用来获取位图

Now you can get bitmap by calling

Bitmap returned_bitmap = new myTask().execute().get()

再一次这样不好,这会挂起你的 UI.但是你可以像这样从 Aysnc 获得价值.

Again this is not good this will hang your UI. But you can get value from Aysnc like this.

另一种方法是使用接口实现回调

public interface MyInterface { public void onPostExecute(); }

活动类

public class MyActivity extends Activity implements MyInterface{ private Bitmap Image; public void onCreate(Bundle b) { super.onCreate(b); new MyTask(this).execute(); } @Override public void onPostExecute() { //Now you have your bitmap update by do in background //do stuff here } private class MyTask extends AsyncTask<Void, Void, Void> { MyInterface myinterface; MyTask(MyInterface mi) { myinterface = mi; } @Override protected Void doInBackground(Void... params) { //firt declare bitmap as class member of MyActivity //update bitmap here and then call myinterface.onPostExecute(); return null; } } }

更多推荐

如何从 Android 应用程序中的异步任务返回位图

本文发布于:2023-11-05 10:30:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560590.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位图   应用程序   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!