Example usage for android.widget ImageView getDrawable

List of usage examples for android.widget ImageView getDrawable

Introduction

In this page you can find the example usage for android.widget ImageView getDrawable.

Prototype

public Drawable getDrawable() 

Source Link

Document

Gets the current Drawable, or null if no Drawable has been assigned.

Usage

From source file:Main.java

public static Uri getLocalBitmapUri(ImageView imageView) {
    // Extract Bitmap from ImageView drawable
    Drawable drawable = imageView.getDrawable();
    Bitmap bmp = null;//w ww  .  j a v  a2  s  .  c  o  m
    if (drawable instanceof BitmapDrawable) {
        bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
    } else {
        return null;
    }
    // Store image to default external storage directory
    Uri bmpUri = null;
    try {
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
                "share_image_" + System.currentTimeMillis() + ".png");
        file.getParentFile().mkdirs();
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.close();
        bmpUri = Uri.fromFile(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bmpUri;
}

From source file:Main.java

public static void recycleImageViewBitmap(ImageView imageView) {
    if (imageView == null)
        return;//from  w w w.  j  ava  2s .com

    Drawable drawable = imageView.getDrawable();
    if (drawable == null)
        return;

    imageView.setImageDrawable(null);

    BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
    Bitmap bitmap = bitmapDrawable.getBitmap();
    if (bitmap != null) {
        bitmap.recycle();
    }
}

From source file:Main.java

/**
 * <pre>/*w w  w .j a  v a 2s .c o m*/
 * Extract {@link Bitmap} object rendered by {@link ImageView}
 * </pre>
 */
public static Bitmap extractBitmap(ImageView imageView) {
    if (imageView == null)
        return null;
    Drawable drawable = imageView.getDrawable();
    if (drawable != null && drawable instanceof BitmapDrawable) {
        Bitmap bm = ((BitmapDrawable) drawable).getBitmap();
        return bm;
    }
    return null;
}

From source file:Main.java

public static void scaleImage(ImageView view, int boundBoxInDp) {
    // Get the ImageView and its bitmap
    Drawable drawing = view.getDrawable();
    Bitmap bitmap = ((BitmapDrawable) drawing).getBitmap();
    // Get current dimensions
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    // Determine how much to scale: the dimension requiring less scaling is
    // closer to the its side. This way the image always stays inside your
    // bounding box AND either x/y axis touches it.
    float xScale = ((float) boundBoxInDp) / width;
    float yScale = ((float) boundBoxInDp) / height;
    float scale = (xScale <= yScale) ? xScale : yScale;
    // Create a matrix for the scaling and add the scaling data
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);//from   ww  w.j av a2  s .  c  o m
    // Create a new bitmap and convert it to a format understood by the ImageView
    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    BitmapDrawable result = new BitmapDrawable(scaledBitmap);
    width = scaledBitmap.getWidth();
    height = scaledBitmap.getHeight();
    // Apply the scaled bitmap
    view.setImageDrawable(result);
    // Now change ImageView's dimensions to match the scaled image
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
    params.width = width;
    params.height = height;
    view.setLayoutParams(params);
}

From source file:com.shafiq.myfeedle.core.BitmapDownloadTask.java

private static BitmapDownloadTask getBitmapDownloadTask(ImageView imageView) {
    if (imageView != null) {
        Drawable drawable = imageView.getDrawable();
        if (drawable instanceof DownloadedDrawable) {
            DownloadedDrawable downloadedDrawable = (DownloadedDrawable) drawable;
            return downloadedDrawable.getBitmapDownloadTask();
        }//from   ww w .j  av  a 2  s .c  o m
    }
    return null;
}

From source file:Main.java

public static void scaleImageWithOriRatio(ImageView view, int boundBoxInDp) {
    // Get the ImageView and its bitmap
    Drawable drawing = view.getDrawable();
    Bitmap bitmap = ((BitmapDrawable) drawing).getBitmap();

    // Get current dimensions
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    // Determine how much to scale: the dimension requiring less scaling is
    // closer to the its side. This way the image always stays inside your
    // bounding box AND either x/y axis touches it.
    float xScale = ((float) boundBoxInDp) / width;
    float yScale = ((float) boundBoxInDp) / height;
    float scale = (xScale <= yScale) ? xScale : yScale;

    // Create a matrix for the scaling and add the scaling data
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);//w ww.j a v a 2  s .  com

    // Create a new bitmap and convert it to a format understood by the
    // ImageView
    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    BitmapDrawable result = new BitmapDrawable(scaledBitmap);
    width = scaledBitmap.getWidth();
    height = scaledBitmap.getHeight();

    // Apply the scaled bitmap
    view.setImageDrawable(result);

    // Now change ImageView's dimensions to match the scaled image
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
    params.width = width;
    params.height = height;
    view.setLayoutParams(params);
}

From source file:arun.com.chromer.settings.widgets.PreferenceIconLayoutHelper.java

/**
 * Finds the icon view and attempts to tint it based on the checked state of the preference
 *
 * @param imageView The image view of icon
 * @param checked   Whether the preference is enabled
 *///from  w ww. j  a v a  2s  .  co  m
private static void applyIconTint(@NonNull ImageView imageView, boolean checked) {
    final Drawable drawable = imageView.getDrawable();
    if (drawable != null) {
        if (drawable instanceof IconicsDrawable) {
            // Just redraw with the correct color
            imageView.setImageDrawable(
                    ((IconicsDrawable) drawable).color(checked ? CHECKED_COLOR : UNCHECKED_COLOR));
        } else {
            final Drawable wrap = DrawableCompat.wrap(drawable);
            DrawableCompat.setTint(wrap, checked ? CHECKED_COLOR : UNCHECKED_COLOR);
            imageView.setImageDrawable(drawable);
        }
    }
}

From source file:Main.java

/**
 * inject a image touch overley/*from  ww w  .  ja  v a 2  s . c  o  m*/
 * @param v
 * @param event
 */
public static boolean imageOverlay(View v, MotionEvent event) {
    ImageView view = (ImageView) v;
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        // overlay is black with transparency of 0x77 (119)
        view.getDrawable().setColorFilter(0x77000000, PorterDuff.Mode.SRC_ATOP);
        view.invalidate();
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL: {
        // clear the overlay
        view.getDrawable().clearColorFilter();
        view.invalidate();
        break;
    }
    }
    return false;
}

From source file:com.arlib.floatingsearchview.util.Util.java

public static void setIconColor(ImageView iconHolder, int color) {
    Drawable wrappedDrawable = DrawableCompat.wrap(iconHolder.getDrawable());
    DrawableCompat.setTint(wrappedDrawable, color);
    iconHolder.setImageDrawable(wrappedDrawable);
    iconHolder.invalidate();//from  w w w  .j ava2s  . c  o  m
}

From source file:Main.java

public static void freeImageView(ImageView imageView) {
    // This code behaves differently on various OS builds. That is why put into try catch.
    try {//from www  . j  a  v  a 2 s  .c o m
        if (imageView != null) {
            Drawable dr = imageView.getDrawable();

            if (dr == null) {
                return;
            }

            if (!(dr instanceof BitmapDrawable)) {
                return;
            }
            BitmapDrawable bd = (BitmapDrawable) imageView.getDrawable();
            if (bd.getBitmap() != null) {
                bd.getBitmap().recycle();
                imageView.setImageBitmap(null);
            }
        }
    } catch (Exception e) {
        Log.e("free image view", e.getMessage());
    }
}