Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trace: requestLayout() improperly called?

Can anyone tell me how to fix the following trace:

W/View    (16810): requestLayout() improperly called by 
theme.effects.TopCenterImageView{41dc73f0 V.ED.... ........ 
0,0-480,690 #7f060066 app:id/normal_image} during second 
layout pass: posting in next frame

Here is the code for TopCenterImageView:

public class TopCenterImageView extends ImageView {

public TopCenterImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setScaleType(ScaleType.MATRIX);
}

public TopCenterImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    setScaleType(ScaleType.MATRIX);
}

public TopCenterImageView(Context context) {
    super(context);
    setScaleType(ScaleType.MATRIX);
}

@Override
protected boolean setFrame(int l, int t, int r, int b) {
    if (getDrawable() == null) {
        return super.setFrame(l, t, r, b);
    }
    Matrix matrix = getImageMatrix();
    float scaleFactor = getWidth() / (float) getDrawable().getIntrinsicWidth();
    matrix.setScale(scaleFactor, scaleFactor, 0, 0);
    setImageMatrix(matrix);
    return super.setFrame(l, t, r, b);
}

}
like image 693
Aashir Avatar asked Dec 21 '13 08:12

Aashir


3 Answers

As seen here, setScaleType will call requestLayout, but the constructor of ImageView already call it before. So it will cause the layout to have multiple requestLayout called, one during the layout pass. It's just a warning because at a small scale, it's not a problem.

You will find some good research in this thread (not the roboguice part though).

like image 133
NitroG42 Avatar answered Nov 02 '22 14:11

NitroG42


I changed a child views layout params in the onLayout method AFTER calling super.onLayout(); That lead to a recursion:

Childlayout params changed -> parent view onRequestLayout() -> parent view onLayout -> childview params changed -> ...

like image 3
stoefln Avatar answered Nov 02 '22 16:11

stoefln


Use changed parameter as a change check.

override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
    super.onLayout(changed, left, top, right, bottom)
    Log.i(TAG, "onLayout: $changed")
    if (changed) {
        changeDefaultView(size)
    }
}

changed is equal to:

TRUE - when the layout CHANGED in comparison with the previous.

FALSE - when the layout DID NOT change in comparison with the previous.

like image 1
Владислав Шестернин Avatar answered Nov 02 '22 14:11

Владислав Шестернин