如何使AlertDialog大小适合包含的图像

时间:2015-04-17 10:57:27

标签: android android-imageview android-alertdialog android-image

我正在尝试创建一个AlertDialog,它将以全屏显示图像。我的问题是对话框不适合图像。 我可以使用我找到的一些代码来缩放图像以适应水平或垂直限制(取决于屏幕结构)。但是,对话框显示了我想要消除的空白区域。

我的对话框显示了这个:

enter image description here

但我希望它适合没有左右空格的图像,如下所示: enter image description here

我创建警告对话框的代码:

void showImage(String path, String name) {

    // Get image from the aboslute path
    BitmapDrawable bitmap = new BitmapDrawable(getActivity().getResources(), path);

    // Construct the dialog
    AlertDialog.Builder imageDialog = new AlertDialog.Builder(getActivity());
    imageDialog.setTitle(name);
    imageDialog.setCancelable(true);

    LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(
            Context.LAYOUT_INFLATER_SERVICE);

    View layout = inflater.inflate(R.layout.thumbnail, null);
    ImageView image = (ImageView) layout.findViewById(R.id.imageView);
    image.setImageDrawable(bitmap);
    imageDialog.setView(layout);
    imageAlert = imageDialog.create();

    // Fullscreen
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    WindowManager manager = (WindowManager) getActivity().getSystemService(
            Activity.WINDOW_SERVICE);
    lp.copyFrom(imageAlert.getWindow().getAttributes());
    lp.width = manager.getDefaultDisplay().getWidth();
    lp.height = manager.getDefaultDisplay().getHeight();
    imageAlert.getWindow().setAttributes(lp);
    image.setMinimumWidth(lp.width);
    image.setMinimumHeight(lp.height);

    imageAlert.show();
}

我的XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content">


<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_centerHorizontal="true"
    android:id="@+id/imageView"/>
</LinearLayout>

我尝试了scaleType = fitXY,但它会使图像变形。 如何使对话框适合保持相同宽高比的图像?任何帮助将不胜感激

我可以找到解决问题的方法,但我不知道它是否是最佳方法。我手动计算了图像大小,并将图像和对话框设置为此大小。 首先,我检查哪个尺寸将限制图像的大小,并将屏幕值设置为该尺寸。然后,我计算了与前一个相关的另一个尺寸,以保持相同的比例,并且图像不会变形。

    // Get screen size
    WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();

    Point size = new Point();
    display.getSize(size);
    int screenWidth = size.x;
    int screenHeight = size.y;

    // Get image size
    int imgHeight = bitmap.getBitmap().getHeight();
    int imgWidth = bitmap.getBitmap().getWidth();

    // Get dialog params
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(imageAlert.getWindow().getAttributes());

    // Which dimension is bigger in relation to screen size
    if (((float)imgHeight) / screenHeight > ((float)imgWidth) / screenWidth) {

        // Height is bigger so width is calculated on relation to it to keep the same ratio
        lp.height = screenHeight;
        lp.width = (int) (imgWidth * ((float) screenHeight) / imgHeight);
    }
    else {
        // Width is bigger so height is calculated on relation to it to keep the same ratio
        lp.height =(int) (imgHeight * ((float) screenWidth) / imgWidth);
        lp.width = screenWidth;
    }

    // Set the new size
    image.getLayoutParams().width = lp.width;
    image.getLayoutParams().height = lp.height;
    image.requestFocus();
    imageAlert.show();

    imageAlert.getWindow().setAttributes(lp);

2 个答案:

答案 0 :(得分:0)

使用

imageDialog.getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN, LayoutParams.FLAG_FULLSCREEN);

答案 1 :(得分:0)

尝试执行此imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);并将高度和宽度设为match_parent,如下所示:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent">


<ImageView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_gravity="center"
      android:layout_centerHorizontal="true"
      android:id="@+id/imageView"/>
</LinearLayout>