2014-03-24 4 views
0

나는 어떻게 구할 수는 0ImageView의 높이와 너비를 얻는 방법은 무엇입니까?

public class FullScreenImage extends FragmentActivity { 
    ImageView full_view; 
    int width; 
    int height; 
    @Override 
    protected void onCreate(Bundle arg0) { 
     super.onCreate(arg0); 

     setContentView(R.layout.full_screen); 

     full_view = (ImageView)findViewById(R.id.full_view); 

     Bundle bundle=getIntent().getExtras(); 
     byte[] image= bundle.getByteArray("image"); 

     width  =  full_view.getWidth(); 
     height  =  full_view.getHeight(); 

     Bitmap theImage = BitmapFactory.decodeByteArray(image, 0, image.length); 
     Bitmap resized = Bitmap.createScaledBitmap(theImage,width , height, true); 

     full_view.setImageBitmap(resized); 
    } 


} 

이 제발 도와주세요 반환 것 이미지 뷰 봇의 높이 & 폭을 얻기 위해 노력하고 있어요?

답변

3

새로운 Android 개발자가 흔히 저지르는 실수는 생성자 내부에서보기의 너비와 높이를 사용하는 것입니다. 보기의 생성자가 호출되면 Android는보기의 크기를 아직 알지 못하므로 크기가 0으로 설정됩니다. 실제 크기는 레이아웃 단계에서 계산됩니다. 레이아웃 단계는 생성 후 아무 것도 그리기 전에 발생합니다. onSizeChanged() 메서드를 사용하여 값을 알 때 값을 알리거나 onDraw() 메서드와 같이 나중에 getWidth()getHeight() 메서드를 사용할 수 있습니다.

+0

참조 Farhan이 이미 언급 한 내용에 대해서도'http : // developer.android.com/reference/android/view/ViewTreeObserver.OnGlobalLayoutListener.html'을 살펴볼 수 있습니다. – Anuj

0

당신이 높이를 처음 얻고 그 시간 폭 경우 사용 가능한 이미지가 너무 0

이 코드를 사용하시기 바랍니다되는 결과를하지 있습니다 :

setContentView(R.layout.full_screen); 

    full_view = (ImageView)findViewById(R.id.full_view); 

    Bundle bundle=getIntent().getExtras(); 
    byte[] image= bundle.getByteArray("image"); 


    Bitmap theImage = BitmapFactory.decodeByteArray(image, 0, image.length); 
    Bitmap resized = Bitmap.createScaledBitmap(theImage,width , height, true); 

    full_view.setImageBitmap(resized); 

    width  =  full_view.getWidth(); 
    height  =  full_view.getHeight(); 
1

는 또한 this

int finalHeight, finalWidth; 
final ImageView iv = (ImageView)findViewById(R.id.scaled_image); 
final TextView tv = (TextView)findViewById(R.id.size_label); 
ViewTreeObserver vto = iv.getViewTreeObserver(); 
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { 
public boolean onPreDraw() { 
    finalHeight = iv.getMeasuredHeight(); 
    finalWidth = iv.getMeasuredWidth(); 
    tv.setText("Height: " + finalHeight + " Width: " + finalWidth); 
    return true; 
    } 
}); 
관련 문제