onMeasure()

Android 2015. 4. 15. 17:53


http://developer.android.com/reference/android/view/View.html

Called to determine the size requirements for this view and all of its children.


View의 onMeasure() 는 onLayout() 과 함께 레이아웃을 결정짓는 메소드로,

각각

onMeasure() - 사이즈

onLayout() - 위치 좌표

를 결정한다.


@ onMeasure()이 호출되는 경우

layout이 최초로 만들어질 때.

layout이 변경될 때.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if(mVideoWidth <= 0 || mVideoHeight <= 0) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        return;
    }
 
    float heightRatio = (float) mVideoHeight / (float) getHeight();
    float widthRatio = (float) mVideoWidth / (float) getWidth();
 
    int scaledHeight;
    int scaledWidth;
 
    if (heightRatio > widthRatio) {
        scaledHeight = (int) Math.ceil((float) mVideoHeight
                / heightRatio);
        scaledWidth = (int) Math.ceil((float) mVideoWidth
                / heightRatio);
    } else {
        scaledHeight = (int) Math.ceil((float) mVideoHeight
                / widthRatio);
        scaledWidth = (int) Math.ceil((float) mVideoWidth
                / widthRatio);
    }
 
    setMeasuredDimension(scaledWidth, scaledHeight);
}
cs



http://i5on9i.blogspot.kr/2013/05/android-view-onmeasure-onlayout.html

: