Android Image Dialog/Popup 与图像大小相同且无边框

标签 android image dialog popup transparent

目前我正在使用文件浏览器。一切正常,但有一个异常(exception): 如果用户单击图像(jpg,png,bmp,..),我希望图像显示在与图像大小相同的对话框或弹出窗口中 - 这样就没有边框了。 图像文件位于 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>

这是输出:

Fail Output

图像边缘有丑陋的边框 - 我不希望它们显示出来。我尝试了很多在 google 等中列出的东西和示例。 - 还没有任何效果。

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

如何实现任何解决方案? 我想让背景与图像的大小相同,因此不会留下“不可见”的东西,但我也可以使用透明选项。


解决方案:

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

最佳答案

从此更改您的 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" />

希望这会有所帮助。

关于Android Image Dialog/Popup 与图像大小相同且无边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18110603/

相关文章:

android过滤器自定义数组适配器并再次带回旧项目

android - Android Studio 3.3 Canary 11 (gradle 3.3.0 alpha 11) 与 Kotlin 1.3.0-rc-57 和 constraintlayout 2.0 的大问题

c# - 在对话框的构造函数中调用 .Show() 或 .ShowDialog 是一种好习惯

jquery - Cakephp 中的类似对话框的工具提示 - 类似于 stackoverflow 框

android - resConfigs 如何在 android (gradle) 中工作?

android - 导入库仅用于调试

html - 图像路径在 Angular 2 中不起作用

java - 如何保存和显示应用程序制作的照片

c++ - 使用 Allegro 5 加载 .png 图像文件

java - Android - 手机锁定时显示来自服务的警报对话框