gpt4 book ai didi

java - Android缓存实例

转载 作者:行者123 更新时间:2023-11-30 09:32:20 24 4
gpt4 key购买 nike

View 的性能关键 onDraw() 方法中,将实例存储在变量中并稍后重用会更好,还是我应该只引用我的实例作为这个?

例如:

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();
}

}

thisthis.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

最佳答案

不,那不会有任何改善。

您可以从您的代码中做出的最大改进是将对您的绘画对象的调用移动到 View 的构造函数:

public ScheduleListView(Context context, AttributeSet attrs) {
super(context, attrs);
paint.setColor(Color.GRAY);
paint.setAlpha(100);
paint.setColor(Color.parseColor("#47B3EA"));

}

@Override
protected void onDraw(Canvas canvas) {
canvas.drawRect(this.getLeft(), 10, this.getRight(), 10, paint);
super.onDraw(canvas);
canvas.restore();
}

关于java - Android缓存实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12552009/

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