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

private BitmapDrawable(BitmapState state, Resources res) 

Source Link

Usage

From source file:Main.java

/**
 * Get a BitmapDrawable from a local file that is scaled down
 * to fit the current Window size./*from  w w  w  .jav  a2 s  .c o  m*/
 */
@SuppressWarnings("deprecation")
static BitmapDrawable getScaledBitmap(String path, Activity a) {
    Display display = a.getWindowManager().getDefaultDisplay();
    float destWidth = display.getWidth();
    float destHeight = display.getHeight();

    // read in the dimensions of the image on disk
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    float srcWidth = options.outWidth;
    float srcHeight = options.outHeight;

    int inSampleSize = 1;
    if (srcHeight > destHeight || srcWidth > destWidth) {
        if (srcWidth > srcHeight) {
            inSampleSize = Math.round(srcHeight / destHeight);
        } else {
            inSampleSize = Math.round(srcWidth / destWidth);
        }
    }

    options = new BitmapFactory.Options();
    options.inSampleSize = inSampleSize;

    Bitmap bitmap = BitmapFactory.decodeFile(path, options);
    return new BitmapDrawable(a.getResources(), bitmap);
}

From source file:Main.java

public static Drawable bitmapToDrawable(Bitmap bitmap, Resources res) {
    if (bitmap == null) {
        return null;
    }//from ww w  . j a  v  a2  s .c o  m
    return new BitmapDrawable(res, bitmap);
}

From source file:Main.java

/**
 * Get a BitmapDrawable from a local file that is scaled down
 * to fit the current Window size./*from w  w w. j ava2 s  .  c  o  m*/
 */
@SuppressWarnings("deprecation")
public static BitmapDrawable getScaledDrawable(Activity a, String path) {
    Display display = a.getWindowManager().getDefaultDisplay();
    float destWidth = display.getWidth();
    float destHeight = display.getHeight();

    // read in the dimensions of the image on disk
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    float srcWidth = options.outWidth;
    float srcHeight = options.outHeight;

    int inSampleSize = 1;
    if (srcHeight > destHeight || srcWidth > destWidth) {
        if (srcWidth > srcHeight) {
            inSampleSize = Math.round((float) srcHeight / ((float) destHeight) * 3);
        } else {
            inSampleSize = Math.round((float) srcWidth / ((float) destWidth) * 3);
        }
    }

    options = new BitmapFactory.Options();
    options.inSampleSize = inSampleSize;

    Bitmap bitmap = BitmapFactory.decodeFile(path, options);
    return new BitmapDrawable(a.getResources(), bitmap);
}

From source file:Main.java

public static BitmapDrawable writeOnDrawable(Activity actv, Resources res, int drawableId, String text,
        int textSize) {

    Bitmap bm = BitmapFactory.decodeResource(res, drawableId).copy(Bitmap.Config.ARGB_8888, true);

    DisplayMetrics dm = new DisplayMetrics();
    actv.getWindowManager().getDefaultDisplay().getMetrics(dm);

    int pixelSize = (int) ((textSize * dm.scaledDensity));

    if (text.length() > 2) {
        pixelSize = (int) ((textSize * dm.scaledDensity) * (0.5 - (text.length() / 10)));
    }/*ww w.ja  v  a  2 s.c  o  m*/

    Paint paint = new Paint();
    paint.setStyle(Style.FILL);
    paint.setColor(Color.WHITE);
    paint.setTextSize(pixelSize);
    paint.setTextAlign(Paint.Align.CENTER);

    // float adjust = paint.measureText(text);

    Canvas canvas = new Canvas(bm);
    int xPos = (int) ((bm.getWidth() / 2));
    int yPos = (int) ((bm.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2));

    canvas.drawText(text, xPos, yPos, paint);

    return new BitmapDrawable(res, bm);
}

From source file:Main.java

