Draw a small bitmap above a larger bitmap - java

I am trying to draw something like this, but it is not displaying bitmap 2. Only bitmap 1 (the larger one) displays.
//bitmap1 and bitmap2 already initialized
Bitmap resultBitmap = Bitmap.createBitmap(cubemap.getWidth(), cubemap.getHeight(), cubemap.getConfig());
Canvas canvas = new Canvas(resultBitmap);
canvas.drawBitmap(bitmap1, new Matrix(), null);
canvas.drawBitmap(bitmap2, (bitmap1.getWidth() - bitmap2.getWidth()) / 3, (bitmap1.getHeight()) , new Paint());
return resultBitmap;

set image in Imageview
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView1.setImageBitmap(drawUsingBitmap());
Using Bitmap
public Bitmap drawUsingBitmap() {
bitmapLarge = BitmapFactory.decodeResource(getResources(), R.drawable.bitmap_large);
bitmapSmall = BitmapFactory.decodeResource(getResources(), R.drawable.bitmap_small);
Bitmap resultBitmap = Bitmap.createBitmap(bitmapLarge.getWidth(), bitmapSmall.getHeight() + bitmapLarge.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(resultBitmap);
// using bitmap
Paint paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawBitmap(bitmapSmall, bitmapSmall.getWidth() / 2, 0, paint);
canvas.drawBitmap(bitmapLarge, 0, bitmapSmall.getHeight() - 10, paint);
return resultBitmap;
}
Using drawRect
public Bitmap draWUsingRect() {
// using rectangle
int widthSmall = 120;
int heightSmall = 80;
int widthLarge = 400;
int heightLarge = 300;
Bitmap resultBitmap = Bitmap.createBitmap(widthLarge + 20, heightSmall + heightLarge + 20, Config.ARGB_8888);
Canvas canvas = new Canvas(resultBitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(5);
canvas.drawRect(new Rect(widthSmall / 2, 0, widthSmall, heightSmall), paint);
canvas.drawRect(new Rect(0, heightSmall, widthLarge, heightSmall + heightLarge), paint);
return resultBitmap;
}
I hope it is helpful to you.

Related

How to overlay two bitmaps (CENTER-BOTTOM) with a fixed size

I'm trying to draw a bitmap on the top of another bitmap like this :
I'm using the following code to create an empty background with 420x420 as size, and draw the star on it :
for (int i = 0; i < 10; i++) {
Bitmap resized;
if (stretchedPosition.contains(i)) {
resizedStar = Bitmap.createScaledBitmap(star, star.getWidth() - 80, star.getHeight() + 80, true);
} else
resizedStar = Bitmap.createScaledBitmap(star, star.getWidth() + 80, star.getHeight() - 80, true);
resized = makeBackground(resized);
//code for generating a GIF from bitmaps
}
public Bitmap makeBackground(Bitmap resized) {
Bitmap emptyBitmap = Bitmap.createBitmap(420, 420, Bitmap.Config.ARGB_8888);
int positionLeft = 0;
int positionTop = 0;
Bitmap newBitmap = Bitmap.createBitmap(emptyBitmap.getWidth(), emptyBitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
canvas.drawBitmap(emptyBitmap, positionLeft, positionTop, null);
canvas.drawColor(Color.GREEN);
int bitmap1Width = resized.getWidth();
int bitmap1Height = resized.getHeight();
int bitmap2Width = emptyBitmap.getWidth();
int bitmap2Height = emptyBitmap.getHeight();
float marginLeft = (float) (bitmap1Width * 0.5 - bitmap2Width * 0.5);
float marginTop = (float) (bitmap1Height * 0.5 - bitmap2Height * 0.5);
canvas.drawBitmap(resized, new Matrix(), null);
canvas.drawBitmap(emptyBitmap, marginLeft, marginTop, null);
return newBitmap;
}
THE ISSUE:
As you can see here, the girl image is not centered and the image gets cut as well.
This may helps you;
Bitmap fileBitmap = BitmapFactory.decodeFile(filePath);
Bitmap blankBitmap = getBlankBitmap(YOUR_WIDTH, YOUR_HEIGHT);
Bitmap mergeBitmap = overlay(blankBitmap, fileBitmap);
you can generate blank bitmap using following method:
public static Bitmap getBlankBitmap(int w, int h) {
Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
Bitmap bmp = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap
Canvas canvas = new Canvas(bmp);
return bmp;
}
and Merge two bitmap like this;
public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
int padding = (bmp1.getWidth() / 2) - (bmp2.getWidth() / 2);
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);
canvas.drawBitmap(bmp2, padding, 0, null);
bmp1.recycle();
bmp2.recycle();
return bmOverlay;
}

Android canvas add stroke and elevation

I am creating a thumbnail from a large image via canvas to use it as marker in google map and how can i add a border (stroke) of 2 dp and also light elevation in my thumbnail
Canvas function
public static Bitmap getCroppedBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.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());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
bitmap.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}

