Android Image Dialog/Popup与图像大小相同,没有边框

rek*_*t0x 21 android dialog image popup transparent

目前我正在使用文件浏览器.一切正常,只有一个例外:如果用户点击图像(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();
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

这是输出:

失败输出

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

最好的选择是在图像后面制作对话框/视图,大小与图像相同.另一种方法是将图像背后的背景设置为透明.

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


解:

// 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();
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

最终产出:

产量

小智 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>
Run Code Online (Sandbox Code Playgroud)

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

如果这有效的话。尝试在自定义主题中修改它:

    <style name="Widget.ImageWell">
    <item name="android:background">@android:drawable/panel_picture_frame_background</item>
    </style>
Run Code Online (Sandbox Code Playgroud)