public static Drawable load_drawable_from_uri_string(Context context, String uri, int sizeX, int sizeY) {
    Drawable drawable = null;//from  w w w. ja va 2s  . co m
    Uri img_uri = Uri.parse(uri);
    if (uri != null) {
        try {

            InputStream inputStream = context.getContentResolver().openInputStream(img_uri);
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            Bitmap small_bitmap = Bitmap.createScaledBitmap(bitmap, sizeX, sizeY, false);
            bitmap.recycle();
            drawable = new BitmapDrawable(context.getResources(), small_bitmap);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    return drawable;
}

From source file:Main.java

private static Drawable recolor(Resources res, Drawable drawable, int color) {
    if (drawable == null) {
        return null;
    }//from w w w .  j  av  a 2 s .  co m

    Bitmap outBitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(outBitmap);
    drawable.setBounds(0, 0, outBitmap.getWidth(), outBitmap.getHeight());
    drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
    drawable.draw(canvas);
    drawable.setColorFilter(null);
    drawable.setCallback(null); // free up any references
    return new BitmapDrawable(res, outBitmap);
}

From source file:Main.java

public static Bitmap flattenExtensionIcon(Context context, Bitmap baseIcon, int color) {
    return flattenExtensionIcon(new BitmapDrawable(context.getResources(), baseIcon), color);
}

From source file:Main.java

public static Drawable rotateBitmap(Bitmap inputBitmap) {
    Matrix matrix = new Matrix();
    matrix.setRotate(90, (float) inputBitmap.getWidth() / 2, (float) inputBitmap.getHeight() / 2);

    float outputX = inputBitmap.getHeight();
    float outputY = 0;

    final float[] values = new float[9];
    matrix.getValues(values);//  ww  w  . j a  v a  2s  .  c o m
    float x1 = values[Matrix.MTRANS_X];
    float y1 = values[Matrix.MTRANS_Y];
    matrix.postTranslate(outputX - x1, outputY - y1);
    Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap.getHeight(), inputBitmap.getWidth(),
            Bitmap.Config.ARGB_8888);
    Paint paint = new Paint();
    Canvas canvas = new Canvas(outputBitmap);
    canvas.drawBitmap(inputBitmap, matrix, paint);
    return new BitmapDrawable(null, outputBitmap);
}

From source file:Main.java

public static Drawable createGradientColorAndCover(Context context, int coveredResID, int CoverColor) {
    //final Resources res=context.getResources();
    final Resources res = context.getResources();
    Bitmap bitmap = BitmapFactory.decodeResource(res, coveredResID);
    if (bitmap == null) {
        return null;
    }//from  w w w  .  ja  v a  2 s. com
    final int length = bitmap.getWidth();
    final int height = bitmap.getHeight();
    int[] shape = new int[length * height];
    int[] colors = new int[length * height];
    bitmap.getPixels(shape, 0, length, 0, 0, length, height);
    int color = CoverColor;
    for (int i = 0; i < length * height; i++) {
        float percent = ((float) i % length / length) * 0xff;
        int alpha = ((int) percent << 6 * 4);
        alpha &= shape[i] & 0xFF000000;
        colors[i] = (alpha) | (color & 0x00FFFFFF);
    }
    Bitmap newbitmap = Bitmap.createBitmap(length, height, Bitmap.Config.ARGB_8888);
    newbitmap.setPixels(colors, 0, length, 0, 0, length, height);
    Bitmap fooBitmap = Bitmap.createBitmap(length, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(fooBitmap);
    Paint paint = new Paint();
    canvas.drawBitmap(bitmap, 0, 0, paint);
    canvas.drawBitmap(newbitmap, 0, 0, paint);
    newbitmap.recycle();
    bitmap.recycle();
    return new BitmapDrawable(res, fooBitmap);
}

From source file:Main.java

private static Drawable getMoreSuggestionsHint(final Resources res, final float textSize, final int color) {
    final Paint paint = new Paint();
    paint.setAntiAlias(true);/*from   w ww  . j  a v  a  2 s .  com*/
    paint.setTextAlign(Align.CENTER);
    paint.setTextSize(textSize);
    paint.setColor(color);
    final Rect bounds = new Rect();
    paint.getTextBounds(MORE_SUGGESTIONS_HINT, 0, MORE_SUGGESTIONS_HINT.length(), bounds);
    final int width = Math.round(bounds.width() + 0.5f);
    final int height = Math.round(bounds.height() + 0.5f);
    final Bitmap buffer = Bitmap.createBitmap(width, (height * 3 / 2), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(buffer);
    canvas.drawText(MORE_SUGGESTIONS_HINT, width / 2, height, paint);
    return new BitmapDrawable(res, buffer);
}