Custom class ImageView written text ends up behind setImageBitmap - android

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.

Related

Reference to drawn text using drawText() on a canvas

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);

Saving Multiple Circles to Bitmap android

I need to save a number of formatted circles to a Bitmap whenever a button is pushed, and then draw those circles to the canvas. I was looking around, but had a great deal of trouble finding resources on how to save a shape I had created that was not an already created resource file. Any advice on how to go about this would be appreciated.
Say you are in your custom view, but it should work also with a OnTouchListener in any view.
Create a mutable bitmap with Bitmap.createBitmap() with the same width and height of your view
use your logic for getting information of the circles, for example you can use onTouchEvent() to get the coordinates of you finger(s)
with this information you can use Bitmap.setPixel() to save it to the bitmap
optional, onDraw() you can draw your bitmap with Canvas.drawBitmap()
In case you need to draw shapes to the bitmap you can create a new Canvas(bitmap) and draw on this canvas: the pixels will be set on the bitmap.

change canvas size for TextView

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.

Transparecy in canvas android

I want to add canvas (somewhat transparent in color) on background which has some image on its Ontouch event. I want to remove that transparent color of canvas which makes my background image clear.
I'm using:
paint.setColor(Color.TRANSPARENT);
but nothing is happening on canvas. Any help would be greatly appreciated.
i have added transparent canvas on background...which makes my image above:
and ontouch resultant should be like this:

Tiling a Bitmap on a Canvas

I would like to create a 'graph paper' look to the Bitmap I am drawing via a Canvas, and trying to figure out the best way to do this.
I can't pass a source Bitmap containing the graph paper background to the Canvas constructor, as I am getting the Canvas in a SurfaceView via the .lockCanvas() call.
Some solutions I've tried:
I've tried implementing this solution in my SurfaceView's Thread.run(), but the issue I believe is when the BitmapDrawable is converted to a Bitmap... it loses the tiling properties.
canvas = mSurfaceHolder.lockCanvas(null);
BitmapDrawable TileMe = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.editor_graph));
TileMe.setTileModeX(Shader.TileMode.REPEAT);
TileMe.setTileModeY(Shader.TileMode.REPEAT);
Bitmap b = TileMe.getBitmap();
canvas.drawBitmap(b, 0, 0, null);
If I use the Canvas.drawBitmap method that takes a destination RectF as a parameter, it looks like the bitmap will be tiled to fill the RectF... but how do I declare a RectF reliably that fills the entire view area?
Setting the Activities background to the desired graph paper look also doesn't work, as the bitmap/canvas layout is opaque and blocks that from being seen.
Any ideas how to achieve this?
You have two easy solutions:
Either use a BitmapDrawable, but instead of extracting the Bitmap, just call BitmapDrawable.draw(Canvas). Don't forget to set the drawable's bounds to fill your drawing area.
Create a Paint with a BitmapShader and draw a rectangle with it (this is basically what BitmapDrawable does).
I'm sure there is a way to get a tiled effect using a SurfaceView. Unfortunately, it looks like you can't use the BitmapDrawable with a canvas. So you would probably have to implement you own custom tiling method by creating your own series of Rect's on the Canvas and drawing a scaled bitmap to each one.
It honestly wouldn't be that hard. Just get the width/height of the view, and create an array of Rect's based on this data that you will draw the Bitmap to.
Alternatively, if you don't need to make modifications to the actual tiled background on the fly, just draw it as a background and draw the SurfaceView on top of it. That post you linked provided multiple solutions to tiling a BitmapDrawable that you could implement.

Categories

Resources