如何显示宽高比的图像?

时间:2014-09-18 11:14:43

标签: android image imageview relativelayout

在我的应用程序中,我希望显示具有预期比率的图像,如图库。在我的应用程序中,我必须在图像上显示标记图像。因为标记图像具有拖动功能。所以以下是xml的那个< / p>

<RelativeLayout 
    android:background="#000000" 
    android:layout_above="@+id/buttonlayout"    
    android:id="@+id/imagelayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <RelativeLayout
        android:layout_centerInParent="true"
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </RelativeLayout>


    </RelativeLayout>

在我的活动中选择了图库中的图像后,在onactivityresult方法中我正在使用以下代码。

                    try {
//                      bmGallayImage = decodeFile(selectedImagePath);
                        bmGallayImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
                        bmRotated = InventorySubmitImagesActivity.rotateBitmap(bmGallayImage, orientation); 
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } 
                    if(bmRotated!=null){
                        Drawable dr = new BitmapDrawable(bmRotated);

                        old_width=bmRotated.getWidth();
                        old_height=bmRotated.getHeight();
                        Log.e("gal bitmap height and width",""+old_height+"  "+old_width);
                        relImage.getLayoutParams().height = old_height;
                        relImage.getLayoutParams().width = old_width;
                        relImage.setBackgroundDrawable(dr);

                    }
                    relImage.removeAllViews();
                    imgMarker = new ImageView(this);
                    final RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                         RelativeLayout.LayoutParams.MATCH_PARENT);
                    layoutParams.leftMargin = 0;
                    layoutParams.topMargin = 0;
                    relImage.addView(imgMarker, layoutParams);
                    imgMarker.setScaleType(ImageView.ScaleType.MATRIX);
                    bmdragImage=BitmapFactory.decodeResource(getResources(), R.drawable.image_marker);
                    imgMarker.setImageBitmap(bmdragImage);

在此代码中,relImage是相对布局的参考,imgMarker是图像视图的参考。并且图库图像设置为相对布局。我的设备分辨率为480×800,图像分辨率为1920×1080。所以这是我得到的输出。

enter image description here 画廊图像视图就像是轰鸣声的屏幕截图。

enter image description here

但是我的要求就像画廊图片一样,我希望以预期的比例显示图像。所以请建议我如何做到这一点。在此先感谢所有..

1 个答案:

答案 0 :(得分:0)

您可以使用FrameLayout将图片视图置于此处,并将您的标记ImageView设为this http://chiuki.github.io/images/android-layout-101/frame-layout-center.png

相关问题