0

我使用初始化图像视图

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

之后相机拍照。在onActivityResult(int requestCode, int resultCode, Intent data)方法中,我正在执行以下操作来设置 ImageView。

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

我只是得到一个空白屏幕。应用程序启动时的屏幕布局也是这样的: 在此处输入图像描述

拍完照片后,屏幕变形了,我不知道为什么会这样。

在此处输入图像描述

PS:我增加了第二张图片的大小,以便按钮可见,如果您看到按钮的大小已经减小了很多,并且 Imageview 占位符似乎占用了大部分屏幕空间而没有显示 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>
4

3 回答 3

0

正如 Ravi bhatt 怀疑的那样,这是位图图像大小的问题。我得到的大小使用:

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);

显示器现在工作正常。

于 2013-10-15T01:01:14.557 回答
0

发生这种情况是因为您解码的位图尺寸很大并且您ImageView的高度是wrap_content,所以尺寸会增加。要么给定尺寸,要么用layout_weight它来正确格式化。

于 2013-10-21T07:20:38.930 回答
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>
于 2013-10-11T04:27:22.167 回答