简体   繁体   English

在Android上的onDraw之外更新视图的画布

[英]Update view's canvas outside of onDraw on Android

I need update a small portion of a custom view in order to display a small animation. 我需要更新自定义视图的一小部分以显示小动画。 The rest portion of the view has only static image. 视图的其余部分仅具有静态图像。 The most straightforward would be to obtain the canvas of the view and update only that particular portion directly. 最直接的方法是获取视图的画布,并仅直接更新特定部分。 But I can't really find anyway to get the view's canvas object outside of the view::onDraw method. 但是我还是找不到将视图的canvas对象移到view :: onDraw方法之外的方法。

The only alternative I know is this: call view::invalidate() with a specified rectangle to minimize the drawing flicker. 我知道的唯一替代方法是:使用指定的矩形调用view :: invalidate()以最大程度地减少图形闪烁。 I have the code to update the entire view within onDraw. 我有代码来更新onDraw中的整个视图。 So the best thing to do is to detect the clipping rect and only run the code to update the specified area, in order to minimize CPU usage as well? 因此,最好的办法是检测裁剪矩形并仅运行代码以更新指定的区域,以最大程度地减少CPU使用率?

I guess I will try to answer this question myself to the best my knowledge so far. 我想到目前为止,我将尽我所能尝试回答这个问题。

  1. There is no direct access to the canvas outside of the onDraw method. 在onDraw方法之外无法直接访问画布。
  2. Although we can detect the clipping rect with the function Canvas.getClipBounds(), the getClipBounds function always return the entire view area if GPU is enabled. 尽管我们可以使用Canvas.getClipBounds()函数检测到剪切矩形,但是如果启用了GPU,则getClipBounds函数始终返回整个视图区域。 When GPU is not used, getClipBounds() returns the actual dirty area. 不使用GPU时,getClipBounds()返回实际的脏区。 Since there is a GPU in most phones, it makes the function getClipBounds pretty much useless. 由于大多数手机中都有GPU,因此它使getClipBounds函数几乎毫无用处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM