Reputation: 1257
i'd like to move a view (in my case - a button, but it doesn't really matter) to the center of the screen. when i'm running the move without animations, it works. however, i'm having a hard time deciding which animations i should use to do so, since TranslateAnimation only lets me do so but specify the exact x,y coordinates, which i don't have (tried it with metrics, but the calculation didn't work, and i don't want to use this method).
So, this code works without animations :
RelativeLayout.LayoutParams parms2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
parms2.addRule(RelativeLayout.CENTER_VERTICAL);
parms2.addRule(RelativeLayout.CENTER_HORIZONTAL);
_buttonC.setLayoutParams(parms2);
and when i try to add animations, it doesn't.
ObjectAnimator ani = ObjectAnimator.ofFloat(_buttonC, "Y", 2000);
ani.setDuration(4000);
ani.addListener(new AnimatorListenerAdapter()
{
@Override
public void onAnimationStart(Animator animation) {
RelativeLayout.LayoutParams parms2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
parms2.addRule(RelativeLayout.CENTER_VERTICAL);
parms2.addRule(RelativeLayout.CENTER_HORIZONTAL);
_buttonC.setLayoutParams(parms2);
}
@Override
public void onAnimationEnd(Animator animation)
{
}
});
any help will be greatly appreciated :)
Upvotes: 10
Views: 12475
Reputation: 173
First. You need get screen coordinates(class Point). From onCreate launch this method.
fun getWindowSize():Point{
val display = windowManager.defaultDisplay
val size = Point()
display.getSize(size)
val width: Int = size.x
val height: Int = size.y
return size
}
Second. You need:
calculate where is centre
calculate size of a view that you want to move and divide it to 2
subtract from center half the moving view size
use ViewPropertyAnimator to move the view
protected fun toCentre(view: View){
//get centre
val windowXcentre = size.x / 2
val windowYcentre = size.y / 2
//half size of moving view
val viewWidth = view.width /2
val viewHeight = view.height/2
//wher to move
val x = windowXcentre - viewWidth.toFloat()
val y = windowYcentre - viewHeight.toFloat()
//move it
view.animate().x(x).y((y))
}
Upvotes: 0
Reputation: 5826
private void moveSearchViewDown() { View edit_text = findViewById(R.id.edit_text);
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
int mid = metrics.heightPixels/2 - edit_text.getHeight();
ObjectAnimator positionAnimation = ObjectAnimator.ofFloat(edit_text, View.Y, mid);
positionAnimation.start();
}
Upvotes: -1
Reputation: 2295
Have you tried:
_buttonC.animate().translationX(parentCenterX - _buttonC.getWidth()/2).translationY(parentCenterY - _buttonC.getHeight()/2);
Of course you will have to calculate the parent centerX and centerY:
float parentCenterX = parent.getX() + parent.getWidth()/2;
float parentCenterY = parent.getY() + parent.getHeight()/2;
Hope this will help.
Upvotes: 17
Reputation: 3789
I don't know if this is the problem but please try:
ObjectAnimator ani = ObjectAnimator.ofFloat(_buttonC, "translationY", 2000);
...
ani.start();
Upvotes: -1