Adding image WaterMark programmatically in Android

I am trying to add an image watermark to another image. I have the following code but I'm facing a problem. I don't know what this 'Resources res' is.
Can anyone help?
public static Bitmap addWatermark(Resources res, Bitmap source)
{
int w, h;
Canvas c;
Paint paint;
Bitmap bmp, watermark;
Matrix matrix;
float scale;
RectF r;
w = source.getWidth();
h = source.getHeight();
bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG |Paint.FILTER_BITMAP_FLAG);
c = new Canvas(bmp);
c.drawBitmap(source, 0, 0, paint);
watermark = BitmapFactory.decodeResource(res, R.drawable.android_mo);
scale = (float) (((float) h * 0.10) / (float) watermark.getHeight());
matrix = new Matrix();
matrix.postScale(scale, scale);
r = new RectF(0, 0, watermark.getWidth(), watermark.getHeight());
matrix.mapRect(r);
matrix.postTranslate(w - r.width(), h - r.height());
c.drawBitmap(watermark, matrix, paint);
watermark.recycle();
return bmp;
}
It's the Resource object you can have through activity.getResources() or fragment.getResources()
Resources res
is the resource object that you need to pass from the calling Activity/Fragment.
suppose, if the method is in Utility class then invoke the method as
//Add watermark to the selected image
Bitmap markedBitmap = Utility.addWatermark(context.getResources(), bitmap);
Main bitmap is bmp1 and bmp2 transparante bitmap :
public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);
canvas.drawBitmap(bmp2, 0, 0, null);
return bmOverlay;
}

Draw bubble programmatically with arrow

I am implementing the following Class from Stackoverflow
https://stackoverflow.com/a/20811323/1936925
but I need a bubble like this
I am trying a lot, but don't know how to do it.
What edits do I need to do in order to get the above drawable.
Try this:
public Bitmap overlay(Bitmap image, String content) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
//My bg bitmap, use yours
Bitmap background = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(MapActivity.context.getResources(), R.drawable.marker_base, options), marker_width, marker_height, true);
//your red bitmap here
Bitmap bmOverlay = Bitmap.createBitmap(background.getWidth(), background.getHeight(), background.getConfig());
Canvas canvas = new Canvas(bmOverlay);
Paint paint = new Paint();
paint.setFilterBitmap(true);
canvas.drawBitmap(background, new Matrix(), paint);
canvas.drawBitmap(image, 3, 3, paint);
if (content != null)
{
//Use your TextSize and Color and paddings
paint = new Paint();
paint.setColor(Color.BLACK);
if (content.length() < 11)
paint.setTextSize(13);
else
paint.setTextSize(10);
//30 / 28, 17 - my paddings
canvas.drawText(content, (content.length() < 11)?30:28, 17, paint);
}
return bmOverlay;
}
Solved
mPointer.rLineTo(80, 0);
mPointer.rLineTo(0, 60);
mPointer.rLineTo(-(80), -60);
mPointer.close();

Android: padding left a bitmap with white color

