onDraw how to continue drawing over the previous graphics - android

I am working on a Paint app. I want the drawings like each line or oval on their own layers, on top of each other. The onDraw method refreshes the whole canvas each time it is called and erase all the previous drawings. I want the previous drawings to be there and draw more over them. Here is my code of the onDraw method.
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(path, myPaint);
if(drawOval){
drawOval = false;
canvas.drawCircle(100, 100, 100, myPaint);
}
}
It refreshes the canvas as soon as I start drawing something. I was restoring the previous state this way.
canvas.drawBitmap(getDrawingCache(), 0, 0, myPaint);
I want each drawing on its own layer above all the others previously drawn.
Good ways to retire from this task. Thanks!

Related

Android Canvas drawing clearing

I've a bit of an issue.
#Override
public void draw(Canvas canvas) {
super.draw(canvas);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}
My canvas scope is local to just that draw method, but is being wrote to mBitmap. I know how to clear canvas inside there but it'd be pretty pointless. My question is how can I reset canvas from just touch event, or any sort of event in run time? Let me know if you need some more information
Thanks
edit: i was going to use a private variable to the class like doesScreenNeedClearing and have a listener set that, then in the ondraw have it question it but i don't think things like that should be going in onDraw, but i don't know maybe that's how people do it?
See the canvas.drawColor() method. You can easily just use Color.BLACK or Color.WHITE or every color you want.

Draw image view with animation

I've create custom ImageView (extends from ImageView).
on the onDraw method I have a circle and I want to update the circle very often when onSensorChanged called.
what happens is that canvas.drawCircle() called with different values each time (I call invalidate when sensor change)
and I can see the circle moving but the movement is not smooth enough. I think because it's running on the main thread.
I don't have accesses to surfaceHolder from the image.
is there a way to make it run more smoothly?
protected void onDraw(Canvas canvas) {
canvas.drawRoundRect(mainRectangle, 20f, 20f, rectanglePaint);
canvas.drawRoundRect(validationRectangle, 20f, 20f, rectanglePaint);
canvas.drawCircle(circleXLocation, circleYLocation, circleRadios, circlePaint);
}
the circle is moving with the change of circleXLocation and circleXLocation

Drawing custom shape onto Canvas in Android

I've been stumped by this one for a little bit, so I figured I'd ask here to see if anyone has any pointers.
In a nutshell, I have an app where I want multiple complex shapes to be drawn onto a canvas, which will then be drawn onto the screen (I'll be implementing panning around the large canvas as suggested in this question's answer: Android: Drawing and rotating on a canvas)
How exactly do I go about creating a custom shape and drawing it at arbitrary locations/rotations on a Canvas in Android?
Below is an example of a simple custom shape, as I've tried to implement it so far (typically they'd be created at run-time)
public class Symbol {
public Bitmap b = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
public Symbol() {
Canvas canvas = new Canvas(b);;
Paint paint = new Paint();
paint.setColor(Color.GRAY);
paint.setStyle(Paint.Style.STROKE);
paint.setTextSize(25);
paint.setStrokeWidth(4);
paint.setDither(true);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setAntiAlias(true);
String str="TestStr";
canvas.drawText(str, 250,250, paint);
}
}
Wow, I derped hardcore on this one.
Android: canvas.drawBitmap performance issue
Adding this in my main class made everything happy, at least as far as this simple case went.:
Symbol s = new Symbol();
canvas.save();
canvas.rotate(5);
canvas.drawBitmap(s.b, 0,0, null);
canvas.restore();

Add and manage a layer over a canvas using SurfaceView

I want to say that maybe (probably) the word 'layer' is not the correct one, but I believe it gives the correct idea of what I want to do.
I am using a SurfaceView which implements SurfaceHolder.Callback.
I am working on a canvas. I am drawing a quite complex set of points.
I have the canvas translated, rotated and scaled (translateX and translateY are variable that changes when the user interacts with the canvas):
canvas.translate(0, 0);
canvas.rotate(-90);
canvas.translate(translateX, translateY);
canvas.scale(scaleX, scaleY);
My next step is to add text and images on top of this canvas.
Both the text and the images should not be scaled nor rotated, only translated (they should follow the canvas).
For this reason I was thinking about having some sort of 'layer' or something similar transparent which only follows the canvas movements and does not change on zoom in or zoom out or on rotation [think them as some sort of UI which stay over the whole canvas].
EDIT:
Here is a snippet:
#Override
public void onDraw(Canvas canvas) {
try{
pt.setColor(Color.BLACK);
canvas.drawColor(Color.WHITE);
canvas.drawText("HELLOOOOOOOOOOOOOOOO", 100, 10, pt);
pt.setAntiAlias(true);
canvas.save();
canvas.rotate(-90, 0, 0);
canvas.drawText("HELLOOOOOOOOOOOOOOOO", getHeight(), 10, pt);
canvas.restore();
}
catch(Exception e)
{}
}
I want the two 'HELLOOOOOO' to appear close to each other. I can't figure out how to.
Why not use save() and restore() so that after all your canvas is "normal" again. Than you can draw on it easily?
Save and restore working like a SceneGraph, if you have heard of it.
I managed to fix this. I simply rotated the canvas using:
rotate(+90, X, Y);
and then rotated back
rotate(-90, X, Y);
Doing this, the (X,Y) coordinates remain the same.

Android - canvas drawing doubts

I am looking at one of the sample applications from Google, which deals with touch drawing using canvas:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/TouchPaint.html
I have a few doubts:
I am not able to understand what's the role of Canvas versus the role
of the bitmap.
In the drawPoint function, I am not able to
understand this code snippet:
mCanvas.drawCircle(x, y, radius, mPaint);
mRect.set((int) (x - radius - 2), (int) (y - radius - 2),
(int) (x + radius + 2), (int) (y + radius + 2));
invalidate(mRect);
If the circle is already drawn into the canvas above, then what happens in the onDraw function where the following code is given:
canvas.drawBitmap(mBitmap, 0, 0, null);
Canvas vs Bitmap
A Bitmap is what the name suggests: A normal image as a bitmap. The Canvas class is an editor for bitmaps. You use it to change the bitmap data, it holds all drawing methods. This principle behaves similar to the shared preferences (if you already worked with them), you have a SharedPreferences class that holds the preferences, and an Editor class to change things.
Drawing the circles
This code does something similar to double buffering. drawPoint() basically draws a circle into the mBitmap object¹. But this bitmap object is not yet visible. It exists in the memory. When onDraw() is called, it has a Canvas argument that represents the drawing surface of the view. All that drawBitmap() does here is use the prepared bitmap from the memory and draw it inside the views graphical representation to make it visible.
¹ The used canvas mCanvas is tied to mBitmap inside onSizeChanged()
if you go to the developper refference:
drawBitmap(Bitmap bitmap, float left, float top, Paint paint)
Draw the
specified bitmap, with its top/left corner at (x,y), using the
specified paint, transformed by the current matrix.
Then if you see that mBitmap doesn't exist in the class , thats cause that var comes from the extend from another activity .
Canvas also has a setBitmap(Bitmap bitmap) function . Then the solution is that that paint in canvas if you have set into it a bitmap object.
From the Android SDK:
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).
I'm assuming you're referring to this snippet:
#Override protected void onDraw(Canvas canvas) {
if (mBitmap != null) {
canvas.drawBitmap(mBitmap, 0, 0, null);
}
}
Well it looks like an override of an inherited onDraw method which by default probably 'does nothing', hence the override to actually give it some behaviour, in this case given a non-null Bitmap instance, make the canvas draw it.

Categories

Resources