Reputation: 899
I have a view which I want to move it with animation from out of screen to the bottom of my screen and reverse! I've tried this :
bottomRelative = findViewById(R.id.bottomRelative);
int widthOfScreen = this.getWindowManager().getDefaultDisplay().getHeight();
ObjectAnimator animator = ObjectAnimator.ofFloat(bottomRelative, "y", widthOfScreen, (widthOfScreen-700));
animator.setDuration(2000);
animator.start();
but I can't set a exact location to stop! I want this view to set with 50 margin from bottom!
how can I set the final place of my view to 50 margin of bottom of the screen?
another problem is, with different devices! with above code, the view animate to my screen in 2 devices! but in 4 inch device, is not working! how can I solve these 2 problem?
I think by setting the right destination for my view, the different devices issue will be solved.
Upvotes: 2
Views: 1000
Reputation: 3711
I have a solution, that is using 2 views, startView
and destinationView
, this way to move startView
to destinationView
Assume we have 2 views: startView
and destinationView
int[] startLoc = new int[2];
int[] desLoc = new int[2];
startView.getLocationOnScreen(startLoc);
destinationView.getLocationOnScreen(desLoc);
float x = desLoc[0]
- startLoc[0]
- (startView.getWidth() - destinationView.getWidth()) / 2;
float y = desLoc[1]
- startLoc[1]
- (startView.getHeight() - destinationView.getHeight()) / 2;
createMoveAndScaleThenFadeAnimation(
startView, x, y, 1.0f, 1, 1000, 300,
new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
}
}).start();
and createMoveAndScaleThenFadeAnimation()
method:
public AnimatorSet createMoveAndScaleThenFadeAnimation(View view, float deltaX,
float deltaY, float scaleFrom, float scaleTo, int duration, int offset,
@Nullable final AnimatorListenerAdapter listener) {
ObjectAnimator animX = ObjectAnimator.ofFloat(view, "translationX", deltaX);
ObjectAnimator animY = ObjectAnimator.ofFloat(view, "translationY", deltaY);
ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", scaleFrom, scaleTo);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", scaleFrom, scaleTo);
ObjectAnimator fade = ObjectAnimator.ofFloat(view, "alpha", 1.0f, 0.0f);
AnimatorSet animSet = new AnimatorSet();
animSet.setDuration(duration);
animSet.setStartDelay(offset);
animSet.play(animX).with(animY).with(scaleX).with(scaleY).before(fade);
if (listener != null) {
animSet.addListener(listener);
}
return animSet;
}
so now we don't care about device's screen size or any thing else, just define 2 view in layout and do this way. In your case, we can set view 2th visibility is INVISIBLE.
NOTE
Please make sure whole view has been inflated before do this way, if not, we can not get view's position.
Option 1: Using OnGlobalLayoutListener()
ViewTreeObserver vto=view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
@Override public void onGlobalLayout(){
//Here we can get the view's position and view size
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
option 2 override onWindowFocusChanged()
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
// Here we can get the view's position and view size
}
Thank @Azin Nilchi for the suggestion.
hope this helps!
Upvotes: 1