I am currently trying to get an image view containing an image to move from top of the screen downwards. When it reaches the buttom of the screen i'd like to switch direction going upwards again.
My problem is i can't find the exact spot, where i should make the image view change direction. My screen has a very high resolution and therefore i've set my os, windows, to enlarge all components. I can therefore not use my screen size to calculate how many pixels there are from top to bottom.
Therefore i use following code to get height of screen:
Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();
bounds.getHeight();
My Imageview is initialized at point (0,0) and i can therefore at any given time get the y-koordinate of my top left corner by using imageview.getY()
Therefore i should move it downwards until the value of imageView.getY() + height the image view equals the height of the screen.
But this solution seems to make my image view switch direction a bit before it reaches the bottom of the screen.
For calculating height of image i use the method imageView.getFitHeight();
I suspect imageView.getFitHeight for delivering the height desired by the imageview before it is actually determined by the underlying anchor pane and therefor i am not sure imageView.getFitHeight() actually delivers the height of the imageView. I can't seem to find any other method in imageView which regards height of the imageView.
I don't know how to make the imageview switch direction exactly at the bottom, can anybody help?
Regards Martin
Related
In android I would like to draw flower by adding each petal from a center point. I used setRotation on the image view but the center point is different for each petal. (I mean the center point from the flower) Can anybody look at my code and suggest me correction? Thanks.
int angle=0;
int ypos=500;
int xpos=500;
RelativeLayout layout = (RelativeLayout)findViewById(R.id.ln1);
for(int i=0;i<10;i++)
{
ImageView image = new ImageView(this);
image.setLayoutParams(new android.view.ViewGroup.LayoutParams(150,400));
image.setX(xpos);
image.setY(ypos);
image.setPadding(-7,-30,-10,0);
image.setPivotX(1.0f);
image.setScaleX(1.5f);
image.setScaleY(1.5f);
image.setImageResource(R.drawable.petal);
image.setRotation(image.getRotation() + angle);
angle=angle+36;
layout.addView(image);
}
Image I get is this
When you rotate the image, the rotation is done with the top left corner of the image, not the center of the rotated image.
Below image might illustrate this. The black square represents your image. The lefthand site shows the situation you have now. The righthand side shows the situation you want.
Before rotating, you should subtract half the width from the x position, and add half the height from the y position. Then you should get the desired image.
As user Ralf Renz pointed out in their comment, you could also simply start with an angle of -36. This is a useful workaround.
I accomplished this using the following strategy.
image.setPivotY(-1);
Now the flower was coming in a different design because petal was Tilted. So I Tilted my actual image to the opposite direction using inkscape and now I get the desired output.
I have an imageview which only takes up a portion of the screen. And I have a textview on top of the imageview. When the user touches the textview, they can drag it to where ever they want on the screen. However, I want to limit the user to only be able to move the textview on the imageview.
When the textview is moved to the top or right side of the screen it automatically blocks it from going any further (similar to if you move your computer mouse to the top or left side of the screen). I want to try to mimic this effect just in the bounds of the imageview.
I have tried numerous techniques like getting the size of the imageview and comparing it to the y and x values and if the textview would go past this point it would equal this point. However, no success so far.
This is my current code (within the OnTouchListener()):
if (touchY >= imageHeight){
params.leftMargin = leftSideText;//sets the margin so the textview is displayed on the screen according to them because its aligned to top and left
params.topMargin = (tvOne.getHeight()) - imageHeight;
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
tvOne.setLayoutParams(params);
}
where some of the variables are:
touchX = (int) eventTwo.getRawX();
touchY = (int) eventTwo.getRawY();
//helps make the edit text start where the user touches the screen rather than that just being the top corner
int editTextWidth = tvOne.getWidth() / 2;
int editTextHeight = tvOne.getHeight() / 2;
topText = touchY - editTextHeight;
leftSideText = touchX - editTextWidth;
This code just makes the textview disappear once it goes past this point.
So, to clear up what I'm asking, how do you limit textview boundaries to the range of an imageview? If you could provide any help or suggestions it would be greatly appreciated, thanks!
EDIT: I found the reason to my problem, yet, I have not found the solution. First, it seems the textview is not disappearing its just not visible on the black background below the image. Second, the reason why the textview is going past the imageview is because the full image is not being displayed, or so it seems. So, when I get the height its referring to the whole image height, yet, the whole image is not being displayed.
Take a look at my question here
I'm new to Android but i have a simple problem.
I have a vertical linear layout, if i place here a normal button, and give it the MATCH_PARENT flag for the width and the height, the button will fill all the space. All ok.
Now i have a my simple custom view (a class that extends View) and i overrided the OnDraw method for draw a cross in the middle of the view. For draw the cross i need to get the width and the height of the control, if in the UI Layout editor of eclipse i set a fixed width and height, it works correcly (i see a preview of the cross directly in Eclipse) but if i extend the View to the borders i got only WARP_CONTENT (With button i get MATCH_PARENT), no problem, i change it in the XML, but LayoutParams width and height will return 0!! (I see the error directly in the preview of eclipse!) Why?
I tried to set manualy the width and the height in the OnWindowFocusChanged method to wait but it give me always 0.
The problem is that the view actualy have the rights dimensions but when i call the getLayoutParams().width it give me 0, so i see a white rectangle in the right position (white background of the view), but the cross is drawed on the upper-left corner!
Sorry for my bad english.
Thank you.
Mauro.
i think u need to set this parameter for your view.
android:layout_alignTop
or something related to this.i guess
Suppose I have an Android View that is being partially masked by another View. For instance, imagine that the masking view has a transparent rectangular "window" in the center of it, and my view is centered beneath the window and possessing dimensions that are larger than the dimensions of the window. In such a case, some rectangular portion of the view will be visible, with the rest being obscured by the mask.
So my question is, is there any straightforward way to retrive the position and dimensions of the visible area without having any access to the masking view itself (so without knowing how big the mask's "window" itself is)?
I tried calling getLocalVisibleRect(), which sounded promising, but that only seems to return the view's layout rectangle and not the rectangle that is actually visible.
This is pretty late to the game, but I suspect that getGlobalVisibleRect does what you want.
I have used getWindowVisibleDisplayFrame method to determine if view is partially visible or not to determine if a soft keyboard is open. You can try it out.
Rect r = new Rect();
// r will be populated with the coordinates of your view
// that area still visible.
rootView.getWindowVisibleDisplayFrame(r);
Does this not work: http://developer.android.com/reference/android/view/View.html#getDrawingRect(android.graphics.Rect)
The documentation says that it does what you want.
I'm having a problem with positioning images,I need to position a lot images over another larger background image.
An image Like this
I have tried Absolute but it does not keep the position of the image say I wanted to put a clip art image of a board pin over the background image and need it pointing at a sun and when it's clicked I get a popup dialog,
but then when I change the size of the emulator screen the clip art image is not at the same position I wanted it on the background image.
I first tried just putting the clip art on it with a image editor and used onTouch Listener but that didn't work out when I changed the size of the screen with the x and y coordinates. And tried Absolute Layout and that doesn seem to keep the position.
any ideas would help me big time thanks
AbsoluteLayout is deprecated, so it's probably best to use relative layout alongside with dp.
You could use relative layout so you can use layout_below="#id/view1", android:layout_toRightOf="#id/view2", and android:layout_toLeftOf="#id/view3". You can also use android:layout_marginLeft="10dip", android:layout_marginRight="10dip", android:layout_marginTop="10dip", and android:layout_marginBottom="10dip" to move the views left/right and up/down relative to their current positions. There is also ALIGN_PARENT_LEFT, ALIGN_PARENT_RIGHT, ALIGN_PARENT_TOP, and ALIGN_PARENT_BOTTOM. Click here for more properties.
You can manually calculate visible width and height of the image by using its drawable's getIntrinsicHeight() and getIntrinsicWidth() and then set Image's scaleType to FIT_XY (this saves image's ratio, and also makes its size correct, while FIT_CENTER does not).