I want to display text (relatively large) and determine which character the user touches. I'd also like to embellish the text by drawing a circle (for example) over certain characters. How do I know which characters are at what location? Enforcing monospaced font would be fine if helpful programmatically.
Would it also be able to make this one line of text scroll left/right (using touch->slide) and still have touch work to determine the correct character?
A very broad question, and you've got a lot of work ahead, but here's the outline of one possible solution:
Create a custom view. ImageView is often a good base class to
inherit.
Override the onDraw() method of the view.
Use canvas.drawText() to place your text on screen.
Use paint.getTextBounds to meausure your text. See the discussion
linked below for a thorough understanding. Since you drew the text
on screen, and you measured it, you know exactly where each
character is.
Override the onTouch() method in your custom view and track the
events you need. There are many QAs here in SO on how to do this.
For scrolling the text, use canvas.translate() using the movement
you calculate in the onTouch(). Since you know how much you
scrolled (translated), you know how far your characters have moved
and can offset your touch detection to detect character touches.
Finally, since you now control all of the drawing and, your
character positions are abstract, only having meaning when compared
to a touch position, you can embellish them in any way choose.
Android Paint: .measureText() vs .getTextBounds()
Phew!
Good luck.
Related
I'm going to be determing some arbitrary rectangle shapes in an image (borders around OCR-recognized text, in fact,) and am trying to figure out the best way to listen for clicks on them.
I was originally going to draw invisible Rects around them, but then realized that you can't set an onClickListener on a Rect. So I'm trying to determine the most processor- and memory-efficient means to accomplish this.
It seems to me like I can either:
A) Listen for the click on the Canvas, determine the x- and y-coordinates of the click, then run down a list of every single rectangle to see which, if any, it corresponds to.
B) Create invisible/transparent View objects of some kind corresponding to the rectangles I want monitored, and set listeners on them. If this is the case, would it be better to use empty LinearLayout type objects, or something like a TextView? Or something even simpler?
It seems to be like 'A' would be more resource-intensive, but then, I'm not sure what the overhead for creating potentially dozens of invisible views, each with their own onClickListeners, would be.
Any suggestions as to the best way to approach this would be more than welcome. Happy to include any code requested, though it seems like this is an abstract enough question to not need it.
In the project I am working on I decided to use SurfaceView instead of a custom double buffered medium. It provides everything I need and it's already double buffered.
The problem is that it wont let me specify multiple dirty rectangles to redraw.
SurfaceView.lockCanvas(Rect) only allows single rectangle and without parameter it's pretty expensive to redraw whole thing. Another solution to call lockCanvas(Rect) for each Rect causes eye-bleeding blinking in the screen, obviously.
Do you have any solution giving the opportunity staying inside Android API field, if not do you have any external alternatives I can use?
If you know the dirty areas before you need to call lockCanvas (sounds like you might), you could calculate a "super rectangle" that locks an area that contains all of your rectangle. For example if your rectangles are (using l,r,t,b coordinates) [0,10,0,20] and [15,30,10,35], your super rectangle would be [0,30,0,35].
I am looking to create a custom EditText, where each character entered should lie within its own cell (see image).
My best guess is that I need to create my own .png's for the various states of the EditText, which provide the rectangular outline, then extend EditText's onDraw method to draw the vertical lines that separate adjacent characters.
I've never made a custom view before, and I know little about manual drawing in Android, so some guidance is needed.
Am I on the right path here?
How can I determine how tall and at what location to draw the vertical lines?
What is the best way to eat an oreo?
This is gonna be a very difficult task. Just look at TextView.onDraw() (which you are thinking of override). If I were you, I'd immediately change my mind :)
Instead, I'd use a LinearLayout to hold an array of customized EditText, but I don't know what kind of interaction you are looking for
Finally, to measure text you use Paint.getTextBounds(). Where to draw vertical separators depends on your design. If you have a fixed number of fixed length cells, you know where, otherwise you need to measure text
I am working on a project that involves painting on images.
To delete the unwanted lines or curves i have to draw a border and X button to delete it.
I have a relative layout where i have the freehand drawing canvas. on edit mode i should make them as u see in the pic, where i have to create button on a varying x,y positions.
i am confused how to achieve this.
Thanks in advance.
Jana.
I suggest doing this manually rather than using the Button widget. Override the onTouchEvent on the view holding your painting and use MotionEvent.getX and MotionEvent.getY in combination with MotionEvent.getAction to determine behaviour when the user touches the 'button'. Skipping widget creation will improve performance and open up doors to other types of functionality.
You could use the deprecated AbsoluteLayout container for this or keep the RelativeLayout and use layoutMargins to set the location of the buttons. The former is the route you should take despite the container being deprecated as the later breaks the layout paradigm by misusing margins...
You should keep in mind that there are a variety of devices with different screen sizes and setting explicit, pixel based locations is going to be awkward.
I have a graphical Android project and my primary trick for providing user interface functionality is to use drawrect and drawtext to draw a rectangle with a label on the screen. Then, I capture touch events and check to see if they occur in the rectangle -- when they do, voila, I have a button.
It may not be the most elegant method, but is seems to be working like a charm. However, not all of my labels look good all on one line, and I'd like to write them on two. I suppose I could write two separate lines and manually work out arranging the text to be nicely spaced and centered, but I'd like to avoid that if possible.
In Android, is there a simple way to split a text label and write it out in a single step??
Thanks,
R.
I don't think there is a easy way to achieve this, you will have to measure the text and draw it with an offset that is ButtonWidth - TextWidth / 2.
In an Android application, you can create a Layout and populate it with a variety of widgets. One of these potential widgets is the button and if you use it, it WILL display your button label on more than one line if the label's length requires that...
However, my application is a SurfaceRunner, and this view doesn't support buttons, as far as I can tell. I do use a button-ish functionality by drawing various rectangles and checking for screen touches inside their boundaries. This works fine for my purposes, but does not, in any way, automate any of the display tasks (like the button widget does). In order to display a long label on two lines, it's necessary to do that explicitly in the code. Oh well.
Fortunately, it's only one additional line:
canvas.drawRoundRect (Button, ROUNDOVER_SIZE-1, ROUNDOVER_SIZE-1, paint);
canvas.drawText (Text1, Button.centerX() - paint.measureText (Text1)/2, Button.centerY() - 10, paint);
canvas.drawText (Text2, Button.centerX() - paint.measureText (Text2)/2, Button.centerY() + 20, paint);
Obviously, it would have been cooler if I had used something a tad more automatic than "10" and "20", but this does get the job done.