Android Canvas draw a multiple frame Image - android

I have an image with different frame to be displayed, like the following:
As you can see that image has three frames, the full heart, half heart and empty space.
Now i need to only one frame of the three. I'm just wondering if there is a method to do that using a single gif in android sdk.
For example in several language there is a method like:
canvas.drawImage(xpos,ypos,xwidth,xheight,gifx,gify,gifwidth,gifheight)
where gifx,gify,gifwidth,gifheight are the coordinates and size of the selected frame.

Ok i answer myself, i just found a solution.
In order to draw a multiframe image the following method can be used:
public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint)
Where:
Bitmap bitamp is the Bitmap resource obtained with BitmapFactory.decodeResource (or via your preferred method).
Rect src is the frame to be shown (it can be null)
Rect dst rhe rectangle that the bitmap will be scaled/translated to fit into (it can be null)
Paint paint used to draw the bitmap (it can be null)
Another way (that i didn't tested) could be using BitmapRegionDecoder first of all a new instance of the object must be created using BitmapRegionDecoder.newInstance(...)
and then the selected region of bitmap to be shown could be obtained with the method:
public Bitmap decodeRegion (Rect rect, BitmapFactory.Options options)
Where rect is the selected region to be shown. For more info on BitmapRegionDecoder:
http://developer.android.com/reference/android/graphics/BitmapRegionDecoder.html

Related

Why does drawing a shape to a bitmap reduce quality?

Why does drawing a shape to a bitmap cause the shape to be blurry versus drawing the shape straight to the canvas?
Bitmap is a rasterized computer image, consisting of points (pixels).
The Canvas class holds the "draw" calls.
A shape is an abstract definition of shape (vector-like).
drawing the shape straight to the canvas
That's not accurate, maybe you mean straight to the screen of device, which is Bitmap too.
To use Canvas for drawing, you need the canvas to host the calls, some bitmap as target, and some drawing primitive (shape is one), and Paint (contains information how to paint the drawing primitive).
Once you draw some shape into the target bitmap, it will be aliased to the pixels of target bitmap. Ie. an circle will turn into some approximation created by rectangular pixels of target bitmap.
What you probably see in your particular case is, that your Bitmap has lower resolution than screen, and when you draw that low-res bitmap to the target screen bitmap, it is upscaled by some filter which makes the upscaled picture a bit blurry to avoid big rectangular pixelation (or it may be also other way, downscaling hi-res bitmap, which contains too sharp/thin contours, which will be aliased second time when downscaled, and even with anti-aliasing filtering it will get blurred a bit). Or maybe you use some paint with settings causing blur (unlikely, can't think of creating one by accident, you would knew).
If you will use for both targets Bitmap of identical density (resolution of single pixel), and the same paint method, then the result will be same, and also drawing the shape from bitmap to bitmap as long as you will use whole pixel coordinates/size, and no filtering, again the result will be same as drawing directly to screen bitmap.
So start first by checking the size of your bitmap vs screen bitmap, and then check paint settings and additional canvas arguments to draw the bitmap, whether you don't upscale/downscale it with some kind of filtering.

How to make a background image fill the canvas?

Hello everyone I am new to android and I just have a quick question. I am making an animation using Bitmaps and drawing them to the canvas using the onDraw(). I am trying to draw a background to the canvas that I made in photoshop. I already uploaded it as a drawable and then decoded it to a Bitmap then added it to the onDraw method. It draws, but I doesn't fill the entire canvas like I want it to. Any suggestions and or fixes? Thank you so much, I looked around and saw something about converting the bitmap to an image view but didn't really understand all to well.
Use drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) where the rect for source is the entire bitmap (from 0,0 to width, height) and dest is the entire canvas. This will stretch the bitmap to fill the canvas. Be warned this can cause the aspect ratio to be off and/or issues with fuzziness or blockiness.

Android: Canvas drawBitmap?

I have a 200x200px bitmap. I want to draw the top left 50x50px corner of my bitmap, on my canvas at coordinates 100,100 with a width and height of 50px, by using:
drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)
This is what I've tried:
drawBitmap(myBitmap, new Rect(0,0,50,50), new Rect(100,100,150,150) , null);
What am I doing wrong?
From developer.android.com:
Parameters
bitmap The bitmap to be drawn
src May be null. The subset of the bitmap to be drawn
dst The rectangle that the bitmap will be scaled/translated to fit into
paint May be null. The paint used to draw the bitmap
What is missing in my code?
Thanks!
You need to change your rectangles. This is because, as described in the documentation, the first rectangle is the subset of the bitmap you want to draw, the second is the scaling/translating so basically the size of the destination draw (50x50)
So it should look like this:
drawBitmap(myBitmap, new Rect(100,100,150,150), new Rect(0,0,50,50) , null);

How to draw Bitmap from ARGB_8888 Bitmap

I am drawing paths on canvas and creating Transparent bitmap while saving and creating cropping bitmap from transparent bitmap.
See Images :
In this I mage I am drawing path on canvas and I am creating transparent bitmap and according to startX,lowestY and highestX,highestY
Bitmap cropBitmap =Bitmap.createBitmap(sourceBitmap,startX,lowestY,highestX,highestY);
When I am cropping Bitmap I want Only "Test" drawing crop bitmap.But it's giving empty bitmap. Like this
Inside red box I want cropped bitmap from transparent bitmap whatever I draw on canvas.
You cannot simply create a transparent bitmap, and assume that whatever is within the bounds of the bitmap has become a part of it meaning that the pixels are identical. There are two problems with your assumption, number 1, a transparent bitmap won't help because transparency serves as a see through bitmap which only will have alpha value, number 2, the data that is drawn with the path, has no correlation to the data of your bitmap, you initially created a bitmap with no color, except that it's completely transparent.
Here's the general code for the correct way how to achieve such a task:
class myDrawingView extends View() {
// all your class members you initialize here
#Override
public void onDraw(Canvas canvas) {
// get your width and height using startx, starty, highestx, highesty, lowesty
Bitmap buffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
canvas.setBitmap(buffer);
canvas.drawPath(//draw your path here);
invalidate();
}
The Bitmap called buffer, now is the frame buffer for all your drawing so you can draw a path and that will be rendered on the buffer you defined. And you see that no transparency was used we just simply created a bitmap with no pixels assigned to it at first, once you draw to it via the canvas, your bitmaps pixels will be whatever you drew.

Android - Draw bitmap as one color

I have several bitmaps (game sprites) which I'd like to draw into another bitmap, however each non-transparent pixel of the source bitmap should be drawn using a single color, ignoring the original pixel color of the source. Basically, I'm trying to use the sprite as a "stamp" of a single color to be drawn into the destination bitmap.
I believe I should be using canvas.drawBitmap (Bitmap bitmap, Matrix matrix, Paint paint), however I'm not exactly sure how I should initialize the paint object. Is this approach correct?
You don't need to perform as many steps as Romain Guy suggests, just initialize your paint with the desired color, and use Paint.setColorFilter() with PorterDuff.Mode.SRC_ATOP
myPaint.setColorFilter(new PorterDuffColorFilter(myColor, PorterDuff.Mode.SRC_ATOP));
If your destination bitmap is transparent, draw all your sprites inside that bitmap normally (you can use a null Paint.) Then, draw a filled rectangle that covers the entire bitmap, using the Porter-Duff xfermode called SrcIn (Source In.)

Categories

Resources