gpt4 book ai didi

android - Canvas.getClipBounds 是否分配一个 Rect 对象?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:00:04 29 4
gpt4 key购买 nike

在我的自定义 View 中,我正在考虑使用 Canvas.getClipBounds()优化我的 onDraw 方法(这样我每次调用它时只绘制绝对必要的内容)。

但是,我仍然想绝对避免任何对象创建...

因此,我的问题是:getClipBounds() 是否在每次调用时都分配一个新的 Rect?或者它只是简单地回收单个 Rect?

如果它正在分配一个新对象,我可以通过使用 getClipBounds(Rect bounds) 来节省这笔费用吗? , 它似乎使用传递的 Rect 而不是它自己的?


(在你尖叫过早的优化之前,请考虑一下当放置在 ScrollView 中时,onDraw 可以每秒调用 很多 次)

最佳答案

我查看了Android 4.0.1的Canvas源码,如下:

/**
* Retrieve the clip bounds, returning true if they are non-empty.
*
* @param bounds Return the clip bounds here. If it is null, ignore it but
* still return true if the current clip is non-empty.
* @return true if the current clip is non-empty.
*/
public boolean getClipBounds(Rect bounds) {
return native_getClipBounds(mNativeCanvas, bounds);
}


/**
* Retrieve the clip bounds.
*
* @return the clip bounds, or [0, 0, 0, 0] if the clip is empty.
*/
public final Rect getClipBounds() {
Rect r = new Rect();
getClipBounds(r);
return r;
}

因此,回答您的问题,getClipBounds(Rect bounds) 将使您免于创建一个对象,但每次调用 getClipBounds() 时实际上都会创建一个新的 Rect() 对象。

关于android - Canvas.getClipBounds 是否分配一个 Rect 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10036672/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com