2013-06-11 2 views
6

왜 lColorWheel.getWith() 메서드가 0을 반환합니까? 나는 그것이 onMeasure 이벤트와 관련이 있다고 생각하지만 실제로 어떻게 작동하는지 문서에서 이해할 수 없다. 어디서 mDrawable에 대한 치수를 설정해야합니까?View.getWidth()는 onCreate 메소드에서 작동하지 않습니까?

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_light); 

    ShapeDrawable mDrawable; 
    ivColorWheel=(ImageView)findViewById(R.id.ivColorWheel); 
    lColorWheel=findViewById(R.id.lColorWheel); 
    ld=(LayerDrawable)getResources().getDrawable(R.drawable.colorwheel); 

    mDrawable = new ShapeDrawable(new OvalShape()); 

    mDrawable.setIntrinsicWidth(lColorWheel.getWidth()); 
    mDrawable.setIntrinsicHeight(lColorWheel.getHeight()); 
    Log.d("lColorWheel.getWidth()",Integer.toString(lColorWheel.getWidth())); //returns 0, why? 
    Log.d("lColorWheel.getHeight()",Integer.toString(lColorWheel.getHeight())); //returns 0, why ? 

및 appropiate XML 파일 :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/background" 
android:gravity="center_horizontal" 
android:addStatesFromChildren="true" > 

<RelativeLayout 
    android:id="@+id/lColorWheel" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="20dp" 
    android:layout_marginRight="20dp" 
    android:layout_marginTop="20dp" 
    android:layout_centerHorizontal="true" 
    android:adjustViewBounds="true"> 

    <ImageView 
     android:id="@+id/ivColorWheel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:adjustViewBounds="true" 
     android:paddingBottom="0dp" 
     android:paddingTop="0dp" 
     android:src="@drawable/iconwheel" 
     /> 

     <ImageView 
      android:id="@+id/ivCenterButton1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true" 
      android:maxHeight="5dp" 
      android:maxWidth="5dp" 
      android:onClick="clc" 
      android:paddingBottom="0dp" 
      android:paddingLeft="0dp" 
      android:paddingRight="0dp" 
      android:paddingTop="0dp" 
      android:src="@drawable/center3" /> 

</RelativeLayout> 

<SeekBar 
    android:id="@+id/sbColorIntensity" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:layout_alignParentLeft="true" 
     android:layout_below="@id/lColorWheel" 
    android:layout_marginLeft="20dp" 
    android:layout_marginRight="20dp" 
    android:layout_marginTop="20dp" 
    android:background="@drawable/colorintensitystrip" 
    android:max="255" 
    android:maxHeight="-25dp" 
    android:thumb="@drawable/thumb" 
    android:thumbOffset="0dp" 
/> 



<SeekBar 
    android:id="@+id/sbOrientation" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@id/sbColorIntensity" 
    android:layout_marginLeft="20dp" 
    android:layout_marginRight="20dp" 
    android:layout_marginTop="20dp" 
    android:background="@drawable/orientationstrip" 
    android:max="255" 
    android:maxHeight="-25dp" 
    android:thumb="@drawable/thumb" 
    android:thumbOffset="0dp" 
    /> 

<SeekBar 
    android:id="@+id/sbWhiteIntensity" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@id/sbOrientation" 
    android:layout_marginLeft="20dp" 
    android:layout_marginRight="20dp" 
    android:layout_marginTop="20dp" 
    android:background="@drawable/whiteintensitystrip" 
    android:max="255" 
    android:maxHeight="-25dp" 
    android:thumb="@drawable/thumb" 
    android:thumbOffset="0dp" 
    /> 
</RelativeLayout> 

들으 당신은 마감을 렌더링하기 전에 사용하려는 경우 뷰의 높이와 폭을 측정 할 필요가

답변

15

.

다음 코드는 모든보기의 높이와 너비를 측정하는 데 도움이됩니다.

imgView.measure(
    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 
imgView.layout(0, 0, 
    imgView.getMeasuredWidth(), 
    imgView.getMeasuredHeight()); 

높이와 너비를 사용하기 전에이 코드를 작성하십시오.

때로는이 접근 방식이 적절한 결과를주지 못합니다. 그것의 대안이 있습니다. 아래 코드는 화면에 그리기 전에 높이와 너비를 계산합니다.

ViewTreeObserver viewTree = imgView.getViewTreeObserver(); 
viewTree.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { 
    public boolean onPreDraw() { 
     finalHeight = imgView.getMeasuredHeight(); 
     finalWidth = imgView.getMeasuredWidth(); 
     //print or do some code 
     return true; 
    } 
}); 
+0

측정 방법을 사용할 때 항상 0,0을 얻습니다. 왜? imgView는 원하는 차원을 어떻게 알 수 있습니까? – suitianshi

0

getwidth()를 너무 일찍 호출하면 UI에서이 값을 가져올 준비가되지 않았습니다. 옵서버가 필요합니다. 이 값은 UI의 첫 번째 페인트 직후에 설정되었다고 생각합니다. 당신의에서 onCreate() 메소드에서 이것을 시도 :

final ViewTreeObserver vto = myView.getViewTreeObserver(); 
    if (vto.isAlive()) { 
     vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 

      public void onGlobalLayout() { 

       int witdth = lColorWheel.getWith() 
       // remove the listener... or we'll be doing this a lot. 
       ViewTreeObserver obs = promotionSub_ly.getViewTreeObserver(); 
       obs.removeGlobalOnLayoutListener(this); 
      } 
     }); 
    } 
0

는 당신은 너무 일찍 getWidth()를 호출하는

CommonsWare here 인용한다. UI의 크기가 조정되지 않았고 아직 화면에 이 배치되었습니다.

나는 당신이 어쨌든, 무엇을하고 있는지 일을하려는 의심 - 위젯 을 자신의 클릭 가능한 영역을 변경하지 않는 애니메이션을, 그래서 버튼 는 여전히 얼마나 에 관계없이 원래의 방향으로 클릭에 응답 할 것이다 회전.

그렇다면 치수 리소스를 사용하여 크기를 정의한 다음 레이아웃 파일에서 치수 리소스를 참조하고이 문제를 방지하려면 소스 코드를 사용할 수 있습니다.

+0

질문에 대한 답변은 무엇입니까? – xmen

1

onCreate 메서드가 실행 중일 때 Android가 아직 레이아웃 통과 또는 측정 패스를 수행하지 않았습니다.

measure 메서드를 사용하여보기의 크기를 측정 할 수 있지만 RelativeLayout (트리를 따라 두 개의 레이아웃을 전달 함)을 사용하고 있으므로 특히 ViewTreeObserver에서 콜백을 구현하는 것이 좋습니다.

ViewTreeObserver.OnGlobalLayoutListener ()을 사용하면 각 항목의 위치와 해당 크기를 정확히 알면 레이아웃을 조정할 수 있습니다.

+0

초기 너비와 높이를 설정하려고했습니다.이것은 좋은 방법이 아닙니다. 더 좋은 방법은 자체 객체를 만들고 onMeasure 이벤트를 구현하는 것입니다. – foterm

관련 문제