android.graphics.View无效(int,int,int,int)画布的一部分,但onRedraw()整个画布?

ite*_*ter 1 android android-canvas

Android View有三个版本invalidate():一个使整个视图无效,另外两个只使其中的一部分无效.但它只有一个onDraw(),它绘制整个画布.必须有一些使用系统的提示,我只想让视图的一部分无效,但我不清楚它是什么.

我有一个自定义绘图的视图onDraw().我有办法找出画布的哪些部分无效,所以我只画出那些?

Mic*_*use 5

当Android准备好向屏幕呈现更改时,它会通过创建需要重新绘制的所有单个矩形区域的联合(所有已经失效的区域)来实现.

调用视图的onDraw(Canvas画布)方法时,可以检查Canvas是否有剪辑边界.

如果存在非空剪辑边界,则可以使用此信息来确定您将不需要绘制的内容,从而节省时间.

如果剪辑边界为空,您应该假设Android要您绘制视图的整个区域.

像这样的东西:

private Rect clipBounds = new Rect();

@Override
protected void onDraw(Canvas canvas) 
{
    super.onDraw(canvas);

    boolean isClipped = canvas.getClipBounds(clipBounds);

    // If isClipped == false, assume you have to draw everything
    // If isClipped == true, check to see if the thing you are going to draw is within clipBounds, else don't draw it
}
Run Code Online (Sandbox Code Playgroud)