How to Draw a Pattern on Bitmap using canvas - java

I just want to know is there anyway to draw a pattern over a bitmap , something like this. For example, I want to draw a pattern of this over my bitmap.

You can definitely set a bitmap.And creating a canvas over it.Once canvas gets created over the bitmap, you can definitely draw custom patterns over it.
You can see the complete guide how to draw text here:
https://www.skoumal.net/en/android-how-draw-text-bitmap/
Long story short:
Copy your bitmap to make it mutable and create Canvas based on it.

Option 1: Transparent Foreground
If you follow the process described here to merge the two images, you can draw one image onto another. Simply change the opacity of the top-most image as described here. Consider the following example:
Bitmap bitmap = Bitmap.createBitmap(/* width */, /* height */, Config.ARGB_8888);
Paint alpha = new Paint();
alpha.setAlpha(/* alpha */);
Canvas canvas = new Canvas(bitmap);
Bitmap background = BitmapFactory.decodeResource(
getResources(), R.drawable.background);
Bitmap foreground = BitmapFactory.decodeResource(
getResources(), R.drawable.pattern);
canvas.drawBitmap(background, /* xPos */, /* yPos */, null);
canvas.drawBitmap(foreground, /* xPos */, /* yPos */, alpha);
Option 2: Background Masked with Foreground
Alternatively, if you are trying to mask the image, you can consider implementing the solution found here.

Related

Drawing drawable in canvas doesn't work

I want to draw a drawable on canvas with this code but it doesn't work and i don't know why
getResources().getDrawable(R.drawable.allergist).draw(canvas);
I set my custom views heigh and width to match parent but whole screen is white and there is no drawable on screen
You need to load your image as bitmap:
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.allergist);
Then make the bitmap mutable and create a canvas over it:
Canvas canvas = new Canvas(bitmap.copy(Bitmap.Config.ARGB_8888, true));
After that you can draw it on canvas.
EDIT 1
Set bounds To Drawable.
Drawable d = getResources().getDrawable(R.drawable.allergist);
d.setBounds(left, top, right, bottom);
d.draw(canvas);
We had a similar problem, and we solved it by calling drawable.invalidateSelf(). After that, it rendered successfully onto our Canvas.

How do i display a custom background image from my drawables instead of drawing color

I have the following code which I got from a youtube tutorial for drawing canvas:
(https://www.youtube.com/watch?v=6NBt36fO0iw):
#Override
protected void onDraw(Canvas canvasporch) {
canvasporch.drawColor(Color.BLACK);
for (int i = temps.size() - 1; i >= 0; i--){
temps.get(i).onDraw(canvasporch);
for (Sprite sprite : sprites){
sprite.onDraw(canvasporch);
This works just fine. The only problem is instead of having the color Black as background, I want to display an image from my drawable folder as the background for my sprites. I've tried using drawBitmap() but I cannot seem to make it work. Thanks.
1) Create a member variable to store the Bitmap:
Bitmap spriteBackground;
2) In onCreate() (or some other initialization function) load the bitmap from the drawable:
spriteBackground = BitmapFactory.decodeResource(getResources(), R.drawable.sprite_background);
3) Draw the bitmap using drawBitmap() inside onDraw():
canvasporch.drawBitmap(spriteBackground, 0, 0, null);
If you want to scale the bitmap to cover the canvas then either scale it at load time using createScaledBitmap (uses more memory) or apply a matrix transform to the canvas (possibly slower).
Another way is to pass the canvas bounds to drawBitmap(), like this:
canvasporch.drawBitmap(spriteBackground, null, canvasporch.getClipBounds(), null);

Add text with grey background to pictures like Snapchat does? Android/Java

Bitmap newBm = ...
Canvas canvas = new Canvas(newBm);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE);
paint.setTextSize((int) (44 * scale));
Rect bounds = new Rect();
paint.getTextBounds(gText, 0, gText.length(), bounds);
canvas.drawText(gText, x, y, paint);
I drew text on the Bitmap like so. How could I get a grey background that is the same height as the text but covers the whole screen??
You could use a Rect. Before drawing the text draw the Rect to the screen:
int screenWidth = getApplicationContext().getResources().getDisplayMetrics().widthPixels;
Rect greyBack = new Rect(0,top,screenWidth,bottom);
Paint paint = new Paint();
paint.setARGB(128, 100, 100, 100); //added alpha because Snapchat has translucent //grey background
canvas.drawRect(greyBack, paint);
top and bottom need to be coordinates above and below the text. You could use y's value and take away a bit for top and add a bit for bottom. How much you add/subtract is up to you and changes the height of the greyBack background.
The best way to see and learn how these sort of things are done with well written code is to look at the android source code itself. For example here is the onDraw method for a TextView it includes additional stuff you won't probably need like compoundPadding, but you can follow it through and get the basic concept of how it's done.

