Is it possible to expand the Canvas in Android? - android

I have a rotated TextView class whose Canvas I rotate in the onDraw method:
#Override
protected void onDraw(final Canvas canvas) {
canvas.save();
canvas.rotate(-90);
canvas.translate(-getHeight(), 0);
super.onDraw(canvas);
canvas.restore();
}
The TextView is being positioned and rotated correctly, but the text seems to be getting clipped off:
How can I expand the Canvas correctly such that the TextView does not cut the text off?
Note the highlight area is the result of drawing the clip bounds of canvas right before super.onDraw(canvas) so I know the clip region is correct.

Of course the text is clipped off!
If you rotate the canvas, you just rotate the drawing inside the View, whereas the bounds (which don't depend by Canvas, but they're defined by the View) aren't rotated.
Therefore you have to rotate the whole View, just like the code below:
this.setRotation(-90); // 'this' is your view.

Related

How to gradually increase the width of a Canvas?

I'm trying to implement a Waveform visualizer in my app which draws the waveform on a canvas from a real-time audio input. (Screenshot below)
What I need to do is to make it in a way that it can be scrolled horizontally from left to right.
I figured I should use a HorizontalScrollView and make the canvas gradually grow in width so that it spans over the screen width. Now I'm wondering how this can be achieved?
Keep the Canvas's width the same and use the canvas.translate() method when actually drawing.
So, let's say you have a Rect rect that contains the rectangle to draw (whose boundaries may exceed that of the View), as well as int horizontalOffset = 0 as the variable holding the offset of your drawing rectangle which will be adjusted as the user touches the View.
Now, when onDraw(Canvas) is called:
#Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.save();
canvas.translate(horizontalOffset, 0);
canvas.drawRect(rect, paint);
canvas.restore();
}
Then you can then override onTouch(View, MotionEvent) to calculate the horizontal offset based on the touch event and then invalidate() the View.
Note: this is clearly only drawing a Rect, but you should be able to modify what I did in there to draw whatever you're drawing. The important parts are (in order) save(), translate(), draw, restore().

drawLine() draws lines outside of its drawing area

I have a custom LinearLayout class that overwrites its onDraw() method. It goes into each row of ListView, as shown in the third picture. Everything looks great until I scroll the listview upward. Then the dotted line rendered from drawLine() call gets drawn over LinearLayout that sits above ListView. This is strange because the orange rounded rectangles get drawn without this problem.
My custom LinearLayout does call "this.setWillNotDraw(false);" I tried calling "LinearLayout1.postinvalidate();" in the ListView's onScrollListener, but it fails to get the top LinearLayout to redraw. What can I do to prevent the lines from getting drawn on top of LinearLayout?
Had to resort to the following workaround:
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Bitmap offScreenBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas offScreenCanvas = new Canvas(offScreenBitmap);
// do your drawings onto offScreenBitmap here
Paint offScreenPaint = new Paint();
offScreenPaint.setAntiAlias(true);
offScreenPaint.setFilterBitmap(true);
offScreenPaint.setDither(true);
canvas.drawBitmap(offScreenBitmap,0,0,offScreenPaint);
}
My code actually prepares offScreenBitmap at the beginning, because its contents are static and need to be drawn only once.

Rotating android canvas causes myRect.contains(X,Y) not to work

I've written a custom view that displays text in the center of the screen as seen below.
I've also made it so that if you touch the green box you can rotate and scale the text as seen below.
Heres the problem.
Whenever I rotate the text and let go, then try to rotate again it cant detect that the rect is being touched using myRect.contains(X,Y).
After some time I found that after its rotated, and touch where the original green box was, it allows me to rotate again.
OnTouchEvent is obviously calculating the Rects position correctly since it drawing in the correct location. I just can figure out why the touch coordinates seem to be referencing old positions.
Here's my onDraw() method.
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.rotate((float)mAngle, mBorderRect.centerX(), mBorderRect.centerY());
canvas.drawRect(mBorderRect, mTextBorderPaint);
canvas.drawText(mText, mBorderRect.left, mBorderRect.bottom, mTextPaint);
canvas.drawRect(mResizeRect, mBGPaint);
canvas.restore();
}
Remember that you're rotating the canvas, not the Rect. To get this to work you'll have to apply the inverse rotation on the touch position first, then compute myRect.contains(X,Y).

Get invalidated region in onDraw in EditText subclass

I'm trying to override onDraw in an EditText subclass, to show a custom subtitle.
I've got it working, but there are a few bugs.
Basicall, all I need to do is draw StaticLayout at a certain offset from top left corner of the view.
Unfortunantly, all I get in my onDraw method is canvas. The size of the canvas is equal to the size of the whole screen (320x480 on a device with 320x480 display) and its clip bounds can be pretty much anything - it can be the whole view; it can be only top or bottom part of the view if the view is inside scrollview and partially visible; it can even be same arbitrary rect inside the view, probably because superclass invalidates only some of its region.
So if I have this view with size 320x48, I can get canvas with size 320x480 and clipping rect (200, 200, 300, 230) (left, top, right, bottom). I don't understand how this clipping rect maps to my view coordinates.
I need to know where is top left corner of the clipping rect relative to the top left corner of the view. Unfortunantly, I cannot figure out how to get this.
Added:
This code will work on all os versions that I've tested:
private int[] coordinates = new int[2];
private Matrix identityMatrix = new Matrix();
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
getLocationInWindow(coordinates);
canvas.setMatrix(identityMatrix);
canvas.translate(coordinates[0], coordinates[1]);
//do the drawing in EditText coordinate space
canvas.restore();
}
However, I still have one question: why does it work?
I've trying overriding View class and it's onDraw method will always recieve a canvas which size matches that of the View itself. Canvas for direct View subclass will have no clipping rect. Same for TextView (direct ancestor of the EditText class). But it's not the same for EditText. Canvas that gets passed to onDraw method of EditText will always (or not?) have the size of the screen, and a custom clipping rect. This whole "translate by view coordinates in window thing" seems very hacky. I don't understand why I should translate the coordinate space.
I've tried hacking android source code for the answers, but found none. EditText has no onDraw of its own. Theoretically, there should be no difference between overriding TextView onDraw and EditText onDraw. But there is a difference. The canvas object passed to onDraw method will be different depending on whether its TextView or EditText. Why? How do I know when I should apply transformation to matrix, and when I shouldn't?
you can have it with the View.getLocationOnScreen method

Android: Scaling ImageView and it's onDraw() items

All, I've extended the ImageView in order to implement pinch and zoom scaling on the image. This is done by modifying the matrix and applying it to the image. Now, I am also overwriting the onDraw() to draw primitives (i.e. rectangles and circles). I've applied the matrix to the canvas and it appears to have handled the scaling properly, but the only problem is that that position is off on the drawn items. How do I go about translating the positions of the drawn items to reflect the new scale?
There is an aproach without matrix, you can implement the pinch and zoom directly in the onDraw method. Check this blog post: http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.translate(mPosX, mPosY);
mIcon.draw(canvas);
canvas.restore();
}

Categories

Resources