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.tr4android.support.extension.widget.CircleImageView.java

/**
 * Helper for creating a bitmap from a drawable
 *
 * @param drawable The drawable which should be converted to a bitmap
 * @return the bitmap containing the drawable
 *//*from   w w  w .  j a  v  a  2s.c o m*/
public static Bitmap getBitmapFromDrawable(Drawable drawable) {
    if (drawable == null)
        return null;
    if (drawable instanceof BitmapDrawable) {
        Log.w(LOG_TAG, "For better performance consider using setImageBitmap() instead!");
        return ((BitmapDrawable) drawable).getBitmap();
    } else {
        Bitmap bitmap = Bitmap.createBitmap(Math.max(2, drawable.getIntrinsicWidth()),
                Math.max(2, 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 void clearCanvas(Canvas canvas) {
    if (USE_DRAWCOLOR_TO_CLEAR_CANVAS) {
        if (USE_DRAWCOLOR_MODE_CLEAR) {
            canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        } else {//  ww  w.  ja v a  2  s  .  co  m
            canvas.drawColor(Color.TRANSPARENT);
        }
    } else {
        RECT.set(0, 0, canvas.getWidth(), canvas.getHeight());
        clearCanvas(canvas, RECT);
    }
}

From source file:com.xxxifan.devbox.core.util.ViewUtils.java

public static Bitmap toBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }/*from   w  w w.j  a  v a2 s .c o  m*/

    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:me.zhang.bingo.Utility.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap;/*from  w w w .jav  a2  s . c om*/

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

    if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
        // Single color bitmap will be created of 1x1 pixel.
        bitmap = Bitmap.createBitmap(1, 1, 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;
}

From source file:com.tafayor.selfcamerashot.taflib.helpers.GraphicsHelper.java

public static Bitmap drawableToBitmap(Drawable drawable, int width, int height) {
    Bitmap bitmap;// w  w w  .  jav a 2s .co m

    bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

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

    /*
    The drawable may be composite of bitmap and drawable layers
     */
    if (drawable instanceof BitmapDrawable) {
        Bitmap bmp = ((BitmapDrawable) drawable).getBitmap();
        canvas.drawBitmap(bmp, (canvas.getWidth() - bmp.getWidth()) / 2,
                (canvas.getHeight() - bmp.getHeight()) / 2, null);
    }
    drawable.draw(canvas);

    return bitmap;
}

From source file:org.bottiger.podcast.utils.UIUtils.java

public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
    Drawable drawable = ContextCompat.getDrawable(context, drawableId);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        drawable = (DrawableCompat.wrap(drawable)).mutate();
    }/* www .  j a va2 s  .c om*/

    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:com.ecuamobi.deckwallet.util.Renderer.java

public static void printQR(final Activity context, final String addressUri) {
    new AsyncTask<Void, Void, Bitmap>() {

        @Override/* w w w.  ja  va2s . c o m*/
        protected Bitmap doInBackground(Void... params) {
            TextPaint textPaint = new TextPaint();
            textPaint.setAntiAlias(true);
            textPaint.setColor(0xFF000000);
            final int bitmapMargin = 100;//big margin is to prevent possible clipping
            final int textHeight = 28;
            textPaint.setTextSize(textHeight);
            textPaint.setTextAlign(Paint.Align.CENTER);
            final int qrCodePadding = (int) (textPaint.descent() * 2);
            int textWidth = getTextWidth(addressUri, textPaint);
            QRCode addressQrCode = QRCode.getMinimumQRCode(addressUri, ErrorCorrectLevel.M);
            Bitmap addressQrCodeBitmap = addressQrCode.createImage(textWidth);
            Bitmap bmp = Bitmap.createBitmap(textWidth + bitmapMargin * 2,
                    addressQrCodeBitmap.getHeight() + qrCodePadding * 2 + bitmapMargin * 2,
                    Bitmap.Config.RGB_565);
            Canvas canvas = new Canvas(bmp);
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL);
            paint.setARGB(0xFF, 0xFF, 0xFF, 0xFF);
            paint.setAntiAlias(false);
            canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);

            int centerXForAddress = bitmapMargin + textWidth / 2;
            int y = bitmapMargin + qrCodePadding;
            Paint qrCodePaint = new Paint();
            qrCodePaint.setAntiAlias(false);
            qrCodePaint.setDither(false);
            canvas.drawBitmap(addressQrCodeBitmap, centerXForAddress - addressQrCodeBitmap.getWidth() / 2, y,
                    qrCodePaint);
            y += qrCodePadding - textPaint.ascent();
            canvas.drawText(addressUri, centerXForAddress, y + addressQrCodeBitmap.getHeight(), textPaint);
            return bmp;
        }

        @Override
        protected void onPostExecute(final Bitmap bitmap) {
            if (bitmap != null) {
                //DEBUG
                //                    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(context);
                //                    android.widget.ImageView view = new android.widget.ImageView(context);
                //                    view.setImageBitmap(bitmap);
                //                    builder.setView(view);
                //                    builder.setPositiveButton(android.R.string.ok, null);
                //                    builder.show();

                PrintHelper printHelper = new PrintHelper(context);
                printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT);
                printHelper.printBitmap(addressUri, bitmap);
            }

        }
    }.execute();

}

From source file:cc.softwarefactory.lokki.android.utilities.Utils.java

