Android 에서 getWidth()와 getMeasured Width()의 차 이 를 분석 합 니 다.
2272 단어 getWidth()getMeasuredWidth()
1.getMeasuredWidth

원본 코드 에서 볼 때 getMeasured Width()는 mMeasured Width 의 이 값 을 얻 었 습 니 다.이 값 은 8 자리 16 진법 의 숫자 로 높 은 두 자 리 는 이 measure 단계 의 Mode 값 을 나타 내 며,구체 적 으로 MeasureSpec 의 원 리 를 볼 수 있다.여기 mMeasured Width&MEASUREDSIZE_MASK 는 측정 단계 가 끝나 면 뷰 의 실제 값 을 나타 낸다.그리고 이 값 은 set Measured DimensionRaw()함 수 를 호출 한 후에 설 정 됩 니 다.그래서 getMeasured Width()의 값 은 measure 단계 가 끝 난 후에 얻 은 view 의 원시 값 입 니 다.
2.getWidth()

그럼 문제 가 생 겼 습 니 다.mRight 와 mLeft 는 어떤 값 이 고 언제 설정 되 었 습 니까?우 리 는 layot 단계 의 원본 코드 를 다시 봅 니 다.

layot 단계 에 서 는 setOpticalFrame()을 호출 하거나 setFrame()방법 을 호출 합 니 다.원본 코드 에서 setOpticalFrame()방법 을 알 수 있 습 니 다.결국 호출 된 setFrame()방법 입 니 다.

그래서 최종 mLeft 와 mRight 의 값 은 setFrame()방법 에서 설정 되 었 습 니 다.그리고 이 mLeft,mRight 는 view 가 최종 적 으로 화면 에 표시 하 는 크기 를 대표 합 니 다.
3.다음은 간단 한 ViewGroup 을 사용자 정의 하여 layot 단계 에서 left,right 의 값 을 바 꾸 고 getMeasured Width()와 getWidth()방법 간 의 차 이 를 관찰 합 니 다.



이 demo 에서,우 리 는 textview 에 폭 을 100 px 로 설정 하 였 으 나,layot 단계 에서 200 까지 늘 렸 습 니 다.최종 결 과 는 width 단 추 를 누 르 면 200 으로 표시 되 고,measuredWidth 단 추 를 누 르 면 100 으로 표 시 됩 니 다.


총결산
위 에서 말 한 것 은 소 편 이 소개 한 안 드 로 이 드 에서 getWidth()와 getMeasured Width()의 차이 점 입 니 다.여러분 에 게 도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 시 면 댓 글 을 남 겨 주세요.소 편 은 제때에 답 해 드 리 겠 습 니 다.여기 서도 저희 사이트 에 대한 지지 에 감 사 드 립 니 다!