on a canvas is it possible to draw text but make half of it not show up (as if it is being drawn partially off of the canvas, but actually not being off the canvas). i have an indicator on a "graph" with some text and it follows the point but I do not want the text to be drawn out of the graph portion (i am drawing other stuff outside that area).
I also have a background on the layout behind the canvas so I cannot just paint with a bitmap because that would cause some aspect ratio/sizing issues (wouldn't look good).
I have tried looking for answers all over Google, Stack overflow, and by experimentation with no avail. On Google I found many interesting things about drawing text on an android canvas but nothing that I was looking fore, I am pretty stumped, and I am starting to think that what I want is not even possible, and i may need to draw the text custom with points or figure out an alternative that looks just as good.
It's absolutely possible. Probably the fastest is to use clipRect to limit drawing to your graph portion. See this answer: Using clipRect - explanation
The reference on the various forms of clipRect is here: http://developer.android.com/reference/android/graphics/Canvas.html#clipRect(android.graphics.Rect, android.graphics.Region.Op)
If I recall, the whole operation will look something like:
yourCanves.save()
yourCanvas.clipRect(...)
yourCanvas.drawText(...)
yourCanvas.restore()
The restore() call serves to undo the clipRect so you can draw outside it for later drawing.
The simplest solution that popped in my mind, would be to cut using substring the text that you want to dispaly.
Example:
if(MyString >5){
canvas.drawText("VeryLongTe...");
}
Related
I'm currently attempting to make a simple 2D CAD-type viewer app for Android. Basically the input file contains a bunch of primitives (rectangles, lines, circles, octagons, that type of thing), and the goal is to draw these to the screen at whatever coordinates/sizes they offer.
My initial instinct is to use a Canvas to draw these to, using a quadtree or some similar structure to track which items will show up on the screen at any given time.
Does anyone have any recommendations here for a better way to implement this (my graphics programming experience is minimal, and hence I'm having problems even finding a starting point to Google from)?
Thanks in advance,
-Ross
That's a very broad question so my answer will only point at classes that you should be looking at.
Extend a surfaceView to be your cadView, that way you'll be all the calculation outside the main thread.
you'll still have to draw on the canvas.
from the canvas you can getWidth() and getHeight() and use those values for base comparison on your positions.
Canvas have some primitives drawings types like arc, circle, point.
Further you can use Path to draw full figures, line, filling, quadratic, etc.
for backgrounds you can create color drawables and draw it on the canvas.
and that's pretty much it.
I've got some problem. I have an image drawn using canvas, and I would like to animate it. I would like to do something like TranslateAnimation, but TranslateAnimation works only with views, and my image is not a view. What can I do?
Personally I haven't worked with Android canvas, but I am fairly certain that what you need to do is this:
Draw your shape on the canvas
Wait for a couple of miliseconds
Clear the canvas
Redraw shape to next immediate position
and repeat this until the final position is reached.
From my little knowledge the canvas is more similar to a piece of paper than to a flash animation. You need to draw, clear, recalculate, redraw. I highly recommend reading some more on the topic. Firstly, I would start with the android canvas reference: http://developer.android.com/reference/android/graphics/Canvas.html
and then try some tutorials like canvas frame by frame tutorial for android : http://developingthedream.blogspot.ro/2011/01/android-canvas-frame-by-frame-animation.html
And also I highly recommend looking up things on google first before posting a question on stack. There is a good chance, especially if you are a beginner, to find existing tutorials with everything you need and more. Good luck learning.
easy now.
What I want eventually:
I want to have 2 bitmaps overlayed on a view. Same bitmaps with the one above have higher brightness than the below one.
Now when the user strokes(with touch event (like paint brush)) on the upper bitmap, I want those parts of the upper bitmap to go invisible.
For those who are familiar with adobe photoshop perhaps this will make more sense:
I want to draw a mask on an image being display so that only the unmasked parts remain visible. But the mask can be drawn from a brush with variable hardness/size.
How do I achieve this functionality? Direct me in in the line where I should research or give sample code.
Also, is it possible to draw strokes on an imageview with a brush which has variable hardness? I know that I can drawPath and drawArc on a canvas, but I do not know how to achieve the different brush strokes/styles.
Please pardon me if I haven't phrased my question right, or wasn't able to find similar duplicates.
You can use FrameLayout to overlay one image over other in Android and for Custom Masking search FingerPaint on google.
I think the best way is to do your own off-screen compositing, then render the composited image using an ImageView or perhaps a subclass with custom interaction. See this sample code for an example of how to do such compositing using the Porter-Duff transfer modes.
The task is following:
To draw parts of some bitmap only behind a path (user draws path using his finger). It must look like the user draws this bitmap by his finger. It is similar to task: erasing some picture by finger, but the approach should be different.
I know it is possible with OpenGL, but at first i am looking for more light option with standard android canvas and view drawing.
I have seen the example FingerPaint in APIDemos (please, do not refer me to it).
I am interested in somebody's real experience of implementing this task, or any other info, links or code, which can help.
Thanks.
You say to not refer you to FingerPaint, but have you considered that this sample may indeed hold the answer?
You could adapt it to two views on top of each other... your hidden image underneath and the fingerpaint view acting as a mask on top. You "paint" with the colour 0x00000000, i.e. total transparency, thereby revealing the image in the view underneath.
I use the following code to draw a bitmap during onDraw of a custom View
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);
p.setShadowLayer(3,3,3,0xff000000);
Bitmap bmp = BitmapFactory.decodeResource(some drawable jpg);
canvas.drawBitmap(bmp,null,new Rect(blah blah),p);
and the shadow looks funny. What do I mean funny? I mean that I get a different shadow then if I was calling canvas.drawRect(). It looks sorta like the android shadow code is treating the bitmap as an alpha channel or something.
have tried adding p.setColor(0xff000000); and p.setStyle(Paint.Style.FILL); but not difference.
I guess I could drawRect with shadow on, then drawBitmap with shadow off, but that seems silly as it would be rendering pixels twice.
Basically the shadow layer doesn't work for anything except text. It's real dumb. Check the Android hardware acceleration supported operations chart.
This question was asked back in 2010 when hardware accelerated view trees didn't exist.. based on things I've read, even then the shadow layer only worked for simple shapes (if at all), and others got weird results using it on anything but text. You might be out of luck.
Finally, there are many ways you can fake a shadow layer. You can wrap a view in another view and draw underneath it. You can write a view that draws the shadow yourself as a radial gradient, etc. But you probably just want to make a 9patch that looks like a shadow and use that.
2018 Update
It looks like most of the operations are supported as of api 28! I haven't had a chance to play around with shadow layers recently, but things are looking much more optimistic now.
Maybe http://developer.android.com/reference/android/graphics/Paint.html#isAntiAlias()
can help you.
paint.setAntiAlias(true);
Maybe you should have different Paint objects for different purposes like text, bitmap, etc. So one's setting will not affect others.