Android Image Dialog / Popup mismo tamaño que la imagen y sin frontera
En este momento estoy trabajando en un navegador de archivos. Todo funciona bien con una excepción: si el usuario hace clic en una imagen (jpg, png, bmp, ..), quiero que la imagen se muestre en un diálogo o en un popup que tenga el mismo tamaño que la imagen – de modo que No hay fronteras en absoluto. Los archivos de imagen se encuentran en la tarjeta sd.
Esto es lo que tengo hasta ahora:
- Creación de ImageButton de forma irregular con diferentes estados de clic
- ¿Cómo puedo crear una actividad transparente en Android?
- Vista transparente sobre ImageView
- Fondo transparente para Android OpenGL ES
- Android XML dibujable degradado transparente
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();
El archivo 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>
Aquí está la salida:
Hay bordes feos en los bordes de las imágenes – no quiero que se muestren. He probado muchas cosas y ejemplos enumerados en google, etc. – nada funcionó todavía.
La mejor opción será hacer que el diálogo / la vista detrás de la imagen, el mismo tamaño que la imagen. Otra forma podría ser configurar el fondo detrás de la imagen transparente.
¿Cómo logro alguna solución? Me encantaría hacer que el fondo del mismo tamaño que la imagen es, por lo que no hay material "invisible" a la izquierda, pero también estaré bien con la opción transparente.
Solución:
// 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();
Archivo 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>
Resultado final:
- Búsqueda de píxeles de borde de una imagen con envolvente transparente (para detección de colisión)
- Problema con la imagen que tiene fondo transparente en Android
- Cómo tener un ImageButton transparente: Android
- Cómo hacer Android Webview Background Transparente en KITKAT (4.4)
- GIF transparente en Android ImageView
- Totalmente transparente ActionBarSherlock utilizando el tema
- Mostrar una actividad transparente en Android?
- Crear un diálogo transparente encima de la actividad
Usted puede hacer un diálogo de encargo para mostrar imágenes, hacer una disposición para inflar en el setcontentview del diálogo, tomar un diseño linear con anchura y el contenido de la envoltura de la altura y el viewview con él con el fitxy de la característica de la escala.
Utilice FrameLayout como su padre en lugar de RelativeLayout y establezca en wrap_content y 0 márgenes.
Tratar:
<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>
El resultado debe ser un fondo del mismo tamaño que ImageView.
Si esto funciona. Intente modificar esto en un tema personalizado:
<style name="Widget.ImageWell"> <item name="android:background">@android:drawable/panel_picture_frame_background</item> </style>
Cambie su ImageView de esto:
<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" />
A esto:
<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" />
Espero que esto ayude.
¿Ha intentado setType en ImageView?
<ImageView ... android:scaleType="fitXY" ... />
- Acceder a google apis en el nuevo intel x86 android emulator
- Android RecyclerView debajo de la barra de herramientas