Trying to do a spotlight effect in android canvas

I have a canvas and a simple bitmap for background image, fills the whole screen. I created a rect painted black and set it's alpha to 250 in order to make a "dark" effect on the background image. My aim to make a simple circle object that reveals the place it's hovering above. I tried thinking in many ways how to excecute it and failed.
I think the best way is to create a simple circle that manages to decrease the darkness alpha on the position it hovers above, but I have no idea how to do it.
The relevant part of my code:
private ColorFilter filter = new LightingColorFilter(Color.BLACK, 1);
private Paint darkPaint = new Paint(Color.BLACK), paint = new Paint(), paint2 = new Paint();//The style of the text and dark.
public DarkRoomView(Context context) {
super(context);
myChild = this;
darkPaint.setColorFilter(filter);
darkPaint.setAlpha(250);
paint2.setAlpha(10);
paint.setAlpha(50);
}
private void loadGFX() {//Loads all of this view GFX file.
backgroundImage = BitmapFactory.decodeResource(getResources(), R.drawable.darkroomscreen);
lightImage = BitmapFactory.decodeResource(getResources(), R.drawable.light);
}
private void drawGFX(Canvas canvas) {
canvas.drawBitmap(backgroundImage, 0, 0, paint2);//The backgeound image.
canvas.drawRect(0, 0, WIDTH, HEIGHT, darkPaint);//The darkness.
canvas.drawBitmap(lightImage, 50, 50, paint);//A spotlight.
}
Any ideas how I should get it done?
Thanks!
For the spotlight, you could draw a circle of the original image over the darkness. You'd simply need to find the correct rectangle of the original image (based on where your finger is), and then draw a circle of that particular rectangle over the darkness. Trying to look "through" the darkness won't really get you anywhere; you need to place something over it.
By the time you draw the "spotlight", you've already darkened the image with the rectangle. It would be difficult to recover information lost during that draw.
A more flexible approach would be to draw a dark rectangle with a spotlight in a separate image (that is, compose the "darkness" and spotlight alpha and color mask image first), and then draw that mask image on top of the background as a separate step. This would also let you easily do things like e.g. give the spotlight fuzzy borders.

Dynamically changing the foreground color of an Image in Android Java (retaining the alpha transparency)

I have an image of red ball with transparent background. I want to change the color of ball to many other colors programatically without affecting the background i.e. background shall remain transparent. How this can be achieved in Android?
You can use the Bitmap class to modify your image in this way, such as using the setPixel() method. You just need to make sure that the color still has its alpha set to transparent.
Refer to the Color documentation here for defining colors with their RGB and Alpha channels (you want to keep the alpha values of each pixel, and only change the RGB values). Also refer to the Bitmap documentation here
Try:
//Bitmap bmp
//int color
int[] pixels = new int[bmp.getWidth() * bmp.getHeight()];
bmp.getPixels(pixels, 0, bmp.getWidth(), 0, 0,
bmp.getWidth(), bmp.getHeight());
for (int i = 0; i < pixels.length; i++) {
pixels[i] &= color;
}
Bitmap newBmp = Bitmap.createBitmap(bmp.getWidth(),
bmp.getHeight(), Config.ARGB_8888);
newBmp.setPixels(pixels, 0, newBmp.getWidth(), 0, 0, newBmp.getWidth(), newBmp.getHeight());
You can also achieve it by using Frame Animation. Use different pictures of same ball image with different ball colors, and use frame animation to run the images as frames.If so, the colors of ball looks to be changing.

Categories