Example usage for android.graphics Canvas getHeight

List of usage examples for android.graphics Canvas getHeight

Introduction

In this page you can find the example usage for android.graphics Canvas getHeight.

Prototype

public int getHeight() 

Source Link

Document

Returns the height of the current drawing layer

Usage

From source file:Main.java

private static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }//from   w  ww.  ja  va  2  s  .  c  o m

    final Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}

From source file:org.microg.gms.maps.bitmap.ResourceBitmapDescriptor.java

public static Bitmap drawableToBitmap(Context context, Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }//from w ww  .  j ava2s .  c  o m

    if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
        return DefaultBitmapDescriptor.DEFAULT_DESCRIPTOR.loadBitmap(context);
    }

    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}

From source file:Main.java

/**
 * Convert a {@link Drawable} into an {@link Bitmap} object.
 * <p>//w  ww.  ja v  a  2 s  . c  om
 * Draws the {@code Drawable} onto a RAM-only {@link Canvas} and grabs the resulting
 * {@code Bitmap}.
 * </p>
 * <p>
 * If the {@code Drawable} is a {@link BitmapDrawable}, no conversion is needed, and no
 * conversion will be done.
 * </p>
 *
 * @param drawable The {@link Drawable} to convert. Can be any drawable.
 * @return A {@link Bitmap} representing the given {@code drawable}.
 */
public static Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap;

    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        if (bitmapDrawable.getBitmap() != null) {
            return bitmapDrawable.getBitmap();
        }
    }

    if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
        bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
        // Single color bitmap will be created of 1x1 pixel
    } else {
        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}

From source file:Main.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap = null;//from   ww w. j av a  2s . com

    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        if (bitmapDrawable.getBitmap() != null) {
            return bitmapDrawable.getBitmap();
        }
    }

    if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
        bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
    } else {
        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}

From source file:Main.java

public static Bitmap getBitmap(Drawable drawable) {
    Bitmap bitmap;/*  w ww  .  j  a  va  2s . c  om*/
    if (drawable instanceof BitmapDrawable) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    } else {
        if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
            bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.RGB_565);
        } else {
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                    Bitmap.Config.RGB_565);
        }
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
    }
    return bitmap;
}

From source file:net.e_fas.oss.tabijiman.PictureUtil.java

public static float[] getInitialScale(Bitmap bitmap, Canvas view) {

    // View?//from w ww.ja v  a2s  . c  o  m
    float FrameWidth = view.getWidth();
    float FrameHeight = view.getHeight();

    // bitmap?
    float imageWidth = bitmap.getWidth();
    float imageHeight = bitmap.getHeight();

    // X?Y?
    float scaleX = imageWidth / FrameWidth;
    float scaleY = imageHeight / FrameHeight;

    e_print("X >> " + scaleX + " Y >> " + scaleY);

    // ?????????????
    // ?????????
    //        return Math.min(scaleX, scaleY);
    return new float[] { scaleX, scaleY };
}

From source file:Main.java

public static Bitmap cropMaxVisibleBitmap(Drawable drawable, int iconSize) {
    Bitmap tmp = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            Bitmap.Config.ARGB_8888);//from ww w.  j av  a2  s .c  o  m
    Canvas canvas = new Canvas(tmp);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    Rect crop = new Rect(tmp.getWidth(), tmp.getHeight(), -1, -1);
    for (int y = 0; y < tmp.getHeight(); y++) {
        for (int x = 0; x < tmp.getWidth(); x++) {
            int alpha = (tmp.getPixel(x, y) >> 24) & 255;
            if (alpha > 0) { // pixel is not 100% transparent
                if (x < crop.left)
                    crop.left = x;
                if (x > crop.right)
                    crop.right = x;
                if (y < crop.top)
                    crop.top = y;
                if (y > crop.bottom)
                    crop.bottom = y;
            }
        }
    }

    if (crop.width() <= 0 || crop.height() <= 0) {
        return Bitmap.createScaledBitmap(tmp, iconSize, iconSize, true);
    }

    // We want to crop a square region.
    float size = Math.max(crop.width(), crop.height());
    float xShift = (size - crop.width()) * 0.5f;
    crop.left -= Math.floor(xShift);
    crop.right += Math.ceil(xShift);

    float yShift = (size - crop.height()) * 0.5f;
    crop.top -= Math.floor(yShift);
    crop.bottom += Math.ceil(yShift);

    Bitmap finalImage = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888);
    canvas.setBitmap(finalImage);
    float scale = iconSize / size;

    canvas.scale(scale, scale);
    canvas.drawBitmap(tmp, -crop.left, -crop.top, new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
    canvas.setBitmap(null);
    return finalImage;
}

From source file:com.danimahardhika.android.helpers.core.DrawableHelper.java

@Nullable
public static Drawable getResizedDrawable(@NonNull Context context, @NonNull Drawable drawable,
        float sizeInDp) {
    try {/*from   w ww  .j av  a2s .  com*/
        int size = Math.round(UnitHelper.toPixel(context, sizeInDp));

        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return new BitmapDrawable(context.getResources(), Bitmap.createScaledBitmap(bitmap, size, size, true));
    } catch (OutOfMemoryError e) {
        return null;
    }
}

From source file:com.dm.wallpaper.board.helpers.DrawableHelper.java

@Nullable
public static Drawable getDefaultImage(@NonNull Context context, @DrawableRes int res, @ColorInt int color,
        int padding) {
    try {/*w  ww  .j  a  va  2s .c  om*/
        Drawable drawable = AppCompatDrawableManager.get().getDrawable(context, res);
        drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            drawable = (DrawableCompat.wrap(drawable)).mutate();
        }

        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        Bitmap tintedBitmap = Bitmap.createBitmap(bitmap.getWidth() + padding, bitmap.getHeight() + padding,
                Bitmap.Config.ARGB_8888);
        Canvas tintedCanvas = new Canvas(tintedBitmap);
        int background = ColorHelper.getAttributeColor(context, R.attr.card_background);
        Paint paint = new Paint();
        paint.setFilterBitmap(true);
        paint.setAntiAlias(true);
        tintedCanvas.drawColor(background, PorterDuff.Mode.ADD);
        tintedCanvas.drawBitmap(bitmap, (tintedCanvas.getWidth() - bitmap.getWidth()) / 2,
                (tintedCanvas.getHeight() - bitmap.getHeight()) / 2, paint);
        return new BitmapDrawable(context.getResources(), tintedBitmap);
    } catch (Exception | OutOfMemoryError e) {
        return null;
    }
}

From source file:com.android.dialer.contactinfo.ContactPhotoLoader.java

private static Bitmap drawableToBitmap(Drawable drawable, int width, int height) {
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);//  w  ww. j  a  v a2  s.c om
    return bitmap;
}