Draw bubble programmatically with arrow - java

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

Related

Android: Cropping an Image from the Bottom Up

I have an image, and I am trying to overlay a cropped version of the image over the original. Something like this:
Original
Overlay Image
Image after the overlay image is cropped to roughly 50%
Right now, my code below reverses what I want, and returns an image like this:
public void setOverlay(ImageView image, Bitmap originalBitmap, double percentageCompleted) {
int percentHeight;
int height = originalBitmap.getHeight();
Bitmap cropped;
if(percentageCompleted == 0){
cropped = Bitmap.createBitmap(overlay, 0, 0, overlay.getWidth() , 0 );
} else {
percentHeight = (int) Math.floor(height * (percentageCompleted));
Log.d("HEIGHT", Double.toString(height));
Log.d("PERCENT Completed", Double.toString(percentageCompleted));
Log.d("PERCENT HEIGHT", Integer.toString(percentHeight));
cropped = Bitmap.createBitmap(overlay, 0, 0, overlay.getWidth() , height-percentHeight);
}
originalBitmap = overlay(originalBitmap, cropped);
//set imageview to new bitmap
image.setImageBitmap(originalBitmap );
}
public Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmp3 = bmp1.copy(Bitmap.Config.ARGB_8888,true);//mutable copy
Canvas canvas = new Canvas(bmp3 );
canvas.drawBitmap(bmp2, new Matrix(), null);
return bmp3;
}
I know I could reverse the image files, but I want to start with the original image and have the overlay draw upwards, rather than starting with the overlay, and drawing the original downwards. Any help is appreciated!
You can use this method:
Canvas.drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)
Here is my overlay method that may meet your need:
public static Bitmap overlay(Bitmap base, Bitmap overlay, Float percentage) {
Bitmap resultBitmap = Bitmap.createBitmap(base.getWidth(), base.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(resultBitmap);
Paint paint = new Paint();
// base bitmap
canvas.drawBitmap(base, 0F, 0F, paint);
// overlay bitmap
int yOffset = (int) (percentage * base.getHeight());
Rect rect = new Rect(0, yOffset, overlay.getWidth(), overlay.getHeight());
canvas.drawBitmap(overlay, rect, rect, paint);
return resultBitmap;
}

canvas drawBitmap() gives different output

below image and code see, if's give different output in different device.
bitmaptemplate= BitmapFactory.decodeResource(getResources(),R.drawable.fb_template);
android.graphics.Bitmap.Config bitmapConfig = bitmaptemplate.getConfig();
// set default bitmap config if none
if(bitmapConfig == null) {
bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
}
bitmaptemplate = bitmaptemplate.copy(bitmapConfig, true);
Canvas canvas = new Canvas(bitmaptemplate);
Bitmap bitmaplogo=BitmapFactory.decodeResource(getResources(),R.drawable.temp);
android.graphics.Bitmap.Config bitmapConfi = bitmaplogo.getConfig();
if(bitmapConfi == null) {
bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
}
bitmaplogo = bitmaplogo.copy(bitmapConfig, true);
Canvas canlogo=new Canvas(bitmaplogo);
canlogo.drawColor(Color.BLACK);
Rect dstRectForRender=new Rect(10,250,700,700);
canvas.drawBitmap (bitmaplogo , null, dstRectForRender, null );
Paint paint = new Paint();
paint.setColor(Color.BLACK);
int spSize = 45;
float scaledSizeInPixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
spSize, getResources().getDisplayMetrics());
paint.setTextSize(scaledSizeInPixels);
paint.setFakeBoldText(true);
paint.setColor(Color.BLACK);
canvas.drawText("PRATIK N SANYAJA", 20, 900+100, paint);

Change color of one LinearLayout to two colors by using canvas

I'm trying to change background color of one LinearLayout to two colors for some reasons like the following pic:
I'm writing this code but the problem this LinearLayout take third different color for the two parts like this pic:
What is the problem in the logic of my code I don't know or I can not do something like that in Android?
Code:
c = (LinearLayout) findViewById(R.id.cell);
c.setBackgroundColor(getColor(container.get(0)));
c.setBackgroundColor(getColor(container.get(1)));
int width = c.getWidth(), height = c.getHeight();
if (width == 0 || height == 0) {
width = 100;
height = 100;
}
Bitmap bitmap;
try {
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
} catch (Exception e) {
e.printStackTrace();
return;
}
Canvas canvas = new Canvas(bitmap);
Path path = new Path();
path.moveTo(0, height);
path.lineTo(0, 0);
path.lineTo(width, 0);
path.lineTo(0, height);
path.close();
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setColor(getColor(container.get(0)));
canvas.drawPath(path, paint);
path = new Path();
path.moveTo(width, 0);
path.lineTo(width, height);
path.lineTo(0, height);
path.lineTo(width, 0);
path.close();
paint = new Paint();
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setColor(getColor(container.get(1)));
canvas.drawPath(path, paint);
I solved my problem, the problem was in the way of parsing color in setColor method I replaced this one:
paint.setColor(getColor(container.get(1)));
by:
paint.setColor(ResourcesCompat.getColor(getResources(),getColor(container.get(1)), null));
and everything is ok now.

Draw a small bitmap above a larger bitmap

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.

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