I've been working on Android for a while and would like to know if it is possible to retrieve the position of a button in android.
My target is to get the X & Y coordinates and print them on the LOGCAT.
Some example to show me how would be appreciated.
Thanks
Sure, you can get these, make sure the views are drawn atleast once before you try to get the positions. You could try to get the positions in onResume() and try these functions
view.getLocationInWindow()
or
view.getLocationOnScreen()
or if you need something relative to the parent, use
view.getLeft(), view.getTop()
Links to API definitions:
getLocationInWindow
getLocationOnScreen
getLeft
getTop
Like Azlam said you can use View.getLocationInWindow() to get the coordinates x,y.
Here is an example:
Button button = (Button) findViewById(R.id.yourButtonId);
Point point = getPointOfView(button);
Log.d(TAG, "view point x,y (" + point.x + ", " + point.y + ")");
private Point getPointOfView(View view) {
int[] location = new int[2];
view.getLocationInWindow(location);
return new Point(location[0], location[1]);
}
Bonus - To get the center point of the given view:
Point centerPoint = getCenterPointOfView(button);
Log.d(TAG, "view center point x,y (" + centerPoint.x + ", " + centerPoint.y + ")");
private Point getCenterPointOfView(View view) {
int[] location = new int[2];
view.getLocationInWindow(location);
int x = location[0] + view.getWidth() / 2;
int y = location[1] + view.getHeight() / 2;
return new Point(x, y);
}
I hope the example can still be useful to someone.
buttonObj.getX();
buttonObj.getY();
Related
I need to get the X and Y position of the "thumb" of a seekbar.
Calling seekbar.getThumb().getBounds() returns me a bound that is relative to the seekbar.
Is there a way I can get the position of the thumb that's relative to the whole screen?
I ended up doing this, not quite sure what to do with the 5 there. Minus 5 is exactly the center of the x position of the dial.
// tipBar is the seekbar.
int [] location = new int[2];
tipBar.getLocationInWindow(location);
int x = location[0] + tipBar.getThumb().getBounds().centerX() + tipBar.getThumbOffset() - 5; // magic 5
int y = location[1] + tipBar.getThumb().getBounds().centerY();
I 'd like someone to advise me a way to find the coordinates of a point on a sprite in libgdx .
As you can see from the image I have set the sprite with the point of origin on the red dot , and I can not change it.
I would leave the red dot as the source and find the coordinates of the green on the same sprite .
Thanks for your help.
EDIT
#Override
public void create () {
img = new Texture("rocket.png");
font = new BitmapFont();
font.setColor(Color.BLUE);
MyInputProcessor inputProcessor = new MyInputProcessor();
Gdx.input.setInputProcessor(inputProcessor);
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
sprite = new Sprite(img);
spacesprite = new Sprite(new Texture(Gdx.files.internal("space.jpg")));
spacesprite.setPosition(0,0);
spacesprite.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
point = new Sprite(new Texture(Gdx.files.internal("point.png")));
batch = new SpriteBatch();
}
#Override
public void render () {
sprite.setPosition(Gdx.graphics.getWidth() / 2 - sprite.getWidth()/2, Gdx.graphics.getHeight() / 2 - sprite.getHeight()/2);
sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
point.setPosition(sprite.getX() + sprite.getWidth()/2 - point.getWidth()/2, sprite.getY() + sprite.getHeight()/2);
point.setOrigin(point.getWidth()/2, 0);
if(Gdx.input.isButtonPressed(Input.Buttons.LEFT)){
//sprite.setPosition(Gdx.input.getX() - sprite.getWidth()/2, Gdx.graphics.getHeight() - Gdx.input.getY() - sprite.getHeight()/2);
if(Gdx.input.getX() < Gdx.graphics.getWidth() / 2)
{
//System.out.println("x: " + Gdx.input.getX() + " - y: " + Gdx.input.getY());
sprite.setRotation(rotation++);
point.setRotation(rotation++);
System.out.println("Sprite: X" + sprite.getX() + " - Y:" + sprite.getY());
}
else
{
//System.out.println("x: " + Gdx.input.getX() + " - y: " + Gdx.input.getY());
sprite.setRotation(rotation--);
point.setRotation(rotation--);
System.out.println("Sprite: X" + sprite.getX() + " - Y:" + sprite.getY());
}
}
batch.begin();
spacesprite.draw(batch);
sprite.draw(batch);
point.draw(batch);
batch.end();
}
someone can adapt the code , when I rotate I'd get the position , but they are insecure about my implementation .
if you're looking for a middle point on the rectangular sprite, try something like this:
float x = obj.getOriginX() + obj.getHeight();
float y = obj.getOriginY() + obj.getWidth() / 2;
Either use Gonio, matrix or quaternions to calculate the rotated coordinates.
For a few simple calculations I'd use gonio, something like:
float angle_rad = sprite.getRotation() / 180.0f * PI;
float rotated_x = Math.sin(angle_rad) * y + Math.cos(angle_rad) * x;
float rotated_y = Math.sin(angle_rad) * x + Math.cos(angle_rad) * y;
If you have to do this more often look into matrices or quaternions, matrices are a little easier though: http://en.wikipedia.org/wiki/Rotation_matrix
I need to animate the drawing of a line graph. I'll receive an array of values (probably between 0 and 10) and that has to go on a graph over time. I want the line graph to be drawn, and the drawing should be visible; animated.
I've looked at Anders Ericsson's tutorial here: http://www.jayway.com/2012/08/29/creating-custom-android-views-part-3-animating-your-custom-views-smoothly/#comment-105813
The code here: https://github.com/andersericsson/ViewTutorialPart3
But I can't seem to swing it to work my way.
Is there another solution out there?
The solution I ended up using involved some algebra in the class where I extended View.
The function gets the array of scores (values between 0 and 10) from the activity and uses the values two-by-two as start and end points to a line (note that the end point of one line is the start point of the next line).
It calculates the length of the line and how many segments will be in the line; also how far to move in the x direction and how for to move in the y direction.
Then it calculates the x and y values of the end of the first segment in the line and draws that segment.
Then the xDirection and yDirection is added to the x and y points respectively and the line is drawn again, which now includes the first and second segment of the line. This is done for every segment in the line, and then the final line from point A to point B is drawn.
But it's not complete - the entire for loop in the setData function should be placed in a recursive function, because postInvalidateDelayed() doesn't pause the for loop from executing.
However, nothing is drawn on the canvas at all, hence the link to my other question currently on SO: Why are no lines drawn on my (custom View) canvas?
But for this question, I think that the solution I ended up using is probably not so bad. Comments?
public void setData(float[] scorePoints, float max, int totalScores){
Log.d(TAG, "Get stuff from activity");
scores = scorePoints;
numberOfScores = totalScores;
Log.d(TAG, "Number of scores = " + numberOfScores);
maxScore = max;
Log.d(TAG, "Max score = " + maxScore);
segmentToDraw = (float) 10;
//get the width of the area to draw
width = Math.abs(getWidth() - getPaddingLeft() - getPaddingRight());
//get the height of the area to draw
height = getHeight() - getPaddingTop() - getPaddingBottom();
Log.d(TAG, "Drawable area in view = " + width + " x " + height);
/*
* Now we start filling an array of points.
* The onDraw function will drawLines of groupings of four points in a given array.
*
* For the first segment, we'll draw from the x and y of the first point (which will be in the 1st and 2nd spots in our array)
* to the x and y of the first segment (which will be in the 3rd and 4th spots in our array).
* And then the 3rd and 4th spots in our array will be replaced by a new x and y
* marking the end of the second segment to be drawn from our first point.
*
* This will happen until the x and y is not smaller than the x and y of the final point of the line, any more.
* Then the 3rd and 4th spots in our array will be replaced by the x and y of the final point of the line.
*
* If there are more points to draw, the 5th and 6th spots in our array will also be created and filled with
* the x and y of the final point of the line because it'll be the first two values (x and y) for drawing the next line.
*
* So, yes, there will be duplicates in our array of points to draw, but a grouping of four spots will be used to draw one line,
* and the end point of the first line is the starting point of the second line, so we need it twice.
*/
points.add(getXPos(scores[0]));
points.add(getYPos(scores[0]));
points.add((float) 0);
points.add((float) 0);
x = points.get(0);
y = points.get(1);
startPoint = scores[0];
endPoint = scores[1];
for(int i=0; i<scores.length-1; i++){
String thePoints = "";
if(i>0){
startPoint = scores[i];
endPoint = scores[i+1];
x = points.get(i*4);
y = points.get((i*4) + 1);
}
startPointX = getXPos(startPoint);
startPointY = getYPos(startPoint);
endPointX = getXPos(endPoint);
endPointY = getYPos(endPoint);
distanceOfLine = (float) Math.sqrt(Math.pow((endPointX - startPointX), 2) + Math.pow((endPointY - startPointY), 2));
Log.d(TAG, "Distance of line = " + distanceOfLine);
//get number of segments in line
numberOfSegmentsInLine = (int) (distanceOfLine/segmentToDraw);
Log.d(TAG, "Number of segments in line = " + numberOfSegmentsInLine);
//get distance to move in Y direction
yDirection = (float) ((endPointY - startPointY)/ (float) numberOfSegmentsInLine);
Log.d(TAG, "Move " + yDirection + " in Y direction");
//get distance to move in X direction
xDirection = (float) ((endPointX - startPointX)/ (float) numberOfSegmentsInLine);
Log.d(TAG, "Move " + xDirection + " in X direction");
//loop for each segment
for(int j=0; j<numberOfSegmentsInLine; j++){
x += xDirection;
y += yDirection;
points.set(points.size()-2, Float.valueOf(x));
points.set(points.size()-1, Float.valueOf(y));
Log.d(TAG, "Line : " + (i+1) + " Segment : " + j);
Log.d(TAG, "X = "+ (x+xDirection) + " Y = " + (y+yDirection));
Log.d(TAG, "Call invalidate now!");
//invalidate();
//postInvalidateDelayed(delayMilliseconds);
}
//draw final line
points.set(points.size()-2, endPointX);
points.set(points.size()-1, endPointY);
invalidate();
//postInvalidateDelayed(delayMilliseconds);
if(i<scores.length-2){
points.add(endPointX);
points.add(endPointY);
points.add((float) 0);
points.add((float) 0);
}
for(int k =0; k<points.size(); k++){
thePoints = thePoints + " : " + points.get(k);
}
Log.d(TAG, "Points array = " + thePoints);
}
}
#Override
public void onDraw(Canvas canvas){
//setWillNotDraw(true);
Log.d(TAG, "DRAW DAMNIT!!!");
Log.d(TAG, "Width = " + (int) width + " Height = " + (int)height);
paint = new Paint();
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(4);
paint.setColor(Color.RED);
//paint.setAntiAlias(true);
//paint.setShadowLayer(4, 2, 2, 0x81000000);
Bitmap bitmap = Bitmap.createBitmap((int)width, (int)height, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
String drawPointsGo = "";
float[] drawPoints = new float[points.size()];
for(int i=0; i<points.size(); i++){
Float f = points.get(i);
drawPoints[i] = (float) (f != null ? f : 0.0);
drawPointsGo = drawPointsGo + " : " + drawPoints[i];
}
Log.d(TAG, "Draw Points: " + drawPointsGo);
canvas.drawLines(drawPoints, paint);
}
Try looking into CountDownTimer or Runnable so that onTick() or when it's time to post you can update your line by drawing a few more pixels. This would let you draw one line after the other but would give the effect of animation.
Here is the general direction to try:
Make sure you are comfortable with Canvas drawing.
Then you will want to create a new View (subclass it) and override the onDraw method.
This method should only draw the graph partially, based on the elapsed time since the start of the animation.
Ok, so I am trying to write a code which returns back the values of all the 4 corner position of the map according to current view able area. For example, a user access Google maps through his device, he focuses on a particular area and hits a button and gets back the position (latitude and longitude) of all the four corners.
For this I have the center coordinate and now all I want span of the current viewable area and get the top left and bottom right corner coordinates. for this I used,
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x; // returns 480
int height = size.y; // returns 800
Now I want to convert them into latitude and longitude values so that I can use simple math such as:
topLeftLat= centerLat + lat/2;
topLeftLon= centerLon - lng/2;
bottomRightLat= centerLat - lat/2;
bottomRightLon = CenterLon + lng/2;
This would give me the corner cordinates.
lat and lng being the converted sceen resolution in latitude and longtiude values.
This took 5 minutes search on google so I don't know why I'm giving you this ... learning to research is the biggest part of being a developer.
GeoPoint mapCenter = myMap.getMapCenter();
float centerLatitude = (float)(mapCenter.getLatitudeE6())/1000000;
float centerLongitude = (float)(mapCenter.getLongitudeE6())/1000000;
float latitudeSpan = (float)(myMap.getLatitudeSpan())/1000000;
float longitudeSpan = (float)(myMap.getLongitudeSpan()/1000000);
GeoPoint mapTopLeft = myMap.getProjection().fromPixels(0, 0);
float topLatitude = (float)(mapTopLeft.getLatitudeE6())/1000000;
float leftLongitude = (float)(mapTopLeft.getLongitudeE6())/1000000;
GeoPoint mapBottomRight
= myMap.getProjection().fromPixels(myMap.getWidth(), myMap.getHeight());
float bottomLatitude = (float)(mapBottomRight.getLatitudeE6())/1000000;
float rightLongitude = (float)(mapBottomRight.getLongitudeE6())/1000000;
String info =
"Center: " + centerLatitude + "/" + centerLongitude + "\n"
+ "Top Left: " + topLatitude + "/" + leftLongitude + "\n"
+ "Bottom Right: " + bottomLatitude + "/" + rightLongitude + "\n"
+ "latitudeSpan: " + latitudeSpan + "\n"
+ "longitudeSpan: " + longitudeSpan + "\n";
Toast.makeText(AndroidMapActivity.this,
info,
Toast.LENGTH_LONG).show();
}};
The full tutorial is available at Android-er: Get the the coordinates (Latitude and Longitude) currently displayed on MapView
is it possible to find the View that is displayed at a given absolute x/y pixel coord?
Edit: I found a suitable Solution that works great:
private View findViewByCoord(float x, float y){
TextView textView = null;
int[] location = new int[2];
int width = 0;
int height = 0;
for(int reference : cardReference){
textView = (TextView) findViewById(reference);
textView.getLocationOnScreen(location);
width = textView.getWidth();
height = textView.getHeight();
if(location[0] <= x && x <= (location[0] + width) && location[1] <= y && y <= (location[1] + height)){
Log.i("Test", "Card " + textView.getText() + " is pointed");
return textView;
}
}
return null;
}
Where cardReference is an array of integer to Resources (in my case 20 TextViews arranged in a 4 x 5 Matrix):
int[] cardReference = new int[]{R.id.card1_1, R.id.card1_2, R.id.card1_3, R.id.card1_4,
R.id.card2_1, R.id.card2_2, R.id.card2_3, R.id.card2_4,
R.id.card3_1, R.id.card3_2, R.id.card3_3, R.id.card3_4,
R.id.card4_1, R.id.card4_2, R.id.card4_3, R.id.card4_4,
R.id.card5_1, R.id.card5_2, R.id.card5_3, R.id.card5_4};
To speed up performance i would consider to use an array of TextViews then call findViewById() in every Loop.
One 'solution' would be to loop through the parent view's children and check the getLeft() and getTop() coordinates against the X and Y coordinates of your choice. If there is a match, you have your view.
I'd like to hear other alternatives though.
Edit: You'd also have to work out the height/width of the view too in relation to the left and top coordinates given to see if your coordinates are within that range.