Android Image对话框/ Popup与图像大小相同,没有边框 [英] Android Image Dialog/Popup same size as image and no border

查看:152
本文介绍了Android Image对话框/ Popup与图像大小相同,没有边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在使用文件浏览器。一切都可以正常工作,但有一个例外:
如果用户点击图像(jpg,png,bmp,..),我希望图像显示在对话框或弹出窗口中,该大小与形象 - 所以没有边框。
图像文件位于sdcard上。

At the moment I am working on a file browser. Everything works fine with one exception: If the user clicks on an image (jpg, png, bmp, ..), I want the image to be displayed in a dialog or in a popup which has the same size as the image - so that there are no borders at all. The image files are located on the sdcard.

以下是我目前为止所述:

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>

以下是输出:

在图像的边缘是丑陋的边框 - 我不希望他们被显示。我尝试了很多东西和例子列在谷歌等.. - 没有任何工作。

There are ugly borders at the edges of the images - I don't want them to be shown. I tried lots of stuff and examples listed in google, etc.. - nothing worked yet.

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

The best option will be to make the dialog/the view behind the image, the same size as the image. Another way could be to set the background behind the image transparent.

如何实现任何解决方案?
我喜欢使背景与图像的大小相同,所以没有隐形的东西,但我也可以透明的选项。

How do I achieve any solution? I'd love to make the background the same size as the image is, so there is no "invisible" stuff left, but I will also be okay with the transparent option.

解决方案:

// 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>

最终输出

推荐答案

您可以制作一个自定义对话框来显示图像,
在对话框的setcontentview中创建一个膨胀的布局,
采用一个线性布局,宽度和高度包装内容和图像视图在其中财产fitxy。

you can make a custom dialog for showing images , make a layout for inflating in dialog's setcontentview , take one linear layout with width and height wrap content and imageview with in it with scale property fitxy .

这篇关于Android Image对话框/ Popup与图像大小相同,没有边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