如何防止 MaterialDialog 冻结
How to prevent MaterialDialog freezing
我正在尝试使用 Glide 下载图片,并将其设置为墙纸,在下载时,它会显示一个使用 MaterialDialogs 库的对话框,但不知何故,当图片快要加载时,对话框会冻结一秒钟...这是我的代码:
final MaterialDialog downloadDialogA = new MaterialDialog.Builder(context)
.content(R.string.downloading_wallpaper)
.progress(true, 0)
.cancelable(false)
.show();
Glide.with(context)
.load(wallurl)
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
downloadDialogA.setContent(context.getString(R.string.setting_wall_title));
if (resource != null) {
WallpaperManager wm = WallpaperManager.getInstance(context);
try {
wm.setBitmap(resource);
Toast.makeText(context, R.string.set_as_wall_done, Toast.LENGTH_LONG).show();
Log.v("Wall", "It worked!");
} catch (IOException e2) {
Toast.makeText(context, e2.getLocalizedMessage(), Toast.LENGTH_LONG).show();
Log.v("Wall", "Error " + e2.getMessage());
}
}
downloadDialogA.dismiss();
}
});
请注意,我不想使用其他库来下载图片。它工作正常。我只是想防止对话框冻结。
提前致谢。
我就是这样做的。
我创建了一个新的 AsyncTask,代码是:
public class ApplyWallpaper extends AsyncTask<Void, String, Boolean> {
private Context context;
private Activity activity;
private MaterialDialog dialog;
private Bitmap resource;
public ApplyWallpaper(Context context, MaterialDialog dialog, Bitmap resource) {
this.activity = (Activity) context;
this.context = context;
this.dialog = dialog;
this.resource = resource;
}
@Override
protected Boolean doInBackground(Void... params) {
WallpaperManager wm = WallpaperManager.getInstance(context);
Boolean worked;
try {
wm.setBitmap(resource);
worked = true;
Log.e("Wall", "It worked!");
} catch (IOException e2) {
worked = false;
Log.e("Wall", "Error " + e2.getMessage());
}
return worked;
}
@Override
protected void onPostExecute(Boolean worked) {
if (worked){
dialog.dismiss();
Toast.makeText(context, R.string.set_as_wall_done, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, R.string.error, Toast.LENGTH_LONG).show();
}
}
}
然后,当资源就绪时(使用Glide提供的回调方法),我执行了AsyncTask,像这样:
Glide.with(context)
.load(wallurl)
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(final Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
if (resource != null) {
downloadDialogA.setContent(context.getString(R.string.setting_wall_title));
new ApplyWallpaper(context, downloadDialogA, resource).execute();
}
}
});
并且工作完美。
感谢Javier Santos的指导。
我正在尝试使用 Glide 下载图片,并将其设置为墙纸,在下载时,它会显示一个使用 MaterialDialogs 库的对话框,但不知何故,当图片快要加载时,对话框会冻结一秒钟...这是我的代码:
final MaterialDialog downloadDialogA = new MaterialDialog.Builder(context)
.content(R.string.downloading_wallpaper)
.progress(true, 0)
.cancelable(false)
.show();
Glide.with(context)
.load(wallurl)
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
downloadDialogA.setContent(context.getString(R.string.setting_wall_title));
if (resource != null) {
WallpaperManager wm = WallpaperManager.getInstance(context);
try {
wm.setBitmap(resource);
Toast.makeText(context, R.string.set_as_wall_done, Toast.LENGTH_LONG).show();
Log.v("Wall", "It worked!");
} catch (IOException e2) {
Toast.makeText(context, e2.getLocalizedMessage(), Toast.LENGTH_LONG).show();
Log.v("Wall", "Error " + e2.getMessage());
}
}
downloadDialogA.dismiss();
}
});
请注意,我不想使用其他库来下载图片。它工作正常。我只是想防止对话框冻结。
提前致谢。
我就是这样做的。
我创建了一个新的 AsyncTask,代码是:
public class ApplyWallpaper extends AsyncTask<Void, String, Boolean> {
private Context context;
private Activity activity;
private MaterialDialog dialog;
private Bitmap resource;
public ApplyWallpaper(Context context, MaterialDialog dialog, Bitmap resource) {
this.activity = (Activity) context;
this.context = context;
this.dialog = dialog;
this.resource = resource;
}
@Override
protected Boolean doInBackground(Void... params) {
WallpaperManager wm = WallpaperManager.getInstance(context);
Boolean worked;
try {
wm.setBitmap(resource);
worked = true;
Log.e("Wall", "It worked!");
} catch (IOException e2) {
worked = false;
Log.e("Wall", "Error " + e2.getMessage());
}
return worked;
}
@Override
protected void onPostExecute(Boolean worked) {
if (worked){
dialog.dismiss();
Toast.makeText(context, R.string.set_as_wall_done, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, R.string.error, Toast.LENGTH_LONG).show();
}
}
}
然后,当资源就绪时(使用Glide提供的回调方法),我执行了AsyncTask,像这样:
Glide.with(context)
.load(wallurl)
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(final Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
if (resource != null) {
downloadDialogA.setContent(context.getString(R.string.setting_wall_title));
new ApplyWallpaper(context, downloadDialogA, resource).execute();
}
}
});
并且工作完美。
感谢Javier Santos的指导。