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.
Related
I upload a png-image with an alpha channel to the drawable folder. From this picture I get a Bitmap, which has transparency in certain places.
int resID = context.getResources().getIdentifier(bitmapName, "drawable", context.getPackageName());
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resID);
I need to get a new Bitmap in which all colors are replaced by one color (for example, red), and the alpha channel should remain unchanged. There was a colored Bitmap with transparent areas, but I need a Bitmap with the same transparent areas, but the same color. Tell me how to achieve this?
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.
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);
I am trying to add a simple 2d Image (.png format) to my 2d Game. My game consists of a Game View Class that extends surface view, a GameState Class, that Controls the logic of the game, and draws the canvas and a Main Activity. My GameState class has this method:
// the draw method
public void draw(Canvas canvas, Paint paint) {
// Clear the screen
canvas.drawRGB(20, 20, 20);
// set the colour
paint.setARGB(200, 0, 200, 0);
// // draw the bats
// canvas.drawRect(new Rect(_bottomBatX, _bottomBatY, _bottomBatX
// + _batLength, _bottomBatY + _batHeight), paint); // bottom bat
}
It is responsible for drawing all the objects on the game. I can easily draw a rectangle. But i cannot figure out how to draw an Image into my game. I plan on having the image move dynamically (like a sprite).
How can i draw a sprite inside of the above method?
I cannot do it this way:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
since GameState does not extend View
Thank you
A simple solution would be.
first you need to store your image in the asset folder
then you need to call the AssetManager to get the assets in your assets folder
Bitmap yourImage;
AssetManager assetManager = context.getAssets();
get the byte stream of your image
InputStream inputStream;
inputStream = assetManager.open("yourImage.png");
// path is relative to the assets folder
let the BitmapFactory do its work in decoding it and storing it to the yourImage variable of type Bitmap
yourImage = BitmapFactory.decodeStream(inputStream);
inputStream.close();
then you need to call a method in the canvas object
canvas.drawBitmap(bitmap, x, y, paint);
The parameters of the method are:
bitmap The bitmap to be drawn
x The position of the left side of the bitmap being drawn
y The position of the top side of the bitmap being drawn
paint The paint used to draw the bitmap (usualy this is null)
Example:
canvas.drawBitmap(yourImage, 100, 100, null);
I have a drawable object that is a vector. Since you can't crop (or clip because of ICS forced hardware acceleration) a drawable it make's sprite animation useless.
I'm trying to convert the drawable to a bitmap with a specific size (percentage of the lowest screen dimension for best results).
The version needs to be super memory efficient of course.
What I need help with is creating the bitmap with the specific size, here's what I got so far:
public Bitmap svgTObitmap(String name, int percentage, int screen_width, int screen_height)
{
_resID = _context.getResources().getIdentifier( name , "raw" , _context.getPackageName());
SVG svg = SVGParser.getSVGFromResource(_context.getResources(), _resID);
Drawable drawable = svg.createPictureDrawable();
Bitmap bmp = ((BitmapDrawable)drawable).getBitmap();
return bmp;
}
Being from a vector, clearly your Drawable is not in fact a BitmapDrawable. You need to instead draw the Drawable on a Bitmap. See this answer I gave to another question:
Bitmap bmp = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
Obviously in your case you would specify the size you want explicitly instead of trying to get the intrinsic height/width of the drawable.