2012-10-02 6 views
1

사용자 정의보기를 구현 중이며 런타임에 크기를 변경할 수 있어야합니다.사용자 정의보기의 가변 크기 Android

간단한 문제 :이보기에서 비트 맵을 표시해야하지만 자주 새로 고쳐지기 때문에 SurfaceView를 확장하고 싶습니다.

문제점은 ImageView의 자동 크기 조정 기능이 손실된다는 것입니다.

그래서 setCustomBitmap (비트 맵 bmp) 메서드를 선언하고 해당 비트 맵 너비와 높이 (가로 세로 비율 유지)에 따라 뷰 크기를 크기 조정 (현재 또는 다음에보기가 표시 될 때) 크기로 변경하려고합니다.

어떤 방법을 사용해야합니까? 나는 setWidth()와 setHeight()가 최선의 아이디어가 아니라고 상상한다.

답변

2

SurfaceView를 확장하고 있으므로 onMeasure() 메서드를 재정의 할 수있다. 수퍼 클래스에 전달하는 MeasureSpec 인스턴스 (super.onMeasure() 통해)는 SurfaceView의 크기를 결정합니다. 비트 맵의 ​​크기를 사용하여 각 MeasureSpec을 만들 수 있습니다. 예를 들어

:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int parentViewWidth = MeasureSpec.getSize(widthMeasureSpec); 
    int parentViewHeight = MeasureSpec.getSize(heightMeasureSpec); 
    // ... take into account the parent's size as needed ... 
    super.onMeasure(
     MeasureSpec.makeMeasureSpec(bitmap.width(), MeasureSpec.EXACTLY), 
     MeasureSpec.makeMeasureSpec(bitmap.height(), MeasureSpec.EXACTLY)); 
} 
관련 문제