Android图像对话框/弹出窗口与图像大小相同且无边框。

23

目前我正在开发一个文件浏览器。除了一个问题,一切都运行良好:如果用户单击图像(jpg、png、bmp等),我希望该图像显示在对话框或弹出窗口中,并且该对话框或弹出窗口的大小与图像相同-因此根本没有边框。这些图像文件位于SD卡上。

以下是我目前所拥有的:

BitmapDrawable bitmap = new BitmapDrawable(context.getResources(), TARGET_PATH);

AlertDialog.Builder imageDialog = new AlertDialog.Builder(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

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

XML文件:

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

    <ImageView
        android:id="@+id/thumbnail_IMAGEVIEW"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:contentDescription="@string/icon_DESCRIPTION" />

</RelativeLayout>

输出结果如下:

失败的输出

图片边缘有难看的边框,我不想让它们显示出来。我尝试了很多谷歌等网站上列出的方法和例子... 暂时还没有起作用的。

最好的选项是使对话框/视图与图像大小相同。另一种方法可能是将图像后面的背景设置为透明。

我该怎么样实现任何一个解决方案呢? 我希望将背景设置为与图像相同的大小,这样就不会留下“不可见”的东西,但如果使用透明选项,我也可以接受。


解决方案:

// Get screen size
Display display = context.getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screenWidth = size.x;
int screenHeight = size.y;

// Get target image size
Bitmap bitmap = BitmapFactory.decodeFile(TARGET);
int bitmapHeight = bitmap.getHeight();
int bitmapWidth = bitmap.getWidth();

// Scale the image down to fit perfectly into the screen
// The value (250 in this case) must be adjusted for phone/tables displays
while(bitmapHeight > (screenHeight - 250) || bitmapWidth > (screenWidth - 250)) {
    bitmapHeight = bitmapHeight / 2;
    bitmapWidth = bitmapWidth / 2;
}

// Create resized bitmap image
BitmapDrawable resizedBitmap = new BitmapDrawable(context.getResources(), Bitmap.createScaledBitmap(bitmap, bitmapWidth, bitmapHeight, false));

// Create dialog
Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.thumbnail);

ImageView image = (ImageView) dialog.findViewById(R.id.imageview);

// !!! Do here setBackground() instead of setImageDrawable() !!! //
image.setBackground(resizedBitmap);

// Without this line there is a very small border around the image (1px)
// In my opinion it looks much better without it, so the choice is up to you.
dialog.getWindow().setBackgroundDrawable(null);

// Show the dialog
dialog.show();

XML文件:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/imageview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</ImageView>

最终输出:

Output


经过数小时的尝试和错误,我终于让它正常工作了。我将解决方案添加到了这篇帖子的第一篇文章中。 - rekt0x
5
嘿,我非常喜欢你的解决方案,所以我将其抽象成一个易于使用的组件 https://github.com/juliomarcos/ImageViewPopUpHelper ImageViewPopUpHelper.enablePopUpOnClick(getActivity(), targetImageView)。 - Julio Rodrigues
太棒了 - 谢谢伙计 ;) - rekt0x
5个回答

2

将您的ImageView从这个改为:

<ImageView
        android:id="@+id/thumbnail_IMAGEVIEW"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:contentDescription="@string/icon_DESCRIPTION" />

转换为:

<ImageView
        android:id="@+id/thumbnail_IMAGEVIEW"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:adjustViewBounds="true" <!-- Here is the thing -->
        android:contentDescription="@string/icon_DESCRIPTION" />

希望这能有所帮助。

好的,如果这样不行的话,我认为你应该制作自己的“Dialog”,并在那里应用布局。因为你正在使用 Android 的“AlertDialog”,所以无法更改布局。干杯。 - user2652394
是的,我按照你建议做了,现在可以看看我的第一篇帖子“解决方案”了。不过还是谢谢你的帮助;) - rekt0x

1
你可以制作一个自定义对话框来显示图片, 创建一个用于在对话框的setcontentview中填充的布局, 取一个线性布局,宽高使用wrap_content,并在其中添加一个带scale属性“fitxy”的imageview。

0
将以下一行代码添加到您的活动中的图像视图初始化下,它可以解决您的问题。
image.setScaleType(ImageView.ScaleType.FIT_XY); 

0
使用FrameLayout作为父布局,而不是RelativeLayout,并将其设置为wrap_content和0边距。
尝试:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="@color/background">

<FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/frameLayout"
        android:background="#b2ff7c">

    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView"
            android:src="@drawable/ic_launcher"/>
</FrameLayout>

</RelativeLayout>

结果应该是与 ImageView 相同大小的背景。

如果这样不起作用,请尝试在自定义主题中修改此设置:

    <style name="Widget.ImageWell">
    <item name="android:background">@android:drawable/panel_picture_frame_background</item>
    </style>

对我没用。结果与我的代码一样。视图边缘仍然有边框。 - rekt0x

0

你试过在ImageView中设置setType吗?

<ImageView ... android:scaleType="fitXY" ... />


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接