I have created a custom MyAspectButton that keeps the aspect:
public class MyAspectButton extends Button
{
private float m_aspect = -1.f;
// ...
// ... Constructors, setters/getters ...
// ...
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
if (aspect >= 0.f)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int measuredWidth = getMeasuredWidth();
widthMeasureSpec = MeasureSpec.makeMeasureSpec(measuredWidth, MeasureSpec.EXACTLY);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(Math.round(measuredWidth * aspect), MeasureSpec.EXACTLY);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
Now I want to have MyAspectLinearLayout, MyAspectRelativeLayout, and so on. Their onMeasure method will be the same.
How can I implement a bunch of these classes with as little copy-paste as possible?
I am aware of a concept of generic classes in Java, but here I would have to inherit from template parameter that compiler does not let me to do:
public class MyAspectWidget<T> extends T
{
// ...
}
does not compile.
onMeasureAbstractLayoutButton extends Button,SpecificLayoutButton extends AbstractLayoutButton. Explain to me why this won't work?AbstractLayoutButton,AbstractLinearLayout,AbstractRelativeLayouthe will have to copy-paste the same measurement code.ButtonandLayoutare not similar classes, are they?