在 a 的性能关键onDraw()
方法中View
,将实例存储在变量中并稍后重用它会更好,还是应该将我的实例称为this
?
前任:
public class ScheduleListView extends ListView {
private Paint paint = new Paint();
public ScheduleListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
paint.setColor(Color.GRAY);
paint.setAlpha(100);
paint.setColor(Color.parseColor("#47B3EA"));
canvas.drawRect(this.getLeft(), 10, this.getRight(), 10, paint);
super.onDraw(canvas);
canvas.restore();
}
}
和this
中提到的this.getRight()
,this.getLeft()
如果我进行以下修改,我是否会有任何性能改进:
private ScheduleListView scheduleListView = this;
... ...
@Override
protected void onDraw(Canvas canvas) {
....
//refer to my instance using scheduleListView instead of this
scheduleListView.getLeft()
....
}
这是好习惯吗?这会有任何性能提升吗?如果没有,这样做的更好方法是什么?
PS:我问这个是因为在 jQuery(javascript) 中,如果我要多次使用一个对象,我会将对象缓存在一个变量中,真的不知道这对 Java 是否有帮助......
前任:
var myDiv = $(".myDiv");
myDiv.html();//do stuff