I have two arrows drawn on my canvas using the canvas.DrawPath(). I'm using canvas.rotate() to rotate, but it is obviously rotating both arrows by the same amount.
Id like to be able to rotate one arrow one way, and rotate the other arrow in a different direction.
Is this possible?
When you use canvas.rotate() you are altering the transformation matrix associated to the canvas, so all what you paint after that will be affected by the current state of the matrix, you have to do the following:
canvas.save(); //Save current canvas matrix state
canvas.rotate(angle);
canvas.DrawPath(); //Draw first arrow
canvas.restore(); //Restore canvas matrix to saved state
canvas.DrawPath(); //Draw second arrow without the rotation
Related
I am using canvas on mapview for custom marker I have draw multiple components in Canvas but I want to rotate only one component rotate not all the components rotate same time. canvas have his own builtin function canvas.rotate(degree); but when we sub components like canvas.drawRect, canvas.drawText or canvas.drawBitmap then how we use rotate drawRect.
sorry for bad english...
thanks in advance.
You can save Canas state and then restore its state to remove all modifications:
canvas.save();
canvas.rotate(degree);
canvas.drawText(...);
canvas.restore();
In this answer you can read more about point of managing cavas state.
I have a circle object, created with OpenGL vertices.
When the circle is being touched, I want a scaling animation to be activated. The circle radius will grow bigger until it reaches a certain radius, and then the circle will transform back to its original radius.
What is the best way to implement this animation?
You have a few possibilities. The simplest would be to just create a variable which would hold a scaling factor for your circle and multiply all vertices by it before rendering. Then using some timers and adjusting the scaling factor in time, you could create your animation.
I should add that if you want to scale your circle by its center then you have to first translate the circle vertices so that circle's center will be at (0, 0) point, then scale by the factor and translate the circle back to its initial position.
If the circle animation you're trying to implement is some kind of your GUI element then another way which comes to my mind is to:
create an ImageView in xml or dynamically in code
draw the vertices on it
create an XML android animation and apply it whenever you want
Here's a draft of the code for drawing a circle on the ImageView:
Bitmap tempBitmap = Bitmap.createBitmap(yourCircleWidth, yourCircleHeight, Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(tempBitmap);
//Draw the circle into the canvas
tempCanvas.drawColor(Color.WHITE);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
tempCanvas.drawCircle(circleCenterX, circleCenterY, yourCircleRadius, paint);
//Attach the canvas to the ImageView
yourImageView.setImageDrawable(new BitmapDrawable(yourImageView.getResources(), tempBitmap));
For XML animations, here's a good reference:
http://www.androidhive.info/2013/06/android-working-with-xml-animations/
I'd like to draw an oval, but I'd like to be able to rotate it. I know that I can use canvas.drawOval(...) and canvas.rotate(...). However, I want to just rotate my oval and not the whole canvas; that is, I want to rotate my oval before draw it to the canvas.
I have successfully rotated a rect by manipulating the coordinates before drawing it, but that approach isn't working for me with the oval.
There's canvas.save() and canvas.restore() to serve that purpose - i.e.
canvas.save();
canvas.rotate(90);
canvas.drawOval(....);
canvas.restore();
....//do other drawing
I would like to display a transperent PNG of a "light" line shape according to user's sliding path.
I'd like to make similar effect like Fruit Ninja has, and leave a track after user slides his finger.
I already have the x,y points of his finger - using onTouch method, and checking the x,y on MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP but how do i draw an image that will be tilted and displayed at those positions? all i know is to add padding/margin to an image, not how to place it using x,y, or how to rotate it..
For positioning and rotating use a canvas (getSurfaceHolder().lockCanvas()) and draw inside it using drawBitmap.
public void drawBitmap (Bitmap bitmap, Matrix mtx, Rect dst, Paint paint)
the matrix can include the rotation:
Matrix mtx = new Matrix();
mtx.postRotate(90);
You might want to se the code from API demos, FingerPaint.
In my Android project, I have a matrix that I use to draw a bitmap on a canvas. During my drawFrame method, I translate the matrix in the Y direction so it looks like the bitmap is moving up. It does move up, but it is also drawn in the previous position leaving multiples of itself behind. Here is my drawFrame code:
Canvas canvas = null;
canvas = holder.lockCanvas();
Matrix moveMatrix = new Matrix ();
moveMatrix.postTranslate(0, 10);
matrix.preConcat(moveMatrix);
canvas.drawBitmap(bitmap, matrix, null);
holder.unlockCanvasAndPost(canvas);
"matrix" is a global Matrix member.
Does anyone know why this leaves a "streaking" trail of bitmaps across the screen?
Thanks!
I forgot to mention that each previous bitmap position seems to shift up and down in the y direction every time the bitmap moves. So it ends up with a string of bitmaps that look like they are jittering up and down. This would mean that every bitmap would have to be redrawn every frame otherwise they would all be standing still.
You need to clear the Canvas at the previous position of the bitmap :)