开发者

Final variables in onDraw() method

开发者 https://www.devze.com 2023-03-18 01:16 出处:网络
I have a class that extends View and overrides the onDraw(Canvas canvas) method. This view runs animations, so onDraw will be called many times per second. Consider the following example...

I have a class that extends View and overrides the onDraw(Canvas canvas) method. This view runs animations, so onDraw will be called many times per second. Consider the following example...

@Override
protected void onDraw(Canvas canvas) {
  final int width = getWidth();
  final int height = getHeight();

  final int padLeft = getPaddingLeft();
  final int padTop = getPaddingTop();
  final int padRight = getPaddingRight();
  final int padBottom = getPaddingBottom();

  final RectF oval = new RectF(padLeft, padTop, width - padRight, height - padBottom);
  ...
}

Should I be worried that there are this many function calls happening each time onDraw is called? Does final tell the compiler that it doesn't need to call these functions each time? Would these variables be better off as member variables so that the functions ar开发者_如何学Ce only called once?

P.S. I know from running my program that performance is not affected. I am asking this question from a learning standpoint. It makes me feel better when I know exactly what I'm doing.


by saying final here you just saying that this local variable will not be changed in this function and functions will be called each time you call onDraw. If it is possible it's better to compose all this variables to another class like DisplayProperties and initialise it only once.

0

精彩评论

暂无评论...
验证码 换一张
取 消