Android 如何在类ASyncTask中操作映像而不阻塞UI线程?
我的代码如下:Android 如何在类ASyncTask中操作映像而不阻塞UI线程?,android,android-asynctask,Android,Android Asynctask,我的代码如下: private ImageView imageAvatar; private Drawable image; private BitmapDrawable drawable; protected Bitmap doInBackground(Void... params) { // TODO Auto-generated method stub String url = "https://graph.facebook.com/" + userId + "/pict
private ImageView imageAvatar;
private Drawable image;
private BitmapDrawable drawable;
protected Bitmap doInBackground(Void... params) {
// TODO Auto-generated method stub
String url = "https://graph.facebook.com/" + userId + "/picture?width=" + width + "&height=" + height;
image = ImageOperations(url);
imageAvatar.setImageDrawable(image);
imageAvatar.setMinimumHeight(32);
imageAvatar.setMinimumWidth(32);
imageAvatar.setMaxHeight(128);
imageAvatar.setMaxWidth(128);
IsFinish = true;
drawable = (BitmapDrawable) imageAvatar.getDrawable();
if(drawable != null) {
return drawable.getBitmap();
}
return null;
}
我通过taskImage.execute().get()
检索数据。我知道get()
将在必要时等待计算。我必须在onPostExecute()
上实现我的代码,以防止阻塞UI线程
问题是:如果我想在onPostExecute()
中操作代码以防止阻塞UI线程,结果不应该是Bitmap
,而是想从ASyncTask
中检索Bitmap
我应该怎么做?将
位图
结果存储在成员变量中?使用get()
,会阻塞UI线程,因此不能以这种方式执行。如果不知道要实现什么目标,很难帮助您。您需要将代码放入onPostExecute方法中,并在不调用get method的情况下启动ASyncTask。您的意思是我必须使用taskimage.execute()
,如何从ASyncTask
获取数据?@barssala:当后台执行完成时,您将以位图的形式在onPostExecute方法中获取数据,只需将代码移到onPostExecute中即可