why there ist no counterpart to getLocationOnScreen? The intuitive way would be setLocationOnScreen... I have an Image View and want to get these coordinates to give the same coordinates to another Image View.
The mentioned intuitive way would be:
int [] location = new int [2];
imageView1.getLocationOnScreen(location);
imageView2.setLocationOnScreen(location);
But this set command is not available. This would be the easiest thing :( Is there any counterpart?
PS: my Image has no margins...
Thanks for help
Because position on screen is controlled by the layout that the ImageView is in, not the ImageView itself. If you need a layout that allows you to position subviews at specific coordinates, try a FrameLayout.
Related
In my application I have a requirement to animate an image (this image view has an arrow set as source).
I am unable to figure this out how I can achieve this.To solve this i got x and y coordinates of second view that is rectangle after getting coordintes i am setting
scaleX() of image view that is purple line but I am not geeting desire out put because it stretches to the whole screen along x-axis
here is the code what i tried is
int x = (int) imageView.getX();
imageView2.setScaleX(x);
here imageview is rectangular box and imageview 2 is the purple line
Why you dont try with scenes? It's exactly what you need.
http://developer.android.com/training/transitions/scenes.html
I think you are getting the correct output when using the above code. Scale is defined against initial dimensions. And you would need to use something like this
dist = box.getX() - circle.getX()
and your scale would be
imageView2.setScale(dist/distInitial)
where distInitial you compute it at creation time using the dist formula.
You might need to change the position because it is scaled around the center of it (i.e. a smaller scale shrink margins to center of image)
You might want to perform this operations using a Canvas. It might be more efficient.
I'm making a app for Android that shows a camera image (after going through some manipulation) on screen using an ImageView and I want to allow to move other ImageViews on the screen - but only inside the the parent ImageView.
Also, I need to get the coordinates of the child views relative to the coordinates of the parent ImageView. i.e - So that (0,0) will not be the whole screen, but the ImageView that shows the camera.
Is that possible at all? If so - How?
Help is much appreciated! Thanks
If you want to move the views by drag here's a lot of info around, e.g Moving image with touch events .
What about getting the top and left side of minorView relative to majorView it's also straightforward with some plain substraction minorViewRelativeTop = minorViewTop - majorViewTop. Use View.getLocationInWindow() for getting these absolute positions.
N.B. That View.getLocationInWindow() will usually not work just inside Activity.onCreate() so better call it after some delay. For testing it could be sth like:
majorView.postDelayed(new Runnable() {
#Override
public void run() {
int[] location = new int[2];
majorView.getLocationInWindow(location);
int majorViewLeft = location[0];
int majorViewTop = location[1];
}
}, 1000);
I have a textview which contains longs text that covers half of the screen,
In this textview I have two words at different position above which I have to place a blank layout which will give it a glass effect.
I can do this by placing the textview in a framelayout and placing the layout above it.
The problem is how will I get the coordinates of the words, as the text may be arranged at different position on different devices.
I want to place a layout at the place marked white. Can some one please help
Thanks.
This will be helpful for you
public void getLocationOnScreen (int[] location)
Since: API Level 1
Computes the coordinates of this view on the screen. The argument must be an
array of two integers. After the method returns, the array contains the x and y
location in that order.
Parameters
location an array of two integers in which to hold the coordinates
See this code, this link, and also this link.
I'm not sure you can get the coordinates for the text directly from TextView - you should search inside TextView class source code. But.... if you render the textview as bitmap and then to seach inside the bitmap the coordinates of the text (if the searched text is blue should be easy to get the rectangle!
What you really want here is a backgroundspan :
span = new BackgroundColorSpan(Color.WHITE);
text.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
I'm trying to make my own custom view, currently all it does is draw an image on a specific x and y coordinate and then draws the similar images repeatedly on different locations.
I want to be able to create a button on each instance of the image that is drawn. if one image is clicked it will have cause something different to happen depending on which image is chosen.
How can I implement this?
would I have to create a different view for each image/button combination and then set an onClick event?
Let me try to be a little more clear
I'm trying to make a map using hexagon (different types of terrains for different players)
I've figured out how to get them to draw (see here - they will have a border to show what terrain is owned by whom)
I just made a custom view class and had the hexagons drawn using a Canvas; however, I'm not sure how to be able to make the hexagons into buttons so that I can differentiate between which hexagon was chosen and how it should react to the opponents spot.
I was thinking of making a ViewGroup called Terrain to contain the Nodes(hexagons) that belong to the player and have a group of Node Views that only draw the hexagon where it should be located.
the question is can i make each node or the entire viewGroup into a button (or do an onTouch ) if a certain hexagon is pressed?
If I understood well, you should do :
One template my_pictures.xml which is a simple button.
Then you create a custom adapter which as a function for each kind of image you wanna create. By this, I mean that you will have to change the background value of your button depending on what you need it to be. Then you do a notifyDataSetChanged(); to refresh the container with the new button.
For the Listener, you just have to add it either in your activity or your adapter when you create your buttonImages, I don't know what's better.
Thanks for your help!
I figured out what I needed to do.
I have a NodeView class and in my GameActivity class, I'm using a relative layout and setting the layout parameters of where I wanted things positioned
RelativeLayout rl = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(95,95);
params.leftMargin = 10;
params.topMargin = 10;
params = new RelativeLayout.LayoutParams(95,95);
params.leftMargin = 10+95*x;
params.topMargin = 81+(71*y);
rl.addView(new NodeView (this,0,0,1,1), params);
This helped me add things where I needed them and now all I'm trying to figure out how to scroll around the territories on both the x and y axis (I tried ScrollView but that only allows me to scroll on the y-axis, I'm looking into it though)
I want to fill the screen with a 100 different letters in random positions. On the iPhone I just created a bunch of UILabels set their x and y positions and then used animations to move them about.
On Android it doesn't look like I can add a TextView to my view and specify its X and Y. Is there a way to do this?
View gameView = findViewById(R.id.gameboard);
tv = new TextView(gameView.getContext());
tv.setText("A");
tv.setWidth(w); tv.setHeight(h);
// How to set the X and Y?
EDIT: The solution was to use AbsoluteLayout:
AbsoluteLayout al = (AbsoluteLayout)findViewById(R.id.gb_layout);
tv = new TextView(this);
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(
AbsoluteLayout.LayoutParams.WRAP_CONTENT,
AbsoluteLayout.LayoutParams.WRAP_CONTENT,10,10);
params.x = 50;
params.y = 50;
al.addView(tv, params);
and to move it base on MotionEvent me:
AbsoluteLayout.LayoutParams p = new AbsoluteLayout.LayoutParams(
AbsoluteLayout.LayoutParams.WRAP_CONTENT,
AbsoluteLayout.LayoutParams.WRAP_CONTENT,(int)me.getX(), (int)me.getY());
mTV.setLayoutParams (p);
I think you are making a game and for this you should look into SurfaceView here is an good example then
1...if you look into code there is onDraw method where you will get the width of screen and height
2...Now taking this width and height as seed for random generator get x and y position.
3...Iterate through point 2 100 times and you will get the desired result
You can use a FrameLayout for this. Set each TextView's background to transparent and add it to the FrameLayout. Make each TextView, and the FrameLayout, fill their parent. The FrameLayout places all its child views at (0,0). To move letters around, just change the top and left padding of the corresponding TextView.
In android positioning child views is the responsibility of the layout class.
You need to create a custom.layout and overide the onLayout method. In this method you can iterate through all the child views calling View.layout(left,top,right,bottom) on each child.
i found this example code whilst trawling the net :
https://gist.github.com/882650
You should also check out view.setTranslationX (and Y) which might be exactly what you need
I'm not entirely sure how you'd go about doing this in code, but you need to use an absolute layout as your root element. (edit: Do not use absolute layout, as it was pointed out that it is deprecated. The closest alternative seems to be RelativeLayout.)
The properties in the XML format for the absolute layout coordinates are layout_x and layout_y.
edit: A little research is saying you need to be using setLayoutParams, but my Eclipse IDE is not working properly, so unfortunately I can't test exactly what you're looking for.