public static Bitmap getDefaultAvatarInitials(Context context, String text) {

    Log.e(TAG, "getDefaultAvatarInitials");

    String initials = getInitials(text);

    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.WHITE);//  w w w.  j  a  va 2  s . com
    paint.setTextSize(36);
    paint.setStrokeWidth(4);
    paint.setTextAlign(Paint.Align.CENTER);

    Bitmap bm = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bm);
    canvas.drawColor(context.getResources().getColor(R.color.material_blue_300));

    int distanceFromBaseline = (int) ((paint.descent() + paint.ascent()) / 2);
    int xPos = (canvas.getWidth() / 2);
    int yPos = (canvas.getHeight() / 2) - distanceFromBaseline;
    canvas.drawText(initials, xPos, yPos, paint);

    return bm;
}

From source file:com.ecuamobi.deckwallet.util.Renderer.java

static void printWallet(final Activity context, final String label, final String addressUri,
        final String privateKey) {
    new AsyncTask<Void, Void, Bitmap>() {

        @Override//from  w  w w .j av  a2s  .  co m
        protected Bitmap doInBackground(Void... params) {

            TextPaint textPaint = new TextPaint();
            textPaint.setAntiAlias(true);
            textPaint.setColor(0xFF000000);
            final int bitmapMargin = 100;//big margin is to prevent possible clipping
            final int textHeight = 28;
            final int spaceBetweenQrCodes = 60;
            textPaint.setTextSize(textHeight);
            textPaint.setTextAlign(Paint.Align.CENTER);
            final int qrCodePadding = (int) (textPaint.descent() * 2);
            Rect bounds = new Rect();
            textPaint.getTextBounds(privateKey, 0, privateKey.length(), bounds);
            int textWidth = getTextWidth(privateKey, textPaint);
            ArrayList<String> labelLinesRelaxed = wrap(label, textWidth, false, textPaint);
            for (String titleLine : labelLinesRelaxed) {
                textWidth = Math.max(textWidth, getTextWidth(titleLine, textPaint));
            }
            textWidth = Math.max(textWidth, getTextWidth(addressUri, textPaint));
            QRCode privateKeyQrCode = QRCode.getMinimumQRCode(privateKey, ErrorCorrectLevel.M);
            Bitmap privateKeyQrCodeBitmap = privateKeyQrCode.createImage(textWidth);
            QRCode addressQrCode = QRCode.getMinimumQRCode(addressUri, ErrorCorrectLevel.M);
            Bitmap addressQrCodeBitmap = addressQrCode.createImage(textWidth);
            ArrayList<String> labelLines = wrap(label, textWidth, true, textPaint);
            Bitmap bmp = Bitmap.createBitmap(
                    textWidth * 2 + bitmapMargin * 2 + spaceBetweenQrCodes, privateKeyQrCodeBitmap.getHeight()
                            + textHeight * (labelLines.size() + 1) + qrCodePadding * 2 + bitmapMargin * 2,
                    Bitmap.Config.RGB_565);
            Canvas canvas = new Canvas(bmp);
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL);
            paint.setARGB(0xFF, 0xFF, 0xFF, 0xFF);
            paint.setAntiAlias(false);
            canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);

            int centerXForAddress = bitmapMargin + textWidth / 2;
            int centerXForPrivateKey = bitmapMargin + textWidth + spaceBetweenQrCodes + textWidth / 2;
            int y = (int) (bitmapMargin - textPaint.ascent());
            for (int i = 0; i < labelLines.size(); i++) {
                canvas.drawText(labelLines.get(i), centerXForPrivateKey, y + i * textHeight, textPaint);
            }
            y = bitmapMargin + labelLines.size() * textHeight + qrCodePadding;
            Paint qrCodePaint = new Paint();
            qrCodePaint.setAntiAlias(false);
            qrCodePaint.setDither(false);
            canvas.drawBitmap(addressQrCodeBitmap, centerXForAddress - addressQrCodeBitmap.getWidth() / 2, y,
                    qrCodePaint);
            canvas.drawBitmap(privateKeyQrCodeBitmap,
                    centerXForPrivateKey - privateKeyQrCodeBitmap.getWidth() / 2, y, qrCodePaint);
            y += qrCodePadding - textPaint.ascent();
            canvas.drawText(addressUri, centerXForAddress, y + addressQrCodeBitmap.getHeight(), textPaint);
            canvas.drawText(privateKey, centerXForPrivateKey, y + privateKeyQrCodeBitmap.getHeight(),
                    textPaint);
            return bmp;
        }

        @Override
        protected void onPostExecute(final Bitmap bitmap) {
            if (bitmap != null) {
                //DEBUG
                //                    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(context);
                //                    android.widget.ImageView view = new android.widget.ImageView(context);
                //                    view.setImageBitmap(bitmap);
                //                    builder.setView(view);
                //                    builder.setPositiveButton(android.R.string.ok, null);
                //                    builder.show();

                PrintHelper printHelper = new PrintHelper(context);
                printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT);
                printHelper.printBitmap(label, bitmap);
            }

        }
    }.execute();
}

From source file:de.gebatzens.sia.SettingsActivity.java

public static CustomTabsIntent createCustomTab(Activity activity, String url) {
    Drawable d = ContextCompat.getDrawable(activity, R.drawable.ic_arrow_back);
    Bitmap bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    d.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    d.draw(canvas);// w w w. j  a  v  a  2  s  .c  om
    CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder()
            .setToolbarColor(SIAApp.SIA_APP.school.getColor()).setSecondaryToolbarColor(Color.RED)
            .setCloseButtonIcon(bitmap).setShowTitle(true).build();
    customTabsIntent.launchUrl(activity, Uri.parse(url));
    return customTabsIntent;
}