I am creating a Custom View where I have to draw text at specific positions on the canvas. For using canvas.drawText() we have to create a paint object that has to be passed as a parameter while calling the drawText(). If I want to get the text to be rendered as centrally aligned, I am using the setTextAlign() call while making the paint object.
statPaint.setTextAlign(Paint.Align.CENTER);
Also when I call the drawText(), I am required to set the starting point for the text to be rendered, as shown below.
canvas.drawText(stat1,paddingLeft,paddingTop,statPaint);
paddingLeft and paddingTop tell the drawText() where to draw the text from. Passing these two parameters is mandatory for the function call. But I still am not able to get the text centrally aligned. How do I get this text centrally aligned.
Also if I want to get two different texts in the same line, the first one being centrally aligned in the first half of the screen, and the second text, centrally aligned in the second half of the screen, how do I do that? The screen is divided into halves vertically.
Thanks in advance.
Here is how to center text in any rectangle (just change left/right/top/bottom)
// Allocated once as a member var, not in each onDraw
private Rect rect = new Rect();
// Inside onDraw
int left = 0;
int right = getWidth();
int top = 0;
int bottom = getHeight();
paint.getTextBounds(text, 0, text.length(), rect);
FontMetrics fm = paint.getFontMetrics();
float x = left + (right - left - rect.width()) / 2;
float y = top + (bottom - top) / 2 - (fm.descent + fm.ascent) / 2;
canvas.drawText(text, x, y, paint);
Related
I want to take user input in numbers and based on that input I want to draw multiple numbers of rectangles on screen using for loop.
This is the image that I want to implement.
thank you in advance.
There are several ways to do this. Probably the simplest way is to draw rects to a canvas using 2 nested for loops, like so:
for (int n = 0; n < numRows; n++){
for (int m = 0; m < numColumns; m++){
canvas.drawRect(leftMargin + m*columnWidth, topMargin + n*rowHeight,
leftMargin + m*columnWidth + width, topMargin + n*rowHeight + height, myPaint);
}
}
where:
numRows = # of rows to draw
numColumns = # of columns to draw
leftmargin = distance from left side of screen to first column
topMargin = distance from top of screen to first row
columnWidth = width of each column of rects
rowHeight = heigth of each row of rects
width = width of each rect, if equal to columnWidth there will be no space between rects.
height = height of each rect, if equal to rowHeight there will be no space between.
myPaint = the Paint object you define for your rects, where you would specify things like color, filled in or outline, opacity, etc.
I have a multi-line TextView. Is there a way to get the x coordinate of the pixel to the left of a character e.g. the 3rd character? In my case it will always be the 3rd character, but a general solution would be nicer.
I have the y coordinate of the pixel just above the character using:
Layout textViewLay = mTextView.getLayout();
int posY = textViewLay.getLineTop(0);
but the x coordinate has me stumped. Any ideas? I'm probably missing something really simple.
Try to use this code:
Rect bounds = new Rect();
Paint tpaint = textView.getPaint();
tpaint.getTextBounds(text,0,2,bounds);
int height = bounds.height();
int width = bounds.width();
I am using Canvas.drawCircle to draw a circle in Android laout.
Method gets 3 parameters - first two are position - x and y.
Is it possible to skip hardcoded position of the circle and draw it centered ?
Following code can be used to get the width and height of the screen.
int width = this.getWidth();
int height = this.getHeight();
To draw circle in the middle of screen you can call :
Canvas.drawCircle(width/2, height/2)
You can paint a circle centered to the screen like this:
Display disp = ((WindowManager)this.getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
canvas.drawCircle(disp.getWidth()/2, disp.getHeight()/2, radius, paint);
Assuming you are extending the View class:
int CentreX = (this.getWidth() / 2);
int CentreY = (this.getHeight() / 2);
Given a TextView, is it possible to know at runtime the X and Y coordinates of where it is drawn?
Is it also possible to know the size (width/length) in pixels?
There are getLeft(), getTop(), getWidth(), getHeight() methods for a view, it works for textView too. for more information , see the following link...
getLeft() and getTop() will return you the starting x,y co-ordinates.
http://developer.android.com/reference/android/view/View.html
Coordinates relative to parent
int x = textView.getLeft();
int y = textView.getTop();
Absolute coordinates
int[] location = new int[2];
textView.getLocationOnScreen(location);
int x = location[0];
int y = location[1];
See this answer for more.
Pixel size
int width = textView.getWidth();
int height = textView.getHeight();
Notes
If you are getting (0,0) it could be because you are getting the relative coordinates related to the parent layout (and it is sitting in the top left corner of the parent). It could also be because you are trying to get the coordinates before the view has been laid out (for example, in onCreate()).
I got my image to at least SCROLL, but I don't want it to scroll past the image itself. I have variables called maxLeft, maxRight, etc that I have that I currently just set to
ImageView img = (ImageView)findViewById(R.id.mapimg);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int maxX = (int)????;
int maxY = (int)????;
// set scroll limits
final int maxLeft = (maxX * -1);
final int maxRight = maxX;
final int maxTop = (maxY * -1);
final int maxBottom = maxY;
I've been messing around with what I could in place of the question marks I put there, but I seem to be stuck, especially when I try on different emulators. Any help would really be appreciated! Thanks
If I understand your question correctly, you want to extend ImageView to add scrolling. If that's the case, you don't want to use getWindowmanager() as that returns the dimensions of the entire screen (including the title bar). Rather, you want to extend ImageView and get the view's dimensions from onMeasure. You can check out my answer here where I added zoom functionality and panning to ImageView.
I used setImageMatrix and postTranslate to set a matrix equal to the image and move it. To track the image's location, I used the following:
float f[] = new float[9];
matrix.getValues(m); //see matrix documentation. inserts matrix values into f.
float x = m[Matrix.MTRANS_X];
float y = m[Matrix.MTRANS_Y];
Float x and y will track the top left corner of the image. Make sure if the next event will cause the image to scroll out of bounds, you adjust postTranslate to equal the border of the image. The answer i linked above should give you a good place to start, and if you also want zoom functionality, then you're in luck, because you don't have to do any additional work.