I have multiple views that are all selectable. When one of them gets selected I want my new view to be first positioned to the same location as the selected one and also move it to the top of my screen. How can I do that? The position of selected item varies.
// Selected item
int[] location = new int[2];
item.getLocationOnScreen(location);
int positionY = location[1];
// Move the view I want to move to the position of selected item
viewToMove.setTranslationY(positionY);
// Create and start animation
Animation slideUp = new TranslateAnimation(viewToMove.getTranslationX(), viewToMove.getTranslationX(), positionY, -positionY);
slideUp.setDuration(1000);
slideUp.setFillEnabled(true);
slideUp.setFillAfter(true);
viewToMove.startAnimation(slideUp);
With ViewPropertyAnimator its easy :
viewToMove.animate().translationX(...).translationY(0).setDuration(1000);
translationY(0) is the top of the Y axis, and
at translationX(...) you fill in the coordinates where you want the View to move to on the X axis.
Related
Is it somehow possible to get an item of a recyclerview at a specific screen position (x and y coordinate)?
You can get View of an item at specific coordinates with this method findChildViewUnder.
View itemView = recyclerView.findChildViewUnder(x, y);
I have to move from x to y point. There are 10 views horizontal.I want to move a imageView to move from x view to the y view.
TranslateAnimation animation = new TranslateAnimation(0, viewTwo.getX()-viewOne.getX(),0 , viewTwo.getY()-viewOne.getY());
animation.setRepeatMode(0);
animation.setDuration(3000);
animation.setFillAfter(true);
viewOne.startAnimation(animation);
but viewTwo.getX() and viewOne.getX() returns 0.0.
Try to look up measure() method to get X position value !
Like, measure the view until Y position, the view-width on the left of Y position would be the result what you wanted.
i would like solve this scenario:
pseudo:
for i = 0; i < 10; i++ {
view.setX(newXpos); //e.g. 100px
view.setY(newYpos); //e.g. 200px
save this position (100;200) on screen and apply FadeOut effect (500ms), but view have to ready for new iteration move to new position
}
The result: see short (500ms) history of View on X,Y coords.
I do not know, it's understandable?
I tried use TranslateAnimation() with setFillAfter(true), but this move with original View.
For Android 5+
Thank you.
I have one image of remote on screen,i want to when i click on any particular button at that time any particular event occurs,means image have some buttons when i exact touch on button held on in this image at that time occurs any events,i how to get position this button held on only image,and screen point x and y also change in different screen. i know getX() and `getY()' used to find touch position but how to get any particular position inside the image ,please help me quickly,thanks in advance.
see in the image ,when i touch on middle of image at that time any event ocurrs.
Try this:
anchorView.getLocationOnScreen(location);
location is an array:
int location[] = new int[2];
anchorView is your view.
So your function might look like:
public getLocation(View anchorView){
int location[] = new int[2];
anchorView.getLocationOnScreen(location);
}
And if you want to display a different view at that location just do:
yourView.showAtLocation(anchorView, Gravity.NO_GRAVITY,
location[0], location[1] + anchorView.getHeight());
Also please note that you can manipulate the values in location[0] and location[1]. Good Luck :)
Edit
Here is a complete example, I use it to display a fragment below a button, this process takes place as soon as the button is clicked:
public void showPopup(View anchorView) {
View popupView = getLayoutInflater().inflate(R.layout.popup_layout, null);
popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button proceed = (Button)popupView.findViewById(R.id.button1);
Button cancel = (Button)popupView.findViewById(R.id.button2);
cb = (CheckBox)popupView.findViewById(R.id.checkBox1);
proceed.setOnClickListener(onProceed);
cancel.setOnClickListener(onCan);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable());
int location[] = new int[2];
anchorView.getLocationOnScreen(location);
popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY,
location[0], location[1] + anchorView.getHeight());
}
The above will result in a popup, just beneath the view clicked.
popup_layout is the XML layout file which will be inflated.
Given an arbitrary ViewGroup G with an arbitrary collection of child views, how can I detect when the user clicks on any of the child views? In this case, I want to draw a highlight for G.
I could add an onClick listener for each child, but I'm trying to avoid that so that the code doesn't have to be changed when the layouts change.
Alternatively, I could add onTouch handlers to G and set the highlight during ACTION_DOWN. However, this would trigger for actions that don't actually result in clicks, such as a swipe (the swipe could be handled by ViewPager, for example, and ultimately be irrelevant to G).
My layout for G has the focusable attributes:
android:focusable="true"
android:focusableInTouchMode="true"
Thanks.
Here is how I do it:
//in onTouch method of parent, I get the coordinates of click
int x = ((int) motionEvent.getX());
int y = ((int) motionEvent.getY());
//obtain the clickable arrea of the child as HitRect
Rect clickRect = new Rect();
Rect rect = new Rect();
imageView.getHitRect(rect);
//ask if the area contains the coordinates of the click
if(rect.contains(x, y)){
//do some work like if onClickListener on the child was called.
return false; //you clicked here, don't need to handle other Childs
}
//ask for other childs like before...
Now, you can target the parent as the delegate of all clicks done inside it, even if it is done in a child.
EDIT:
To ignore other touch event that are not click, you can ask for how much user moved the finger:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_CANCEL:
if (Math.abs(motionEvent.getRawX() - initialTouchX) > 5 || Math.abs(motionEvent.getRawY() - initialTouchY) > 5) {
return true; // user mover finger too much, ignore touch
}
return false; // finger still there waiting for click
I give a square of 10 pixels to permit a confortable click, and if you exit it, I ignore it.
EXTRA:
Here is the complete code for click and long click with onTouchListener.
You could use the View.getChildCount() to loop through all child views and see if the touch intersects with the child view.
This involves getting x and y positions and calculating if it fits within the child view, use View.getChildAt(position) to get the reference to the child view .
So it would be something like this:
int childNr = theView.getChildCount();
for (int i = 0; i < childNr; i++){
YourView tmp = (YourView) theView.getChildAt(i);
if(tmp.intersects(x, y)){
do some work
}
}
here you would have to put your view variable instead of theView and the class name which handles the views instead of (YourView) and x, y are the coordinates of the pressed spot.
In your XML, you could add point all the children to the same onClick method. Inside that method you could draw the highlight to G and then do something (or nothing) for the individual child view.