Taishi-Y / MusicIndicator

Music indicator for Android. Easy to use. 🎧 ✨
Apache License 2.0
492 stars 62 forks source link

Performance Issue #3

Open zyallday opened 6 years ago

zyallday commented 6 years ago
  1. Avoid creating objects while calling onDraw. You can create new Paint(); in constructor.
  2. 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; } });

}



Forgive my poor english.