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.
Related
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!
In my app I am drawing many things on canvas.
Before I draw a new figure I want to remove all the previous drawings and start afresh.
In other words I want to perform a NEW-operation as we do it in MS-Paint with a fresh canvas, nothing drawn on it.
How can I achieve such funstionality ?
Please help.
The best way to do it is to draw a desired starting color onto your entire canvas.
If you want to to be clear, as origionally.
myCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
Or if you want a white background like MSPaint
myCanvas.drawColor(Color.WHITE); // Will accept any color.
If you want to clear the Canvas, you can do this:
protected void onDraw(Canvas canvas) {
...
canvas.drawBitmap(fundo, 0, 0, null);
...
In this case, I am drawing the back scene again, but you can "clear" the canvas too using
Canvas.drawColor(Color.BLACK)
Please see this post
I'm trying to achieve the same results as per thread:
Make certain area of bitmap transparent on touch.
I'm stick to code presented in this answer: Lumis answer and acording to SteD this solution should work.
Unfortunately is not working for me (and also for another user: Make certain area of bitmap transparent on touch doesn't works, it's draw a black circle), I'm just getting black circle.
I tried many things but not get this solve. Make background transparent as per suggestion from second thread do not make any difference.
After many experiments I found that transparency is working, when I'm set this
android:theme="#android:style/Theme.Translucent"
in my AndroidManifest.xml I can see everything under my application i.e. desktop.
I went through code many times and cant see obvious mistake, only reason I thinking is cause this is Z order, but bitmaps and canvas do not maintenance z orders. Z ordering is done by drawing in certain order (which is correct in this code).
Is this some strange example of optimisation in android code, or I'm missing something in android manifest file?
Finally I found solution that's working:
#Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);
//draw background
canvas.drawBitmap(bgr, 0, 150, null);
//copy the default overlay into temporary overlay and punch a hole in it
c2.drawBitmap(overlayDefault, 0, 0, null); //exclude this line to show all as you draw
c2.drawCircle(X, Y, 80, pTouch);
//draw the overlay over the background
//canvas.drawBitmap(overlay, 0, 0, null);
Paint new_paint = new Paint(/*Paint.ANTI_ALIAS_FLAG*/);
new_paint.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP));
canvas.drawBitmap(overlay, 0, 0, new_paint);
}
But I don't understand why is working. I was investigate xfermodes with this source code: android Xfermodes demo and according to this image: Xfermodes image
It not make any sense, apart that xfermode make sure that overlay bitmap is bean drawn second (Z order) and transparency came to play. But Z order should be maintained by drawing order.
If somebody have better idea how to solve this, please share your knowledge.
Regards.
you can edit onDraw() method:
public void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.drawColor(Color.TRANSPARENT);
canvas.drawBitmap(bgr, 0, 0, null);
c2.drawCircle(X, Y, 10, pTouch);
Paint new_paint = new Paint(/*Paint.ANTI_ALIAS_FLAG*/);
new_paint.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP));
canvas.drawBitmap(overlay, 0, 0, new_paint);
}
I tried and it worked !
Hope this solution helps you
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.
Hi all:
I'm writing a class that inherit from TextView, and override its onDraw() method, but in the method, my invoke of canvas.drawText() doesn't seems to work, the code just like below:
protected void onDraw(Canvas canvas) {
// super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(android.graphics.Color.WHITE);
paint.setTextSize(20);
String text = "hello";
canvas.drawText(text, 0, 0, paint);
}
It isn't drawing anything because the text coordinates are bottom left. Since you're trying to draw on 0,0, it will draw above the screen.
Try changing the last line to:
canvas.drawText(text, 0, 20, paint);
Excellent suggestions all around, great job guys really. Next time though it would be nice if you ask the guy in a comment or something whether or not he's tried the completely obvious before posting it as an answer. Do you really think that the second he got to a point that wasn't working he just came straight to Stack Overflow without experimenting?
I do have an alternate suggestion, that crazily enough is based on the entire question and not just the part that could be answered without much actual knowledge.
I would recommend trying your drawText call on a Canvas that's not in a TextView subclass as that way it won't be overridden by the several hundred lines of code in TextView that manage it's drawable state.