Example usage for android.graphics Canvas getWidth

List of usage examples for android.graphics Canvas getWidth

Introduction

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

Prototype

public int getWidth() 

Source Link

Document

Returns the width of the current drawing layer

Usage

From source file:com.abhinavjhanwar.android.egg.neko.NekoShortcuts.java

private static Bitmap getDarkIcon(Context context, int drawableId) {
    Drawable drawable = ContextCompat.getDrawable(context, drawableId);
    if (drawable != null) {
        drawable.setColorFilter(0xFF616161, PorterDuff.Mode.MULTIPLY);
        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);/*from w w w .  j  a  va 2 s  .co  m*/
        return bitmap;
    } else {
        throw new IllegalArgumentException("unsupported drawable type");
    }
}

From source file:Main.java

/**
 * Create darkened version of input drawable offset.
 *
 * @param res/*from   ww  w  .ja  va2s  .  c om*/
 * @param inImage
 * @param offsetX
 * @param offsetY
 * @param blurRadius   Not currently used.
 * @param shadowColor
 * @return
 */
public static BitmapDrawable shadowImage(Resources res, Drawable inImage, int offsetX, int offsetY,
        float blurRadius, int shadowColor) {

    Bitmap inBitmap;
    if (inImage instanceof BitmapDrawable) {
        inBitmap = ((BitmapDrawable) inImage).getBitmap();
    } else {
        // Bitmap from drawable
        int imgWidth = inImage.getIntrinsicWidth();
        int imgHeight = inImage.getIntrinsicHeight();
        inBitmap = Bitmap.createBitmap(imgWidth, imgHeight, Bitmap.Config.ARGB_8888);
        Canvas bottomCanvas = new Canvas(inBitmap);
        inImage.setBounds(0, 0, bottomCanvas.getWidth(), bottomCanvas.getHeight());
        inImage.draw(bottomCanvas);
    }

    Bitmap blurBitmap = shadowBitmap(inBitmap, offsetX, offsetY, blurRadius, shadowColor);
    return new BitmapDrawable(res, blurBitmap);
}

From source file:com.jameswolfeoliver.pigeon.Utilities.Utils.java

public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
    Drawable drawable = ContextCompat.getDrawable(context, drawableId);
    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            Bitmap.Config.ARGB_8888);/*  w w  w .j ava  2s  .  co  m*/
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}

From source file:com.destin.sehaikun.DrawableUtils.java

public static Bitmap getBitmapFromDrawable(Drawable drawable) {
    if (drawable == null) {
        return null;
    }/*ww  w.ja va 2 s.c  om*/

    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }

    try {
        Bitmap bitmap;

        if (drawable instanceof ColorDrawable) {
            bitmap = Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888);
        } 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;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:org.y20k.trackbook.helpers.NotificationHelper.java

private static Bitmap getBitmap(int resource) {
    VectorDrawableCompat drawable = VectorDrawableCompat.create(mService.getResources(), resource, null);
    if (drawable != null) {
        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);//from w w  w  . j  a va 2 s . co  m
        return bitmap;
    } else {
        return null;
    }
}

From source file:com.dm.material.dashboard.candybar.helpers.DrawableHelper.java

@Nullable
public static Drawable getResizedDrawable(@NonNull Context context, @DrawableRes int drawableRes,
        @DimenRes int dimenRes) {
    try {/*from w ww  .j  ava  2 s  . c  om*/
        Drawable drawable = getDrawable(context, drawableRes);
        if (drawable == null)
            return null;

        int size = context.getResources().getDimensionPixelSize(dimenRes);

        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 (Exception | OutOfMemoryError e) {
        LogUtil.d(Log.getStackTraceString(e));
        return null;
    }
}

From source file:com.dm.material.dashboard.candybar.helpers.DrawableHelper.java

@Nullable
public static Drawable getDefaultImage(@NonNull Context context, @DrawableRes int res) {
    try {/*from  w ww  . j av a  2  s .  co  m*/
        int color = ColorHelper.getAttributeColor(context, android.R.attr.textColorSecondary);
        int padding = context.getResources().getDimensionPixelSize(R.dimen.default_image_padding);

        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.zandbee.floatingtitlebar.FloatingTitleBarActivity.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }/*w  w w  . ja  v a2  s  . com*/

    final Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            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:Main.java

public static Bitmap setupFrame(Bitmap bitmap, int width, int color) {
    if (bitmap.getWidth() <= width * 2 || bitmap.getHeight() <= width * 2) {
        return bitmap;
    }//from www .  j av  a2s  . c  om

    Bitmap bp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);

    Canvas canvas = new Canvas(bp);
    canvas.drawBitmap(bitmap, 0, 0, new Paint());
    Paint paint = new Paint();
    paint.setColor(color);
    paint.setStrokeWidth(width);
    paint.setStyle(Style.STROKE);
    canvas.drawRect(0, 0, canvas.getWidth() - width, canvas.getHeight() - width, paint);

    bitmap.recycle();
    return bp;
}

From source file:org.xbmc.kore.utils.Utils.java

/**
 * Converts a drawable to a bitmap/*from w  w w  . ja v  a2  s  .  c  o  m*/
 * @param drawable Drawable to convert
 * @return Bitmap
 */
public static Bitmap drawableToBitmap(Drawable drawable, int width, int height) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }

    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);

    return bitmap;
}