canvas drawBitmap() gives different output - java

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

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

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.

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

Positioning of bitmap

When I rotate a bitmap it rotates but it keeps moving all over the screen and rotating simultaneously. I want it to rotate in the center ? My code is given below.
try {
c = holder.lockCanvas();
// clear the canvas
c.drawColor(Color.BLACK);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);
if (c != null) {
int width=c.getWidth();
int height=c.getHeight();
c.drawBitmap(backgroundImage, 0, 0, null);
Matrix transform = new Matrix();
Matrix transform1=new Matrix();
transform.setRotate(degree, width/2,height/2);
c.drawBitmap(image1, transform, null);
//canvas.rotate(-90);
degree+=5;
c.restore();
}
}
Try this code..
c = holder.lockCanvas();
c.drawColor(Color.BLACK);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);
if (c != null)
{
int width=c.getWidth();
int height=c.getHeight();
c.drawBitmap(backgroundImage, 0, 0, null);
Matrix matrix = new Matrix();
float px = width/2;
float py = height/2;
matrix.postTranslate(-image1.getWidth()/2, -image1.getHeight()/2);
matrix.postRotate(degree);
matrix.postTranslate(px, py);
c.drawBitmap(image1, matrix, paint);
degree+=5;
c.restore();
}

Categories

Resources