How to set all white the 10 rows on the left side of a Bitmap?
I'v got a Bitmap that has to be padded on the left side. I thought i can create a new image iterate on the old one getpixel for each position and setpixel on the new one (white or colored) than return the new bitmap...is this wrong?
Any suggestion? thanks a lot!
You can instead create a new Bitmap with the extra padding number of pixels.
Set this as the canvas bitmap and Color the entire image with the required color and then copy your bitmap.
public Bitmap pad(Bitmap Src, int padding_x, int padding_y) {
Bitmap outputimage = Bitmap.createBitmap(Src.getWidth() + padding_x,Src.getHeight() + padding_y, Bitmap.Config.ARGB_8888);
Canvas can = new Canvas(outputimage);
can.drawARGB(FF,FF,FF,FF); //This represents White color
can.drawBitmap(Src, padding_x, padding_y, null);
return outputimage;
}
public Bitmap addPaddingTopForBitmap(Bitmap bitmap, int paddingTop) {
Bitmap outputBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight() + paddingTop, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(outputBitmap);
canvas.drawColor(Color.RED);
canvas.drawBitmap(bitmap, 0, paddingTop, null);
return outputBitmap;
}
public Bitmap addPaddingBottomForBitmap(Bitmap bitmap, int paddingBottom) {
Bitmap outputBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight() + paddingBottom, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(outputBitmap);
canvas.drawColor(Color.RED);
canvas.drawBitmap(bitmap, 0, 0, null);
return outputBitmap;
}
public Bitmap addPaddingRightForBitmap(Bitmap bitmap, int paddingRight) {
Bitmap outputBitmap = Bitmap.createBitmap(bitmap.getWidth() + paddingRight, bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(outputBitmap);
canvas.drawColor(Color.RED);
canvas.drawBitmap(bitmap, 0, 0, null);
return outputBitmap;
}
public Bitmap addPaddingLeftForBitmap(Bitmap bitmap, int paddingLeft) {
Bitmap outputBitmap = Bitmap.createBitmap(bitmap.getWidth() + paddingLeft, bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(outputBitmap);
canvas.drawColor(Color.RED);
canvas.drawBitmap(bitmap, paddingLeft, 0, null);
return outputBitmap;
}
Here's a kotlin extension function with RxJava to get it done. I haven't tested completely but based combined the previous answers to get something
fun Bitmap.pad(top: Float = 0F, bottom: Float = 0F, left: Float = 0F, right: Float = 0F): Single<Bitmap> {
return Single.create<Bitmap> { emitter ->
val output = Bitmap.createBitmap(
(width + left + right).toInt(),
(height + top + bottom).toInt(),
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(output)
canvas.drawBitmap(this, left, top, null)
emitter.onSuccess(output)
}.subscribeOn(Schedulers.computation())
}
I think the coroutine version would simply be
suspend fun Bitmap.pad(top: Float = 0F, bottom: Float = 0F, left: Float = 0F, right: Float = 0F): Bitmap {
val output = Bitmap.createBitmap(
(width + left + right).toInt(),
(height + top + bottom).toInt(),
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(output)
canvas.drawBitmap(this, left, top, null)
return output
}
I just did this to give padding from all side. Hope it will help someone.
Combination of https://stackoverflow.com/a/44060669/6480433 & https://stackoverflow.com/a/6957333/6480433 these answer.
Bitmap outputimage = Bitmap.createBitmap(Src.getWidth() + padding_x,Src.getHeight() + padding_y, Bitmap.Config.ARGB_8888);
Canvas can = new Canvas(outputimage);
can.drawBitmap(Src, padding_x, padding_y, null);
Bitmap output = Bitmap.createBitmap(outputimage.getWidth()+padding_x, outputimage.getHeight() + padding_y, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
canvas.drawBitmap(outputimage, 0, 0, null);
return output;
You might want to look here:
http://download.oracle.com/javase/1.4.2/docs/api/java/awt/image/BufferedImage.html
methods you might want to use are: getHeight() then you know how many pixels to set and iterate over 10 columns
and setRGB (int x, int y, int RGB) to set the pixel

Categories

Resources