2013-10-11 62 views
0

我初始化使用无法设置的ImageView android系统

iv = (ImageView)findViewById(R.id.ivPic);

相机拍摄的PIC在这之后的ImageView的。在onActivityResult(int requestCode, int resultCode, Intent data)方法中,我正在做以下设置ImageView。

Bitmap bmpEmail = BitmapFactory.decodeFile(out.getAbsolutePath()); 
iv.setImageBitmap(bmpEmail); 

我只是一个空白的屏幕。当应用程序启动时,屏幕的布局是这样的: enter image description here

当我拍照时,屏幕会变形,我不知道为什么会发生这种情况。

enter image description here

PS:我已经增加了第二张图片的大小,使按钮是可见的,如果你看到这些按钮的尺寸已经缩小了不少与ImageView的占位符似乎占用了大部分的屏幕空间而不显示图像视图。

我已经发布我下面的XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent" 
    android:orientation="vertical" android:weightSum="100"> 

    <ImageView 
     android:id="@+id/ivPic" 
     android:layout_width="match_parent" android:layout_height="wrap_content" 
     android:layout_gravity="center" android:src="@drawable/ic_launcher" 
     android:layout_weight="60"/> 

    <ImageButton 
     android:id="@+id/ibPic" 
     android:layout_width="match_parent" android:layout_height="wrap_content" 
     android:layout_gravity="center" android:src="@drawable/ic_launcher" 
     android:layout_weight="20" /> 

    <Button 
     android:id="@+id/bPic" 
     android:layout_width="match_parent" android:layout_height="wrap_content" 
     android:layout_gravity="center" android:text="Email Pic" 
     android:layout_weight="20" /> 

</LinearLayout> 
+0

您是否在bmpEmail中获得位图? –

+0

是的,我得到的价值'bmpEmail >> android.graphics.Bitmap @ 410b8760' – misguided

+0

,因为你的解码位图大小和你的'ImageView'高度'wrap_content',所以尺寸增加。要么给定尺寸或使用'layout_weight'来正确格式化。 –

回答

0

发生这种情况是因为您的解码位图大小很大,而您的ImageView的高度为wrap_content,因此大小会增加。请提供修正尺寸或使用layout_weight正确格式化。

+0

不是确切的答案我是寻找。但有足够的指针让我朝着正确的方向前进。 – misguided

0
// try this 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:orientation="vertical"> 
    <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:gravity="center" 
      android:layout_weight="1"> 
     <ImageView 
       android:id="@+id/ivPic" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:adjustViewBounds="true" 
       android:scaleType="fitXY" 
       android:src="@drawable/ic_launcher"/> 
    </LinearLayout> 
    <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center"> 
     <ImageButton 
       android:id="@+id/ibPic" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/ic_launcher"/> 
    </LinearLayout> 
    <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center"> 
     <Button 
       android:id="@+id/bPic" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Email Pic"/> 
    </LinearLayout> 
</LinearLayout> 
+0

如何在单独的布局中添加每个视图将会解决问题 –

+0

Mate无法看到图像视图。但按钮布局不会改变现在onload的应用程序,并采取图片 – misguided

+0

它现在看起来像这样现在'http:// imgur.com/FwHBJkH'(点击链接查看布局) – misguided

0

由于拉维布哈特怀疑这是与位图图像大小的问题。我使用的大小:

bmpEmail.getHeight() & bmpEmail.getWidth()

使用下面的方法来获得ImageView的大小:

ViewTreeObserver vto = iv.getViewTreeObserver(); 
       vto1.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { 
        public boolean onPreDraw() { 
         Log.d("Test", "Height: " + iv.getMeasuredHeight() + " Width: " + iv.getMeasuredWidth()); 
         return true; 
         } 
        }); 

最后按比例缩小图片,使用以下

Bitmap scaled = Bitmap.createScaledBitmap(bmpEmail, 512, 540, true); 
iv.setImageBitmap(scaled); 

的显示现在工作正常。