为什么 Android 地图指南针演示使用 SmoothCanvas 类的委托?
在 Eclipse 中针对 Google API 提供的 Android MapsDemo 中,他们在 MapViewCompassDemo.java 中创建了一个内部类 SmoothCanvas
。在此类中,重新实现了几乎所有方法,并将其重新路由到 Canvas 的委托实例。
static final class SmoothCanvas extends Canvas {
Canvas delegate;
private final Paint mSmooth = new Paint(Paint.FILTER_BITMAP_FLAG);
public void setBitmap(Bitmap bitmap) {
delegate.setBitmap(bitmap);
}
public void setViewport(int width, int height) {
delegate.setViewport(width, height);
}
...
在这种情况下,委托的意义何在?
In the Android MapsDemo available in Eclipse for the Google API, they create an inner class SmoothCanvas
in MapViewCompassDemo.java. Within this class the re-implement seemingly every method and reroute it to a delegate instance of Canvas
.
static final class SmoothCanvas extends Canvas {
Canvas delegate;
private final Paint mSmooth = new Paint(Paint.FILTER_BITMAP_FLAG);
public void setBitmap(Bitmap bitmap) {
delegate.setBitmap(bitmap);
}
public void setViewport(int width, int height) {
delegate.setViewport(width, height);
}
...
What is the point of the delegate in this instance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
delegate
的值被传递给dispatchDraw
。SmoothCanvas
类是delegate
的包装器。通过委托,传递给dispatchDraw
的Canvas
实现会完成所有繁重的工作。包装器只允许注入平滑的油漆,而不实现 Canvas 的所有逻辑。The value of
delegate
is passed in todispatchDraw
. TheSmoothCanvas
class is a wrapper arounddelegate
. By delegating, theCanvas
implementation passed todispatchDraw
does all the heavy lifting. The wrapper just allows the injection of the smoothed paint without implementing all the logic ofCanvas
.在这种情况下委托的关键点是:
通过激活该标志,绘制位图基本上会提高其性能。在示例中,您将在每个
drawBitmap
调用中使用mSmoth
。The key point of delegating in that instance is:
By activating that flag, drawing bitmaps will basically increase its performance. You will in the example that
mSmoth
is used in everydrawBitmap
call.