Divide String over 2 custom Rect shapes in onDraw(); - android

I'm having issues dividing my String object over 2 Rect shapes.
Firstly i have no idea how to set canvas.drawtext to a specified region.
Like you would do if you use textview.
Secondly I can only calculate / break my string at the width of the Rect shape.
meaning I can only draw 1 line instead of filling out the box...
I could take the box height and divide it by the textsize to calc the number of lines that fit,
but then i would have to draw line by line ?
surely there must be examples for this , but i cannot seem to find them.
somewhat the situation :
View :
[xxxxx]----------
[xxxxx]----------
----------[xxxxx]
----------[xxxxx]
i want a very long string to be divided and placed at the [xxx] boxes
ta.

Related

Android - Canvas not drawing after Translation?

I am working on a custom view where i draw lines horizontally one after another left to right. When the collective width of all the lines combined crosses the width of the view , i translate the view. After translating the view , the view moves leftward , but canvas.drawLine stops drawing lines as soon as i translate the view. Any solution to this problem?
for(someCondition){
canvas?.drawLine(startX,startY,stopX,stopY,linePaint)
if(startX > (width)){
log("Invisible , setting translation to ${-(startX - width)}")
translationX = -(startX - width)
}
log("width is $width and startX is $startX")
}
as soon as i translate , the canvas stops drawing but translate keeps happening.
I solved the problem without having to translate anything.
Everytime the collective width of the lines crossed the width of the View , i increased the starting index of the array so that i drew only n elements, where n is the max number of lines that can fit inside the views width. This solution also helped avoid drawing unnecessary lines everytime a new element was added to the array.

Auto set rectangle length on canvas as per string length

I have an array of strings and I have drawn those strings on screen using android graphics library (Canvas, Paint). Firstly, a transparent rectangle is drawn then on this rectangle I paint the strings, giving the impression of text with border line and filled.
The problem is some strings are long and some are short, how can I modify the rectangle so that it is as long and as wide as the string is? (Like WRAP_CONTENT in textView)
Currently I am using this method
canvas.drawRect(_x-10, _y-10, _x+620, _y+30, rectanglePaint);
canvas.drawText(placeName, _x, _y, textPaint); //text
If there is a better way then please do let me know.
Note: It will be used in an AR app so the text will be moving from left to right and vice versa as the mobile is moving. _x, _y for place name works perfectly in 2nd line, I want text should remain highlighted, no matter how the mobile is moving.
Paint has a measure text method that will give the width of the text if it were to be drawn with that paint. Its get font metrics will probably give you some data that you can calculate the height from. The sizing, positioning, wrapping and rendering of text is a very complicated problem and I would advise using a child TextView (with a background) in your ViewGroup rather than trying to roll your own if you're doing anything complicated.

android canvas drawText

drawText draws a text from the providing point. But I need different behaviour.
Let's call the point(x,y) as p.
How drawText works:
pMYTEXT
What I need:
MYTpEXT
So I want to draw text exactly centered with this point. Is this possible?
I think to do it I should measure text and offset the point. But how can I measure it? It has not drawn yet.
Sure. Use the Paint function measureText to get the width of the string you want to the left of point p. Subtract that length from the x position of p, and draw it there.
Set the paint alignment to center.
paint.setTextAlign(Paint.Align.CENTER)
You can also use LEFT and RIGHT.

Locate text position in TextView

How can I calculate the position X , Y of a letter in a TextView by knowing it's index?
I need this to show a graphical pointer to the touched letter or word but I don't know how to locate the right place.
May this help you:
I am not aware of a simple direct way to do this but you should be able to put something together using the Paint object of the TextView via a call to TextView.getPaint()..
Once you have the paint object you will have access to the underlying FontMetrices via a call to Paint.getFontMetrics() and have access to other functions like Paint.measureText() Paint.getTextBounds(), and Paint.getTextWidths() for accessing the actual size of the displayed text...
Try this:
Rect bounds = new Rect();
Paint textPaint = textView.getPaint();
textPaint.getTextBounds(text,0,text.length(),bounds);
int height = bounds.height();
int width = bounds.width();
That should give you the height and width. Create an array of height/widths with a different value for each individual character, you could do it quickly if you used a loop and ASCII character codes. Then, you could add the widths of each character until you get to the one you want.
But there's what I think is an easier way. Instead of a TextView, try a LinearLayout with multiple textviews. If you use a textview for each character, you can just use getX and getY.

Android Rect not working

I have been trying to work on a simple code for two days now. I have tried all alterations but none seems to be working.
I am trying to draw vertical rectangles with different colors.
In the first one, I am using only one Rect variable and moving the coordinates. Here is the relevant part of the code.
Rect myRect1=new Rect();
Random colorMe=new Random();
for(int j=0;j<5;j++){
myRect1.set(myCanvas.getWidth()/5*j, 0, myCanvas.getWidth()/5*j, myCanvas.getHeight());
paint.setColor(Color.rgb(colorMe.nextInt(255), colorMe.nextInt(255), colorMe.nextInt(255)));
myCanvas.drawRect(myRect1, paint);
}
Other alteration which I tried was through an array.
Here is the code.
Rect[] myRect=new Rect[5];
Random colorMe=new Random();
for(int j=0;j<5;j++){
myRect[j].set(myCanvas.getWidth()/5*j, 0, myCanvas.getWidth()/5*j, myCanvas.getHeight());
paint.setColor(Color.rgb(colorMe.nextInt(255), colorMe.nextInt(255), colorMe.nextInt(255)));
myCanvas.drawRect(myRect[j], paint);
}
Can somebody please help me what is the problem actually there?
The first thing that seems obviously wrong (there might be more):
The minimum x-coordinate of your rectangle is myCanvas.getWidth()/5*j
The maximum x-coordinate of your rectangle is myCanvas.getWidth()/5*j
They are both the same value, so your rectangle is degenerate.
Offtopic, but very relevant: whenever you run into a problem like this, you need to break it down into smaller parts until you get it to do something. This will help you understand what's wrong:
Extract all subexpressions (like the color, and the generated x/y values) into local variables so you can easily inspect them in the debugger
Replace the random color by a predefined color (COLOR.YELLOW), to rule out the random element
Replace the caclulated rectangle by a fixed rectangle (say (10,10) - (20,20)) to rule out the coordinate calculations.
Replace the loop, to rule out the loop.
Your rect has a width of 0px. The parameters of the set() function are, in order, left/top/right/bottom. You use the same value for left and right, so the width is (right-left)=0.
Let's see your coordinates, first:
myRect1.set(myCanvas.getWidth()/5*j, 0, myCanvas.getWidth()/5*j, myCanvas.getHeight());
Top left corner has the same x coordinate as the bottom right corner, so you are drawing a rectangle with 0 width.
Add this line
myRect[j] = new Rect()
before
myRect[j].set(myCanvas.getWidth()/5*j, 0, myCanvas.getWidth()/5*j, myCanvas.getHeight());

Categories

Resources