# Android component animatable on its height

I'd like to have a code review for a component which is supposed to be animatable on its height. It's supposed to be a transitioning element. I find it quite laggy on my phone.

private void init(AttributeSet attrs, int defStyle) {
final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.GuillotineView, defStyle, 0);

a.recycle();

this.paint = new Paint();
this.paint.setFlags(Paint.ANTI_ALIAS_FLAG);
}

this.onSizeChanged(getWidth(), getHeight(), getWidth(), getHeight());
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);

if (getWidth() <= 0 || (getHeight()- Math.round(shadowHeight) <= 0)) {
return;
}

this.path = new Path();

}

// TODO optimize it: it should not have this much computation in the draw method
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
canvas.drawPath(this.path, paint);
}


This component is used as a shared element within two fragments. its height inside both fragment is different so I can have a transitionning animation.

private void prepareTransitionGuillotine(Fragment fragmentFromTransition, Fragment fragment) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ChangeBounds guillotineTransition = new ChangeBounds();
guillotineTransition.setInterpolator(new OvershootInterpolator(0.5f));
guillotineTransition.setDuration(700);
fragment.setSharedElementEnterTransition(guillotineTransition);
fragment.setSharedElementReturnTransition(guillotineTransition);

transition.setDuration(300);
transition.setInterpolator(new DecelerateInterpolator());
fragment.setEnterTransition(transition);
fragmentFromTransition.setExitTransition(transition);
}
}


It's used like this when pushing a fragment on the screen

prepareTransitionGuillotine(currentFragment, fragment);

• the draw method is quite efficent there is no need to optimize on that part... how do you know that the onDraw(Canvas canvas) method is the problem? – Martin Frank Feb 13 '18 at 10:15
• onDraw is called only once, when the Activity becomes visible or if you explicitly call invalidate(). so i guess your Activity is somewhere else forced to be redrawn. Can you show us, where you control the drawing? – Martin Frank Feb 13 '18 at 10:30