Skip to content

SimpleImageDialog

Eltos edited this page Oct 29, 2021 · 9 revisions

Image dialog

extends CustomViewDialog

API reference Examples

A Dialog that displays an image, e.g. a QR code

Usage

For general usage see SimpleDialog.

Additional methods are provided to set the image URI or resource (image), scale type (scaleType) etc. The scale type can be one of:

  • Scale.FIT: scales the image down ensuring the image is fully visible
  • Scale.SCROLL_HORIZONTAL: scales the image up, allowing horizontal scrolling ("panorama")
  • Scale.SCROLL_VERTICAL: scales the image up, allowing vertical scrolling

Please refer to the API reference for a comprehensive documentation of these methods.

Theme

See Styles.

Note that you have to use ImageDialogTheme as parent for custom themes when styling this dialog, otherwise the dialog will not shrink to the image and there will be white stripes on either side of the image.

Image creators

If the image is not available as URI or resource, call image by passing a class that implements one of BitmapCreator, DrawableCreator or IconCreator to image. This ensures that the image can be re-created even after rotation changes.

Here is an example that uses BitmapCreate to display a QR code:

private static final String QR_CONTENT = "qrContent";


Bundle extra = new Bundle();
extra.putString(QR_CONTENT, "https://github.com/eltos/SimpleDialogFragments");

SimpleImageDialog.build()
        .image(QrBuilder.class)
        .extra(extra)
        .show(MainActivity.this);



public static class QrBuilder implements SimpleImageDialog.BitmapCreator {
    public QrBuilder(){}

    @Override
    public Bitmap create(@Nullable String tag, @NonNull Bundle extras) {
        String content = extras.getString(QR_CONTENT);
        if (content == null) return null;
        
        try {
            // Generate QR code using com.google.zxing
            EnumMap<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q);
            hints.put(EncodeHintType.CHARACTER_SET, "UTF8");
            BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, 1024, 1024, hints);

            // Convert to bitmap
            Bitmap qr = Bitmap.createBitmap(bitMatrix.getWidth(), bitMatrix.getHeight(), Bitmap.Config.RGB_565);
            for (int y = 0; y < qr.getHeight(); y++) {
                for (int x = 0; x < qr.getWidth(); x++) {
                    qr.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
                }
            }
            return qr;
        } catch (WriterException ignored) {
            return null;
        }
    }
}

Receiving results

For general usage see SimpleDialog.

Examples

SimpleImageDialog.build()
                 .image(R.drawable.image_sample)
                 .show(this);
// Using QrBuilder as defined above

Bundle extra = new Bundle();
extra.putString(QR_CONTENT, "https://github.com/eltos/SimpleDialogFragments");

SimpleImageDialog.build()
                 .image(QrBuilder.class)
                 .extra(extra)
                 .show(MainActivity.this);
Clone this wiki locally