How do i split a bitmap image in android? - android

I'm just a beginner with android. I want to divide a bitmap image into chunks and then display the image in the same way but divided. is this possible?

Instead of trying to break the image up into chunks you should just use:
canvas.drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)
This will draw parts of the image. So to give the effect of breaking it up you are really just using the src and dest rectangles to draw specified parts of the images. Where src is the rectangle in local image coordinates of the part you want to draw and dest is the rectangle used to position it on the screen.

You can use the following method to get an area of an source bitmap:
Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height)

Related

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 draw a multiple frame Image

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

How do I draw a flipped subset of a bitmap on Android?

I'm using drawBitmap(Bitmap image, Rect src, Rect dst, Paint paint); to draw a portion of an image. Is there a way to do this and also flip the image?
Yes, image is a matrix, so assuem its 4x4 pixel image. so extract block wise pixel. Once you get a portion of image i think then you can apply any matrix transformation on image like rotateX().

Editing the portion of canvas

I want to cut the selected portion of bitmap and i have to paste it in the same bitmap using .. please help me out..
You can do it by generating an second Bitmap with the portion the user selected. Them to draw, just use the Canvas of the first. It's not so hard.
A tip is: The Canvas.drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)
This draws a portion of the Bitmap limited by the Rects args.

Android Bitmap Drawing

Can you draw a Bitmap to a Canvas by telling it to draw Bitmap's X,Y at Canvas' X1,Y1? Rather than drawing Bitmap's 0,0 at Canvas X1, Y1?
That's truly possible the Canvas.drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) is here to help. You can specify src region to draw. Look at Canvas docs for more.
Also you may use Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height) to extract that part of original image you want to render. Then just render extracted Bitmap as ususal. More info about the method is available on Google's docs.

Categories

Resources