web-dev-qa-db-fra.com

Android Image Dialog/Popup même taille que l'image et sans bordure

En ce moment, je travaille sur un navigateur de fichiers. Tout fonctionne bien, à une exception près: Si l'utilisateur clique sur une image (jpg, png, bmp, ..), je souhaite que l'image soit affichée dans une boîte de dialogue ou dans un menu contextuel de même taille que l’image - afin qu’il n’y ait pas de frontières du tout. Les fichiers d’image se trouvent sur la carte mémoire.

Voici ce que j'ai jusqu'à présent:

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

Le fichier 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>

Voici la sortie:

Fail Output

Il y a des frontières laides sur les bords des images - je ne veux pas qu'elles soient montrées. J'ai essayé beaucoup de choses et d'exemples énumérés dans Google, etc. - rien n'a encore fonctionné.

La meilleure option sera de rendre la boîte de dialogue/la vue derrière l'image de la même taille que l'image. Une autre solution consiste à définir un arrière-plan transparent derrière l’image.

Comment puis-je obtenir une solution? J'aimerais beaucoup que l'arrière-plan ait la même taille que l'image, de sorte qu'il ne reste plus d'éléments "invisibles", mais l'option "Transparent" me convient également.


Solution:

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

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

Résultat final:

Output

21
Christian Lanz

vous pouvez créer une boîte de dialogue personnalisée pour afficher des images, créer une mise en page pour gonfler dans la vue setcontentent de la boîte de dialogue, créer une mise en page linéaire avec un contenu en largeur et en hauteur et une image avec la propriété scale fitxy.

1
ss_android

Changez votre ImageView à partir de ceci:

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

Pour ça:

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

J'espère que cela t'aides.

1
user2652394

Avez-vous essayé de définir setType dans ImageView?

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

0
Twin Edo

Ajoutez un code à une seule ligne dans votre activité lors de l’initialisation de la vue d’image pour résoudre votre problème.

image.setScaleType(ImageView.ScaleType.FIT_XY); 
0
Nazmus Saadat

Utilisez FrameLayout en tant que parent plutôt que RelativeLayout et définissez-le sur wrap_content et sur 0 marges.

Essayer:

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

Le résultat devrait être un arrière-plan de la même taille que ImageView.

Si cela fonctionne. Essayez de le modifier dans un thème personnalisé: 

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