why my invoke of Canvas.drawText() just doesn't work - android

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.

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.

Android - Change a portion of a canvas' alpha on a SurfaceView using drawRect - is it possible?

Is it possible alter a portion of the canvas' alpha to change its opacity? What I'm kind of looking for is something like this:
public void onDraw(Canvas canvas){
canvas.drawARGB(255,0,0,0);
.
.
//Other images ( Canvas.drawBitmap() ) goes in here
.
.
Paint paint = new Paint();
paint.setAlpha(100);
canvas.drawRect(20,50,40,100, paint);
}
I am trying to create a game where when the player comes across a tall obstacle, part of his body (For example, just his feet) can be seen through the obstacle with an alpha of 100, depending on how covered his body is with the obstacle. I've tried doing an example (Like the one above), but it doesn't work. I was hoping for a way to do something similar. It doesn't have to be drawRect, though. Thanks!
P.S. It's my first time posting on stackoverflow, so please bear with me. Thanks. :)

How to start fresh on canvas ?

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

Make certain area of bitmap transparent on touch --> again

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

Android Canvas.drawLine not smooth, not consistent

I am trying to use Canvas.drawLine method to draw a polygon
Here's the code that I am using
Canvas canvas = new Canvas(cache);
Paint paint = new Paint();
paint.setStrokeWidth(16);
paint.setColor(this.currentDrawing.getColor());
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
systemCanvas.drawBitmap(cache, 0, 0, paint);
paint.setStrokeCap(Cap.ROOUND);
canvas.drawLine(from.getLeft(), from.getTop(), to.getLeft(), to.getTop(), paint);
And this is the output that I am getting:
Notice the way the lines render, they break on the round shapes and don't join smoothly. I understand why is it happening but I don't know how to make it smooth and consistent.
Any help is appreciated.
You may also want to do this
mPaint.setAntiAlias(true);
Please check this answer out because it is similar to your question:
Android How to draw a smooth line following your finger
Please let me know if this helps!
You need to activate hardware acceleration. If you don't activate this, you can not use method of antialias, cap, join and etc.

Categories

Resources