Android Image Dialog / Popup tego samego rozmiaru co obraz i bez obramowania

W tej chwili pracuję nad przeglądarką plików. Wszystko działa dobrze z jednym wyjątkiem: jeśli użytkownik kliknie obraz (jpg, png, bmp, ..), chcę, aby obraz był wyświetlany w oknie dialogowym lub w oknie podręcznym, które ma taki sam rozmiar jak obraz - tak, że nie ma żadnych granic. Pliki obrazów znajdują się na karcie SD.

Oto, co mam do tej pory:

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();

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

Oto wyjście:

Na krawędziach obrazów są brzydkie obramowania - nie chcę, żeby były pokazywane. Próbowałem wielu rzeczy i przykładów wymienionych w google, itp. - nic jeszcze nie działało.

Najlepszym rozwiązaniem będzie utworzenie okna dialogowego / widoku za obrazem o tym samym rozmiarze co obraz. Innym sposobem może być przezroczyste ustawienie tła za obrazem.

Jak mogę osiągnąć jakiekolwiek rozwiązanie? Chciałbym, aby tło było tego samego rozmiaru, co obraz, więc nie ma już „niewidocznych” rzeczy, ale będę też w porządku z opcją przezroczystości.

Rozwiązanie:

// 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();

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

Wynik końcowy:

questionAnswers(5)

yourAnswerToTheQuestion