Example usage for android.graphics Canvas Canvas

List of usage examples for android.graphics Canvas Canvas

Introduction

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

Prototype

@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
public Canvas(long nativeCanvas) 

Source Link

Usage

From source file:Main.java

public static Bitmap drawTextToBitmap(Context context, int resId, String text) {

    Resources resources = context.getResources();
    float scale = resources.getDisplayMetrics().density;
    Bitmap bitmap = BitmapFactory.decodeResource(resources, resId);

    Bitmap.Config bitmapConfig = bitmap.getConfig();
    if (bitmapConfig == null)
        bitmapConfig = Bitmap.Config.ARGB_8888;
    bitmap = bitmap.copy(bitmapConfig, true);

    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(context.getResources().getColor(android.R.color.white));
    paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    paint.setTextSize((int) (12 * scale));

    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);
    int x = (bitmap.getWidth() - bounds.width()) / 4;
    int y = (bitmap.getHeight() + bounds.height()) / 5;

    canvas.drawText(text, x * scale, y * scale, paint);

    return bitmap;
}

From source file:Main.java

/**
 * Makes a texture from any bitmap. /* w  ww  .  j av a 2 s  .c  o m*/
 * (Texture should have size that is power of 2) 
 * @param bmp bitmap
 * @return BitmapDrawable that has size that is power of 2. Bitmap is not stretched, free space 
 * is filled with default color.
 */
public static Bitmap makeTexture(Resources res, Bitmap bmp) {
    if (bmp == null) {
        throw new IllegalArgumentException("Bitmap can't be null");
    }

    int height = (int) roundPower2(bmp.getHeight());
    int width = (int) roundPower2(bmp.getWidth());

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

    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(bmp, 0, 0, null);

    return result;
}

From source file:Main.java

public static Bitmap createScaledBitmap(Bitmap unscaledBitmap, int dstWidth, int dstHeight,
        ScaleType scalingLogic) {/*  ww w .  j  a v  a  2s  .  c o  m*/
    Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), dstWidth, dstHeight,
            scalingLogic);
    Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), dstWidth, dstHeight,
            scalingLogic);
    Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.width(), dstRect.height(), Config.ARGB_8888);
    Canvas canvas = new Canvas(scaledBitmap);
    canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
    return scaledBitmap;
}

From source file:Main.java

private static Bitmap drawableToBitMap(Drawable drawable) {
    if (drawable == null) {
        return null;
    }/*from w ww  .j a v a2s. co m*/
    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = ((BitmapDrawable) drawable);
        return bitmapDrawable.getBitmap();
    } else {
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
    }
}

From source file:Main.java

public static Bitmap drawImageBorder(Bitmap srcBitmap, Bitmap borderBitmap) {
    if (srcBitmap == null)
        throw new NullPointerException("srcBitmap should not null");
    if (borderBitmap == null)
        return srcBitmap;
    Bitmap temp = srcBitmap.copy(Config.ARGB_8888, true);
    /*Bitmap border = borderBitmap;
    if (srcBitmap.getWidth() != borderBitmap.getWidth() || 
      srcBitmap.getHeight() != borderBitmap.getHeight()) {
       border = zoomBitmap(borderBitmap, srcBitmap.getWidth(), srcBitmap.getHeight());
    }*///from  w w  w  .j a  v a 2 s. c o  m
    Canvas canvas = new Canvas(temp);
    Matrix m = new Matrix();
    m.postScale((float) temp.getWidth() / borderBitmap.getWidth(),
            (float) temp.getHeight() / borderBitmap.getHeight());
    //canvas.drawBitmap(border, 0, 0, null);
    canvas.drawBitmap(borderBitmap, m, null);
    return temp;
}

From source file:Main.java

public static Bitmap createRoundedBitmap(Context context, Bitmap bitmap, int radiusDP) {
    Bitmap bmp;/*from w  w w . ja va  2s  .c o m*/

    bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    BitmapShader shader = new BitmapShader(bitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);

    float radius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, radiusDP,
            context.getResources().getDisplayMetrics());
    Canvas canvas = new Canvas(bmp);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(shader);

    RectF rect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
    canvas.drawRoundRect(rect, radius, radius, paint);

    return bmp;
}

From source file:Main.java

public static Bitmap pictureDrawable2Bitmap(Picture picture) {
    Bitmap bitmap = Bitmap.createBitmap(picture.getWidth(), picture.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    picture.draw(canvas);/*from   ww  w.  j  a  v  a2  s  . c  o m*/
    return bitmap;
}

From source file:Main.java

public static Bitmap drawable2Bitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    } else if (drawable instanceof NinePatchDrawable) {
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);//from ww  w.  j  a va2  s  .c  om
        return bitmap;
    } else {
        return null;
    }
}

From source file:Main.java

/**
 * Method to combine two images side by side.
 *
 * @param leftBmp  The left Bitmap./*from   w w  w  .  jav a 2s. c  o  m*/
 * @param rightBmp The right Bitmap.
 * @return A Bitmap with left and right bitmap are glued side by side.
 */
public static Bitmap combineTwoImagesSideBySide(Bitmap leftBmp, Bitmap rightBmp) {
    int width;
    int height = leftBmp.getHeight();

    if (leftBmp.getWidth() > rightBmp.getWidth()) {
        width = leftBmp.getWidth() + rightBmp.getWidth();
    } else {
        width = rightBmp.getWidth() + rightBmp.getWidth();
    }

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

    Canvas comboImage = new Canvas(cs);
    comboImage.drawBitmap(leftBmp, 0f, 0f, null);
    comboImage.drawBitmap(rightBmp, leftBmp.getWidth(), 0f, null);

    return cs;
}

From source file:Main.java

private static Bitmap createBitmapForFotoMix(Bitmap first, Bitmap second, int direction) {
    if (first == null) {
        return null;
    }/*from  w  ww. ja v  a 2  s  . c  om*/
    if (second == null) {
        return first;
    }
    int fw = first.getWidth();
    int fh = first.getHeight();
    int sw = second.getWidth();
    int sh = second.getHeight();
    Bitmap newBitmap = null;
    if (direction == LEFT) {
        newBitmap = Bitmap.createBitmap(fw + sw, fh > sh ? fh : sh, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(newBitmap);
        canvas.drawBitmap(first, sw, 0, null);
        canvas.drawBitmap(second, 0, 0, null);
    } else if (direction == RIGHT) {
        newBitmap = Bitmap.createBitmap(fw + sw, fh > sh ? fh : sh, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(newBitmap);
        canvas.drawBitmap(first, 0, 0, null);
        canvas.drawBitmap(second, fw, 0, null);
    } else if (direction == TOP) {
        newBitmap = Bitmap.createBitmap(sw > fw ? sw : fw, fh + sh, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(newBitmap);
        canvas.drawBitmap(first, 0, sh, null);
        canvas.drawBitmap(second, 0, 0, null);
    } else if (direction == BOTTOM) {
        newBitmap = Bitmap.createBitmap(sw > fw ? sw : fw, fh + sh, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(newBitmap);
        canvas.drawBitmap(first, 0, 0, null);
        canvas.drawBitmap(second, 0, fh, null);
    }
    return newBitmap;
}