2013-10-16 6 views
1

나는 extendsImageView 인 사용자 정의보기 (CustomStrechView)가 있습니다. 이 뷰의 요점은 가로 세로 비율을 잃지 않고 뷰를 채울 수있는 이미지를 얻는 것입니다.HorizontalScrollView에서 사용자 정의보기를 어떻게 사용합니까?

이제 CustomStrechView은 대부분의 응용 프로그램에서 사용할 때 잘 작동하지만 내 HorizontalScrollView 내에서 적용하려고하면 이미지가 모두 사라진 것처럼 보입니다. 왜 이런 일이 일어날 지 모르겠습니다. <HorizontalScrollView />을 제거하려고 시도했는데 이미지를 백업 할 때 설정할 필요가있는 속성이 있음을 느낄 수 있지만 찾지 못하는 것 같습니다.

누구나 내가 잘못하고있는 것을 지적하거나 HorizontalScrollView에서 작동하는 사용자 정의보기를 얻는 더 좋은 방법을 지적 할 수 있습니까?


편집 :

조금이 놀아 후, 나는 내가 width 차원, 즉 하드 코딩하는 경우에만 이미지를 표시하는 사용자 정의보기를 얻을 수있는 것으로 확인되었습니다 :

fill_parent로 높이를 떠나 는
<com.example.dragdropshapes.CustomStrechView 
    android:layout_width="200dp" 

...


01 괜찮 23,516,

코드 : 디버깅을 통해

public class CustomStrechView extends ImageView{ 

    private final Drawable srcPic; 
    private final String TAG = "strechyTag"; 
    private boolean changeSize = false; 
    private int Xpx; 
    private int Ypx; 

    public CustomStrechView(Context context) { 
     super(context); 
     srcPic = this.getDrawable(); 
    } 

    //... other constructors, not really important... 

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ 
     Log.d(TAG, "in the onMeasure!!\n"); 
     int width, height; 

     width = MeasureSpec.getSize(widthMeasureSpec); 
     height = width * srcPic.getIntrinsicHeight()/srcPic.getIntrinsicWidth(); 
     setMeasuredDimension(width, height); 
    } 
} 
+0

무엇을 얻고 있습니까? –

+0

@Hussain - "오류"가 표시되지 않고 이미지가 나타나지 않습니다. 'HorizontalScrollView'는 아무것도 표시하지 않습니다. 스플래시 화면이 정상적으로로드되고 스크롤보기가 이동합니다. 'ImageView'를 사용하면 내 사용자 정의보기로 바꾼다면 아무 것도 나타나지 않습니다. – Mike

답변

0

:

<!-- Removing this makes the images show up --> 
<HorizontalScrollView  
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:fillViewport="true" 
    android:layout_marginTop="50dp"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:showDividers="middle" 
     android:orientation="horizontal"> 

     <!-- Changing this to a normal ImageView makes the image show up --> 
     <com.example.dragdropshapes.CustomStrechView 
      android:id="@+id/pickshapes" 
      android:layout_height="fill_parent" 
      android:layout_width="fill_parent" 
      android:clickable="true" 
      android:onClick="pickShapes" 
      android:background="@drawable/border" 
      android:src="@drawable/ic_launcher" 
      /> 
     <!-- There are 5 other custom ImageViews here --> 
    </LinearLayout> 
</HorizontalScrollView> 

여기에 사용자 지정보기 코드의 주요 부분 :

여기 내 사용자 정의보기가 XML 파일에 사용되는 방법 적어도 부분적으로이 문제를 해결할 수있었습니다. Wh는 MeasureSpec.getSize(widthMeasureSpec); 호출에서 얻은 width이 0이라는 것을 나타냅니다. 따라서 계산의 폭과 높이가 0으로 끝나고 setMeasuredDimension 호출은 이미지를 보이지 않게 만듭니다. 난 아직도 이해하지 않기 때문에

지금 내가, 내가 해결 부분이 을 말하는거야 내가 0 I 의 값을 얻고 있었다이 생각하는 이유 그것입니다 때문에 HorizontalScrollView 폭 ISN에 ' (실제로는 콘텐츠에 따라 다름) MeasureSpec.getSize(widthMeasureSpec);은 실제로 부모와 이야기하고 있습니다. 보통 Linear 또는 RelativeLayout의 부모는 정의 된 너비가 있습니다. HorizontalScrollView에있는 것처럼 ...하지만 그건 그냥 추측입니다. 이 지점에서.

그래서 수정했습니다. width이 0이면 대신 height을 통해 계산을 실행합니다. height은 0 오류 (및 else을 총칭 함)로 나누지 않으려면 두 가지 경우가 필요합니다.

다음은 현재 질문에 언급 된 xml 파일의 해결책입니다.

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ 
    int width = MeasureSpec.getSize(widthMeasureSpec); 
    int height = MeasureSpec.getSize(heightMeasureSpec); 

    if(width > 0) 
     height = width * srcPic.getIntrinsicHeight()/srcPic.getIntrinsicWidth(); 
    else if(srcPic.getIntrinsicWidth() < srcPic.getIntrinsicHeight()) 
    width = height/(srcPic.getIntrinsicHeight()/srcPic.getIntrinsicWidth()); 
    else 
    width = height/(srcPic.getIntrinsicWidth()/srcPic.getIntrinsicHeight()); 

    setMeasuredDimension(width, height); 
} 
관련 문제