Adding one canvas to another - android

How can I use two canvas in my application so that I am able to rotate two bitmap images with different rotation speed

Why don't you just use the same canvas?
c.save();
c.rotate(rotationAngleForBitmap1);
c.drawBitmap(bitmap, matrix, paint);
c.restore();
c.rotate(rotationAngleForBitmap2);
c.drawBitmap(bitmap2, matrix, paint)
c.restore();

Related

Rotate and resize rectangle drawn in canvas in Android

In my Android application I have drawn rectangle in canvas, how can I rotate rectangle and resize the rectangle on touch events ?
canvas.drawRect(300,300,500,500,paint);
This code is used to draw the rectangle.
You can do using below
canvas.save(); // saving current canvas
resize
rect.set(x,y,newWidth, newHeight);
rotate
canvas.rotate(45);
Apply changes
canvas.drawRect(rect, paint);
canvas.restore();

What is the relationship between canvas and matrix in Android?

I read this canvas overview :
The Canvas class holds the "draw" calls. To draw something, you need 4
basic components: A Bitmap to hold the pixels, a Canvas to host the
draw calls (writing into the bitmap), a drawing primitive (e.g. Rect,
Path, text, Bitmap), and a paint (to describe the colors and styles
for the drawing).
Can anyone explain the canvas more clearly?
And I am confused about the relationship between the canvas and matrix. Does the canvas takes the transformations from the matrix?
And I want to know if below function affect on the canvas?
canvas.drawBitmap(bitmap, matrix, paint);
In other words, is the canvas matrix different from the bitmap matrix?
I asked this, because when I'm using canvas.drawBitmap and then using canvas.concat() and then drawing any object, this object takes same transformations on canvas, so I think the canvas and bitmap have the same matrix!!
They are different. When using the canvas to draw a bitmap providing a matrix, internally, the provided matrix are concatenated to the current canvas matrix.
In other words, calling canvas.drawBitmap(rectBitmap, matrix, paint); has the same effect of:
canvas.save();
canvas.concat(matrix);
canvas.drawBitmap(rectBitmap, 0, 0, paint);
canvas.restore();
This explain why your object is taking the same transformations because you are calling canvas.concat(matrix); and after that drawing the object.

android how to rotate canvas rect

i create a rectangle in a specific size, and now i want rotate it to 45 degree, i used canvas.rotate, matrix, but not working. how is the proper way to rotate canvas in android? and i'm curios about Path.Direction.CW, is it used for rotation? but i don't see any rotation function in Path()
paint.setAntiAlias(true);
paint.setStrokeWidth(2);
paint.setColor(Color.BLUE);
paint.setAlpha(75);
Path path = new Path();
path.addRect(166, 748, 314, 890, Path.Direction.CW);
canvas.rotate(45);
canvas.drawPath(path, paint);
To draw a rotated rectangle you need to rotate the canvas before drawing, (then rotate it back to right-side up if you're drawing anything else). Canvas.rotate() just alters the canvas's transformation matrix, which transforms shapes drawn after the call.
canvas.save();
canvas.rotate(45);
canvas.drawRect(166, 748, 314, 890, paint);
canvas.restore();
Path.Direction has nothing to do with rotation transforms. From the docs:
Specifies how closed shapes (e.g. rects, ovals) are oriented when they
are added to a path.
If you want to draw something from (x,y) point, you have to rotate the canvas around (x,y) point. For doing this you should use
canvas.rotate(45,x,y);
so,
canvas.save();
canvas.rotate(45,x,y);
//all drawing from (x,y) point
canvas.restore();
Proper way should be something like this:
Path path = new Path();
path.addRect(166, 748, 314, 890, Path.Direction.CW);
canvas.save(); // first save the state of the canvas
canvas.rotate(45); // rotate it
canvas.drawPath(path, paint); // draw on it
canvas.restore(); // restore previous state (rotate it back)

How to draw a transparent circle?

I'm trying to draw a transparent circle, but it just doesn't work.
when I'm drawing a bitmap it works, but a circle doesn't become transparent.
Here's my code in short:
Paint paint = new Paint();
paint.setAlpha(125);
canvas.drawBitmap(bitmap, sourceRect, destRect, paint); // this works fine
canvas.drawCircle(x, y, radius, paint); // the circle is drawn but not transparent
I found it.
paint.setAlpha must come after paint.setColor

what is the relation between Canvas and Bitmap?

What is the relation between Canvas and Bitmap?
Bitmap drawingBitmap = Bitmap.createBitmap(bmp1.getWidth(),
bmp1.getHeight(), bmp1.getConfig());
canvas = new Canvas(drawingBitmap);
paint = new Paint();
canvas.drawBitmap(bmp1, 0, 0, paint);
paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.SCREEN));
canvas.drawBitmap(bmp2, 0, 0, paint);
compositeImageView.setImageBitmap(drawingBitmap);
I don't understand this code.Why the drawingBitmap is the composition of bmp1 and bmp2?
Basically, the Canvas is backed by a Bitmap, so when you draw anything using the canvas, the canvas will draw into the Bitmap it was created with. So, when you draw those two bitmaps using the canvas, it's going to composite the bitmaps together and the result will be stored in drawingBitmap, as it's backing the canvas.
Anh's analogy is correct-ish, though probably confusing (and over-simplifying, which I'm also doing above) – as I mentioned in a comment, you can think of the Canvas as the pen, the Paint as a configuration of that pen (e.g., replaceable ink or something - whatever you can fit into the idea of a configurable pen), and the Bitmap as the paper you draw onto. The analogy becomes confusing if you focus too much on the accepted meaning of the words.
Let's think canvas as a pen, and drawingBitmap as a paper. You use your pen to draw something on your paper, and you get what you draw.
Technically, you can construct Canvas object from Bitmap to draw others bitmaps on it.
Canvas is the place or medium where perfroms/executes the operation of drawing, and Bitmap is responsible for storing the pixel of the picture you draw.

Categories

Resources