Avoid creating objects while calling onDraw. You can create new Paint(); in constructor.
The code below:
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
List<Float> floatList = getGraduateFloatList(stepNum, viewHeight);
generateAnim(floatList, barNum);
}
When the indicator inflates in a ListView/RecyclerView , onLayout will be called many times.Then there will be many animator objects being created. It will cause performance issue. You could do like this:
public Indicator(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override public boolean onPreDraw() {
if (0 != viewWidth && 0 != viewHeight) {
List floatList = getGraduateFloatList(stepNum, viewHeight);
generateAnim(floatList, barNum);
getViewTreeObserver().removeOnPreDrawListener(this);
}
return false;
}
});
new Paint();
in constructor.The code below:
When the indicator inflates in a ListView/RecyclerView , onLayout will be called many times.Then there will be many animator objects being created. It will cause performance issue. You could do like this:
public Indicator(Context context, @Nullable AttributeSet attrs) { super(context, attrs); getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { if (0 != viewWidth && 0 != viewHeight) { List floatList = getGraduateFloatList(stepNum, viewHeight);
generateAnim(floatList, barNum);
getViewTreeObserver().removeOnPreDrawListener(this);
}
return false;
}
});
}