为什么我需要所有这些空方法来为 Glide 创建自定义文件目标?
Why do I need all these empty methods to create a custom file target for Glide?
我正在使用 Glide 调整图像大小并将其保存到 Android 应用程序中的文件系统,并且我创建了一个扩展 Glide 本机的自定义 FileTarget
class Target
去做。
有效。耶!但是我对我必须 'define' 来满足 Target
实现的所有空方法感到有点震惊。我真正关心的是 onResourceReady
...
有没有办法在不声明所有这些空方法的情况下实现这个?我应该担心他们都是这样吗?
我对 Android 有点陌生,感谢您的耐心等待。
public static class FileTarget<T extends Bitmap> implements Target<Bitmap> {
private final int width;
private final int height;
String fileName;
Bitmap.CompressFormat format;
int quality;
public FileTarget(int width, int height, String fileName, Bitmap.CompressFormat format, int quality) {
this.width = width;
this.height = height;
this.fileName = fileName;
this.format = format;
this.quality = quality;
}
@Override
public void onLoadStarted(Drawable drawable) {}
@Override
public void onLoadFailed(Drawable drawable) {}
@Override
public void onLoadCleared(Drawable drawable) {}
@Override
public void getSize(SizeReadyCallback cb) {
cb.onSizeReady(width, height);
}
@Override
public void removeCallback(@NonNull SizeReadyCallback cb) {}
@Override
public void setRequest(@Nullable Request request) {}
@Nullable
@Override
public Request getRequest() {
return null;
}
@Override
public void onStart() {}
@Override
public void onStop() {}
@Override
public void onDestroy() {}
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
try {
FileOutputStream out = new FileOutputStream(fileName);
resource.compress(format, quality, out);
out.flush();
out.close();
onFileSaved();
} catch (IOException e) {
e.printStackTrace();
onSaveException(e);
}
}
public void onFileSaved() {
// do nothing, should be overriden
}
public void onSaveException(Exception e) {
// do nothing, should be overriden
}
}
在 Android 框架中,有一个常见的模式,即创建抽象 FooAdapter
class 实现 Foo
接口中的所有方法 no-ops.
例如,考虑这个 class:
public abstract class AnimatorListenerAdapter implements Animator.AnimatorListener,
Animator.AnimatorPauseListener {
@Override
public void onAnimationCancel(Animator animation) { }
@Override
public void onAnimationEnd(Animator animation) { }
@Override
public void onAnimationRepeat(Animator animation) { }
@Override
public void onAnimationStart(Animator animation) { }
@Override
public void onAnimationPause(Animator animation) { }
@Override
public void onAnimationResume(Animator animation) { }
}
您可以为 Glide 创建相同的东西:
public abstract class TargetAdapter<T> implements Target<T> {
@Override
public void onLoadStarted(Drawable drawable) {}
@Override
public void onLoadFailed(Drawable drawable) {}
@Override
public void onLoadCleared(Drawable drawable) {}
@Override
public void getSize(SizeReadyCallback cb) {}
@Override
public void removeCallback(@NonNull SizeReadyCallback cb) {}
@Override
public void setRequest(@Nullable Request request) {}
@Override
public Request getRequest() {}
@Override
public void onResourceReady(@NonNull T resource, @Nullable Transition<? super T> transition) {}
}
现在,当您创建 FileTarget
class 时,您可以声明它 extends TargetAdapter
而不是 implements Target
,并且只覆盖您关心的方法:
public static class FileTarget<T extends Bitmap> extends TargetAdapter<Bitmap> {
// ...
@Override
public void getSize(SizeReadyCallback cb) {
cb.onSizeReady(width, height);
}
@Nullable
@Override
public Request getRequest() {
return null;
}
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
try {
FileOutputStream out = new FileOutputStream(fileName);
resource.compress(format, quality, out);
out.flush();
out.close();
onFileSaved();
} catch (IOException e) {
e.printStackTrace();
onSaveException(e);
}
}
// ...
}
我正在使用 Glide 调整图像大小并将其保存到 Android 应用程序中的文件系统,并且我创建了一个扩展 Glide 本机的自定义 FileTarget
class Target
去做。
有效。耶!但是我对我必须 'define' 来满足 Target
实现的所有空方法感到有点震惊。我真正关心的是 onResourceReady
...
有没有办法在不声明所有这些空方法的情况下实现这个?我应该担心他们都是这样吗?
我对 Android 有点陌生,感谢您的耐心等待。
public static class FileTarget<T extends Bitmap> implements Target<Bitmap> {
private final int width;
private final int height;
String fileName;
Bitmap.CompressFormat format;
int quality;
public FileTarget(int width, int height, String fileName, Bitmap.CompressFormat format, int quality) {
this.width = width;
this.height = height;
this.fileName = fileName;
this.format = format;
this.quality = quality;
}
@Override
public void onLoadStarted(Drawable drawable) {}
@Override
public void onLoadFailed(Drawable drawable) {}
@Override
public void onLoadCleared(Drawable drawable) {}
@Override
public void getSize(SizeReadyCallback cb) {
cb.onSizeReady(width, height);
}
@Override
public void removeCallback(@NonNull SizeReadyCallback cb) {}
@Override
public void setRequest(@Nullable Request request) {}
@Nullable
@Override
public Request getRequest() {
return null;
}
@Override
public void onStart() {}
@Override
public void onStop() {}
@Override
public void onDestroy() {}
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
try {
FileOutputStream out = new FileOutputStream(fileName);
resource.compress(format, quality, out);
out.flush();
out.close();
onFileSaved();
} catch (IOException e) {
e.printStackTrace();
onSaveException(e);
}
}
public void onFileSaved() {
// do nothing, should be overriden
}
public void onSaveException(Exception e) {
// do nothing, should be overriden
}
}
在 Android 框架中,有一个常见的模式,即创建抽象 FooAdapter
class 实现 Foo
接口中的所有方法 no-ops.
例如,考虑这个 class:
public abstract class AnimatorListenerAdapter implements Animator.AnimatorListener,
Animator.AnimatorPauseListener {
@Override
public void onAnimationCancel(Animator animation) { }
@Override
public void onAnimationEnd(Animator animation) { }
@Override
public void onAnimationRepeat(Animator animation) { }
@Override
public void onAnimationStart(Animator animation) { }
@Override
public void onAnimationPause(Animator animation) { }
@Override
public void onAnimationResume(Animator animation) { }
}
您可以为 Glide 创建相同的东西:
public abstract class TargetAdapter<T> implements Target<T> {
@Override
public void onLoadStarted(Drawable drawable) {}
@Override
public void onLoadFailed(Drawable drawable) {}
@Override
public void onLoadCleared(Drawable drawable) {}
@Override
public void getSize(SizeReadyCallback cb) {}
@Override
public void removeCallback(@NonNull SizeReadyCallback cb) {}
@Override
public void setRequest(@Nullable Request request) {}
@Override
public Request getRequest() {}
@Override
public void onResourceReady(@NonNull T resource, @Nullable Transition<? super T> transition) {}
}
现在,当您创建 FileTarget
class 时,您可以声明它 extends TargetAdapter
而不是 implements Target
,并且只覆盖您关心的方法:
public static class FileTarget<T extends Bitmap> extends TargetAdapter<Bitmap> {
// ...
@Override
public void getSize(SizeReadyCallback cb) {
cb.onSizeReady(width, height);
}
@Nullable
@Override
public Request getRequest() {
return null;
}
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
try {
FileOutputStream out = new FileOutputStream(fileName);
resource.compress(format, quality, out);
out.flush();
out.close();
onFileSaved();
} catch (IOException e) {
e.printStackTrace();
onSaveException(e);
}
}
// ...
}