I have a canvas on which I have drawn some text using drawText(), is it possible to get a reference to that text? I want to select that text using the onLongPress() gesture and I think I need to have the reference of that text to do that.
You won't be having refrence to what you drawn using drawText() on canvas.To implement select text you have to use Paint.measureText() to get height and width of your text and then draw the backgound to make a custom effect of text selection(handle long press using onTouch()).
When drawing text on canvas you have to handle every behaviours of text view as your own.
An alternate way is to to Create a TextView and add it to any layout and then use the following code to draw the layout into canvas
//measure the width and height of the layout(covers entire canvas)
vLayout.measure(canvas.getWidth(), canvas.getHeight());
//set the bounds of the layout.
vLayout.layout(0, 0, canvas.getWidth(), canvas.getHeight());
vLayout.draw(canvas);
Related
I need use TextView with outline text. I try use it: https://stackoverflow.com/a/10294290/4181010 , but it doesn't work correctly with android:layout_width="wrap_content", because it doesn't increase canvas when add outlines.
I need to increase the canvas to include the value of strokeWidth.
I suggest I have to override onMeasure somehow, but TextView call final method setMeasuredDimension at the end of onMeasure to apply view size and I can not intervene at this point so as not to have to rewrite the whole method.
Scaling the canvas in OnDraw does not work either, because TextView scales draw text with canvas.
My solution is increase canvas and use canvas.translate() to move in right position before draw text.
And my question: How can I increase the size of the canvas with minimal intervention? Or someone know another solution for this problem?
I'm guessing you are drawing the text twice, once with the orange (manually on the canvas) and once with the white (what the TextView is drawing with super.onDraw).
Instead of drawing the larger orange manually, you could instead draw the smaller white text and leave the orange text to TextView's super.onDraw. That way everything should fit within the canvas.
I would like to know if it is possible to add a textview programmatically in a custom view that herites from View. In this customView, I draw some shapes with a canvas and I need to put some text too. I tried to use drawText but it doesn't offer enough possiblities, especially for the position of the text. Basically, i want to put text inside, and precisely in the middle of a circle and at the same time that my text fits right in the circle (I already know how to do that).
That's why I'm wondering if it is possible to add a textview by just declaring it and draw it. Maybe I need to use an Inflater ?
I don't really know what are the best choices so I need your help :)
You can use drawText(String text, float x, float y, Paint paint) method for this.
Like this :
Paint mPaint = new Paint();
mPaint.setColor(Color.BLACK);
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawText("YOUR TEXT HERE", 10, 20, mPaint);
Instead of extending View you should extend a ViewGroup subclass like
FrameLayout.
This way you can still use the canvas to draw your custom view, but also add children views to your custom drawn viewgroup.
I'm currently using the following method to draw some text to the SurfaceView:
canvas.drawText("someText", 0, 0, paint);
Yet what if the text exceeds the width of the screen? Is it possible to define a region wherein the text can be drawn?
So now when the string's width exceeds the rectangles width, the text will be formatted to fit underneath the above text and so on eg.
"sometext"
"text carried"
"on"
Based on this answer, I believe this is what the Layout subclasses are used for. Per documentation:
A base class that manages text layout in visual elements on the
screen.
For text that will be edited, use a DynamicLayout, which will be
updated as the text changes. For text that will not change, use a
StaticLayout.
And with each subclass there's a note:
This is used by widgets to control text layout. You should not need to use this class directly unless you are implementing your own widget or custom display object, or would be tempted to call Canvas.drawText() directly.
Which sounds exactly like what you're doing.
It's basically a replacement for canvas.drawText(). It can be used in this fashion:
TextPaint tPaint = new TextPaint(paint);
StaticLayout sLayout = new StaticLayout(sText, tPaint, mWidth, widthToFill, Layout.Alignment.ALIGN_CENTER, 1.2f, 1.0f, false);
canvas.save();
canvas.translate(posX, posY);
sLayout.draw(canvas);
canvas.restore();
I want to draw text view with 2 color like the text in this link here.
Anyone can show me how to do?
See SpannedString & SpannedStringBuilder & ColorSpan
http://developer.android.com/reference/android/text/style/ForegroundColorSpan.html
http://developer.android.com/reference/android/text/SpannedString.html
a simple asnwer is to override onDraw() method, draw your test with black color and create a paint and set to it an xfermode
paint.setXfermode(new AvoidXfermode(Color.BLACK, 255, AvoidXfermode.Mode.TARGET))
paint.setColor(R.color.red);
and draw on canvas a rectangle with the desired width using this paint
hi
The custom ImageView is setting a Bitmap like this, in the constructor.
setImageBitmap(btm);
In my onDraw method im writing text like
canvas.drawText
Text is showed and I can move the text around using onTouchEvent.
But the text is behind the Bitmap.
I thought setImageBitmap(btm) would be part of the Canvas??
Any ide!
When i draw the Bitmap in onDraw like this, The screen is black!!
canvas.drawBitmap(bitmap, 0,0, null);
If you want an ImageView that also displays text and you're already handling onDraw, why not just draw the Bitmap manually to the Canvas in onDraw? That way you have control over the order of the drawing. Draw the image to the canvas first and then the text, and the text will no longer be obscured by the image.