Cutting a Portion of Canvas and Saving it as an Image - java

I'm drawing a Rectangle on a Canvas created from a Bitmap using this method
//void android.graphics.Canvas.drawRect(float left, float top, float right, float bottom, Paint paint)
mycanvas.drawRect(arg1,..arg4,mypaint);
I want to show this rectangular portion of the image in an ImageView.
How can i do it? Do i need to create a new Bitmap?

Related

Cut off a part of canvas

I have a canvas with graph, i get it from server. Sometimes the graph is not in the center. Now i want to cut off the rest part of canvas. I have the graph max and min x,y points. Ho to cut off the rest part of canvas? I can't find a solution.
First create a tempBitmap of size full width and Height like this
Bitmap tempImage = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Bitmap finalBitmap = Bitmap.createBitmap(tempImage, (int) minX, (int) minY, width, height);
here finalBitmap is that which you want.
You can draw it in canvas and also save as bitmap.

how to crop image shape in Hexagon,Octagon or rectangle shape

I want to crop image shape in Hexagon,Octagon or rectangle shape.the image is on imageView and the Coordinates of vertices are given by user on mouse click.
how can i crop image of imageView according to given coordinate.

How to rotate Image and Rectangle

how to rotate a rectangle in canvas?
for example.
Rectangle Box = new Rectangle(100,100,100,100);
and i want to rotate this by (for example)38 degres.
Someone know how i can do it?

Rotated rect changes location

I am trying to rotate a rect and i have to rotate the canvas to do so but the rect changes its location for example, if the rect is in the middle of the screen after the rotation it will go to the side and i cannot figure out how to get it back to the middle.
Here is the code i used:
canvas.save();
canvas.rotate(45);
canvas.drawRect(width/2,height/2, width/2+200, height/2+200, paint);
canvas.restore();
Is there a way to rotate the rect without rotating the canvas so its location will stay the same?
Your rectangle's center differs from the pivot point of the canvas. You can try using the other rotate method to specify the x/y coordinates of the pivot point. As in:
int offset = 200;
int left = width/2;
int top = height/2;
int right = left + offset;
int bottom = top + offset;
canvas.save();
canvas.rotate(45, left + offset/2, top + offset/2);
canvas.drawRect(left, top, right, bottom, paint);
canvas.restore();
Edit:
Just tested the code above and verified that it works (given a few tweaks that I just added, above).
'canvas.rotate' rotates the canvas around the origin of canvas. Origin is top left corner of canvas at initial state.
If you want to rotate a rectangle around the center of the rectangle, move origin of canvas to the center of the rectancle before using 'canvas.rotate'.
Try following code:
canvas.save();
canvas.drawRect(width/2,height/2, width/2+200, height/2+200, paint); // drow rectangle
canvas.translate(width/2+100, height/2+100); // move origin of canvas to the center of the rectangle
canvas.rotate(45); // canvas is roteted around the origine that is the center of the rectangle also. As a result, rectangle is roteted around its center.
canvas.translate(-(width/2+100), -(height/2+100)); // restore origin of canvas at original position
canvas.restore();

Android rotate bitmap around center without resizing

I'm struggling to draw a rotating bitmap around its center and do the rotating without resizing the bitmap. I'm drawing all my sprites to the screen via a game thread, so I'm looking for a solution that incorporates the original bitmap and not the canvas.
Thanks in advance.
This is my code so far, it turns a bitmap around its center, yet resizes it.
i = i + 2;
transform.postRotate(i, Assets.scoresScreen_LevelStar.getWidth()/2, Assets.scoresScreen_LevelStar.getHeight()/2);
Bitmap resizedBitmap = Bitmap.createBitmap(Assets.scoresScreen_LevelStar, 0, 0, Assets.scoresScreen_LevelStar.getWidth(), Assets.scoresScreen_LevelStar.getHeight(), transform, true);
game.getGraphics().getCanvasGameScreen().drawBitmap(resizedBitmap, null, this.levelStar.getHolderPolygons().get(0), null);
Update:
I've noticed this isn't as easy as it sounds. My rotating code is not the problem. The bitmap rotates, yet the dst rect will also have to increase/decrease depending on the angle of rotation, or else the bimap will appear smaller, since it's drawn into a fixed dst rect.
So I'm guessing I'll have to develop some method that will return a dst rect.
So the methods needed to rotate a bitmap without appeared resizing:
public static Bitmap rotateBitmap(Bitmap bitmap, int rotation) // I've got this method working
and
public static Rect rotateRect(Rect currentDst, int rotation) // Don't got this
I understand this will require some math (trig), anyone up for the challenge? :P
You should draw the bitmap using the Matrix class. Below is a very basic idea assuming that you want to rotate an image inside of a "Ship" class. You update the current position Matrix inside the update method. In the onDraw() you draw the bitmap using the newly updated position Matrix. This will draw the rotated bitmap without resizing it.
public class Ship extends View {
private float x, y;
private int rotation;
private Matrix position;
private Bitmap bitmap;
...
#Override
public void onDraw(Canvas canvas) {
// Draw the Bitmap using the current position
canvas.drawBitmap(bitmap, position, null);
}
public void update() {
// Generate a new matrix based off of the current rotation and x and y coordinates.
Matrix m = new Matrix();
m.postRotate(rotation, bitmap.getWidth()/2, bitmap.getHeight()/2);
m.postTranslate(x, y);
// Set the current position to the updated rotation
position.set(m);
rotation += 2;
}
....
}
Hope that helps!
Also keep in mind that generating a new Bitmap object inside you game loop will be resource intensive.
This worked for me!
I created a method that returns a matrix. The matrix can be used in the following drawing method:
public void drawBitmap (Bitmap bitmap, Matrix matrix, Paint paint)
Here you go! (The parameter shape can be replaced with ease, if you would like that, just leave a comment):
public static Matrix rotateMatrix(Bitmap bitmap, Shape shape, int rotation) {
float scaleWidth = ((float) shape.getWidth()) / bitmap.getWidth();
float scaleHeight = ((float) shape.getHeight()) / bitmap.getHeight();
Matrix rotateMatrix = new Matrix();
rotateMatrix.postScale(scaleWidth, scaleHeight);
rotateMatrix.postRotate(rotation, shape.getWidth()/2, shape.getHeight()/2);
rotateMatrix.postTranslate(shape.getX(), shape.getY());
return rotateMatrix;
}
NB: If you want an animated rotation, the rotation parameter will have to be updated with new values every frame eg. 1 then 2 then 3 ...

Categories