Example usage for android.graphics.drawable BitmapDrawable BitmapDrawable

List of usage examples for android.graphics.drawable BitmapDrawable BitmapDrawable

Introduction

In this page you can find the example usage for android.graphics.drawable BitmapDrawable BitmapDrawable.

Prototype

@Deprecated
public BitmapDrawable(java.io.InputStream is) 

Source Link

Document

Create a drawable by decoding a bitmap from the given input stream.

Usage

From source file:Main.java

/**
 * Blur the view's background, for the original idea please refer to:
 * http://www.cnblogs.com/lipeil/p/3997992.html
 * @param context//from   w w w  .  java  2s .c  o m
 * @param view
 */
public static void blur(Context context, View view) {
    Bitmap bitmap = ((BitmapDrawable) view.getBackground()).getBitmap();
    bitmap = blur(context, bitmap);
    view.setBackground(new BitmapDrawable(bitmap));
}

From source file:Main.java

public static Drawable toGreyDrawable(Drawable drawable) {
    int w = drawable.getMinimumWidth();
    int h = drawable.getMinimumHeight();
    if (w <= 0 || h <= 0) {
        return drawable;
    }/*ww  w  . j  av  a2s  . c om*/
    Rect bounds = drawable.getBounds();
    Bitmap grey = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(grey);
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    drawable.setColorFilter(new ColorMatrixColorFilter(cm));
    drawable.setBounds(0, 0, w, h);
    drawable.draw(c);
    drawable.clearColorFilter();
    drawable.setBounds(bounds);
    BitmapDrawable bd = new BitmapDrawable(grey);
    bd.setBounds(0, 0, w, h);
    return bd;
}

From source file:Main.java

public static Drawable view2Drawable(View view) {
    BitmapDrawable mBitmapDrawable = null;
    try {// w  w  w.j ava 2  s  .c  o m
        Bitmap newbmp = view2Bitmap(view);
        if (newbmp != null) {
            mBitmapDrawable = new BitmapDrawable(newbmp);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mBitmapDrawable;
}

From source file:Main.java

public static Drawable resizeImage(Bitmap bitmap, int w, int h) {

    // load the origial Bitmap
    Bitmap BitmapOrg = bitmap;//from   w  ww .ja  v a2 s  .  c o  m

    int width = BitmapOrg.getWidth();
    int height = BitmapOrg.getHeight();
    int newWidth = w;
    int newHeight = h;

    // calculate the scale
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the Bitmap
    matrix.postScale(scaleWidth, scaleHeight);
    // if you want to rotate the Bitmap
    // matrix.postRotate(45);

    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width, height, matrix, true);

    // make a Drawable from Bitmap to allow to set the Bitmap
    // to the ImageView, ImageButton or what ever
    return new BitmapDrawable(resizedBitmap);

}

From source file:Main.java

public static Drawable zoomDrawable(Drawable drawable, int w, int h) {
    if (null == drawable || w < 0 || h < 0) {
        return null;
    }/*  w w w . ja  v  a  2  s .  co  m*/

    try {
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        Bitmap oldbmp = drawableToBitmap(drawable);
        Matrix matrix = new Matrix();
        float scaleWidth = 1;
        float scaleHeight = 1;
        if (w > 0) {
            scaleWidth = ((float) w / width);
        }
        if (h > 0) {
            scaleHeight = ((float) h / height);
        }

        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true);
        return new BitmapDrawable(newbmp);
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static TransitionDrawable bitmapToTransitionDrawable(Bitmap bitmap) {
    TransitionDrawable mBitmapDrawable = null;
    try {//from w ww . j a v a2  s  .  c om
        if (bitmap == null) {
            return null;
        }
        mBitmapDrawable = new TransitionDrawable(
                new Drawable[] { new ColorDrawable(android.R.color.transparent), new BitmapDrawable(bitmap) });
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mBitmapDrawable;
}

From source file:Main.java

public static TransitionDrawable bitmapToTransitionDrawable(Bitmap bitmap) {
    TransitionDrawable mBitmapDrawable = null;
    try {//from  ww  w .j  a  v  a2s .c o m
        if (bitmap == null) {
            return null;
        }
        mBitmapDrawable = new TransitionDrawable(
                new Drawable[] { new ColorDrawable(transparent), new BitmapDrawable(bitmap) });
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mBitmapDrawable;
}

From source file:Main.java

/** simply resizes a given drawable resource to the given width and height */
public static Drawable resizeImage(Context ctx, int resId, int iconWidth, int iconHeight) {

    // load the origial Bitmap
    Bitmap BitmapOrg = BitmapFactory.decodeResource(ctx.getResources(), resId);

    int width = BitmapOrg.getWidth();
    int height = BitmapOrg.getHeight();
    int newWidth = iconWidth;
    int newHeight = iconHeight;

    // calculate the scale
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the Bitmap
    matrix.postScale(scaleWidth, scaleHeight);

    // if you want to rotate the Bitmap
    // matrix.postRotate(45);

    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width, height, matrix, true);

    // make a Drawable from Bitmap to allow to set the Bitmap
    // to the ImageView, ImageButton or what ever
    return new BitmapDrawable(resizedBitmap);

}

From source file:Main.java

public static BitmapDrawable sizeChanger(Context context, ImageView imageView, int sizeOnDps, int orientation) {
    Drawable drawing = imageView.getDrawable();
    if (drawing == null) {
        return null;
    }/*from   w  w w .  j  a  va  2s  .com*/
    Bitmap bitmap = ((BitmapDrawable) drawing).getBitmap();

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int bounding = dpToPx(250, context);

    float xScale = ((float) bounding) / width;
    float yScale = ((float) bounding) / height;
    float scale = (xScale <= yScale) ? xScale : yScale;

    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);
    matrix.postRotate(orientation, imageView.getDrawable().getBounds().width() / 2,
            imageView.getDrawable().getBounds().height() / 2);

    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    width = scaledBitmap.getWidth();
    height = scaledBitmap.getHeight();
    return new BitmapDrawable(scaledBitmap);
}

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 w w  w.  ja  va 2  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);
}