Example usage for android.graphics Bitmap getWidth

List of usage examples for android.graphics Bitmap getWidth

Introduction

In this page you can find the example usage for android.graphics Bitmap getWidth.

Prototype

public final int getWidth() 

Source Link

Document

Returns the bitmap's width

Usage

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
    try {//  ww  w.j  av  a  2 s .  com
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    } catch (Exception e) {
        e.printStackTrace();
        return bitmap;
    }
}

From source file:Main.java

public static byte[] getBytePixels(final Bitmap bit) {
    if (bit == null) {
        return null;
    }/*from w  ww .j  av  a  2s.  c om*/
    final byte[] pixels = new byte[bit.getWidth() * bit.getHeight() * 4];
    final ByteBuffer buf = ByteBuffer.wrap(pixels);
    buf.order(ByteOrder.nativeOrder());
    bit.copyPixelsToBuffer(buf);
    return pixels;
}

From source file:Main.java

public static Drawable get_scaled_drawable_from_uri_string_for_square_container(Context context, String uri,
        int max_size) {
    Drawable drawable = null;//w w  w  . ja  v a 2 s  .co  m
    Uri img_uri = Uri.parse(uri);
    if (uri != null) {
        try {

            InputStream inputStream = context.getContentResolver().openInputStream(img_uri);
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            int sx = bitmap.getWidth();
            int sy = bitmap.getHeight();

            int fsx = max_size;
            int fsy = max_size;

            if (sy > sx)
                fsx = (int) ((float) max_size * ((float) sx / (float) sy));
            else if (sx > sy)
                fsy = (int) ((float) max_size * ((float) sy / (float) sx));

            Bitmap small_bitmap = Bitmap.createScaledBitmap(bitmap, fsx, fsy, false);
            bitmap.recycle();
            drawable = new BitmapDrawable(context.getResources(), small_bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    return drawable;
}

From source file:Main.java

public static Bitmap flipAnimation(Bitmap animationTexture, int subImageWidth, int subImageHeight) {
    Bitmap animationTextureFlipped = Bitmap.createBitmap(animationTexture.getWidth(),
            animationTexture.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(animationTextureFlipped);
    Bitmap b;/*from  ww w. j  ava2  s.co  m*/
    for (int i = 0; (i + 1) * subImageHeight <= animationTexture.getHeight(); i++) {
        for (int j = 0; (j + 1) * subImageWidth <= animationTexture.getWidth(); j++) {
            b = Bitmap.createBitmap(animationTexture, j * subImageWidth, i * subImageHeight, subImageWidth,
                    subImageHeight);
            b = flip(b);
            canvas.drawBitmap(b, j * subImageWidth, i * subImageHeight, null);
        }
    }
    return animationTextureFlipped;
}

From source file:Main.java

public static Bitmap resizeBitmapByScale(final Bitmap bitmap, final float scale, final boolean recycle) {
    final int width = Math.round(bitmap.getWidth() * scale);
    final int height = Math.round(bitmap.getHeight() * scale);
    if (width == bitmap.getWidth() && height == bitmap.getHeight())
        return bitmap;
    final Bitmap target = Bitmap.createBitmap(width, height, getConfig(bitmap));
    final Canvas canvas = new Canvas(target);
    canvas.scale(scale, scale);/*from   ww w .j a v  a  2s .c  o m*/
    final Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    if (recycle) {
        bitmap.recycle();
    }
    return target;
}

From source file:Main.java

/**
 * This method is used to create bitmap with rounded corners.
 * /*from  www  .j a  va 2 s  . com*/
 * @param bitmap
 *            which is to be rouded.
 * @param pixels
 *            dimension of the resultant image
 * @return new bitmap with the specified dimension
 */
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = pixels;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

From source file:Main.java

public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
    final int reflectionGap = 4;
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);//ww w  .jav a 2  s. c om

    Bitmap reflectionImage;
    reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w, h / 2, matrix, false);

    Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint defaultPaint = new Paint();
    canvas.drawRect(0, h, w, h + reflectionGap, defaultPaint);

    canvas.drawBitmap(reflectionImage, 0, h + reflectionGap, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, Shader.TileMode.CLAMP);
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, h, w, bitmapWithReflection.getHeight() + reflectionGap, paint);

    return bitmapWithReflection;
}

From source file:Main.java

public static Bitmap zoom(Bitmap bitmap, float zf) {
    Matrix matrix = new Matrix();
    matrix.postScale(zf, zf);// ww w  . j a  v  a 2s.  c  o m
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}

From source file:Main.java

public static Bitmap rotateBitmap(Bitmap bitmap, int rotationAngle) {
    mMatrix.reset();/*from  w w w . ja v  a 2  s  .c  o m*/
    mMatrix.postRotate(rotationAngle);
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mMatrix, false);
}