There is an answer to the question of how to animate imageView so that from any position on the view screen moves to the center:

https://stackoverflow.com/questions/10276251/how-to-animate-a-view-with-translate-animation-in-android

Not strong in java, and I can't figure out what happens in the moveViewToScreenCenter method after this.getWindowManager().getDefaultDisplay().getMetrics( dm ); and before anim.setDuration(1000);

  • coordinates x and y are calculated where the view will be moved. Yours, cap =) - Lex Hobbit
  • Well, this is understandable, it is not clear how, a bunch of variables, it is difficult to understand. statusBarOffset is the difference between the width of the display and the width of the container. Next, originalPos is an array of numbers 0 and 1. I do not understand what it is for. What a method getLocationOnScreen (originalPos). - VolhaGomel

1 answer 1

Explanations to the code that you requested:

 private void moveViewToScreenCenter( View view ) { RelativeLayout root = (RelativeLayout) findViewById( R.id.rootLayout ); DisplayMetrics dm = new DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getMetrics( dm ); int statusBarOffset = dm.heightPixels - root.getMeasuredHeight();// размер экрана - высота rootLayout в пикселах(фактически высота statusBar) int originalPos[] = new int[2]; view.getLocationOnScreen( originalPos ); //позиция на экране view, x и y записываются в массив originalPos int xDest = dm.widthPixels/2; //середина экрана по х xDest -= (view.getMeasuredWidth()/2); //вычитаем половину ширины view int yDest = dm.heightPixels/2 - (view.getMeasuredHeight()/2) - statusBarOffset; //середина экрана по у вычитаем половину высоты view и вычитаем высоту statusBar TranslateAnimation anim = new TranslateAnimation( 0, xDest - originalPos[0] , 0, yDest - originalPos[1] );// анимация перемещения view, все параметры - дельты anim.setDuration(1000); anim.setFillAfter( true ); view.startAnimation(anim); } 
  • Thanks for the clarification, something already found in the documentation. Just could not understand why view, as a result, does not hit the center)). And about the last remark, so the author all the pictures, like, are shifted to the right from the center, and the animation goes from right to left. - VolhaGomel
  • @VolhaGomel, everything is right with him, I just did not take into account that the delta parameters indicate not the absolute values. I will correct - Lex Hobbit
  • Those. The delta is not the coordinates of the object, but the difference between its initial position and where the animation ends? In this case, xDest = is essentially the value of the left margin (left margin) view when it is in the center, and the originalPos [0] is the left margin value of the transferred view. In the second parameter, TranslateAnimation, the delta is transmitted, which is the difference between the value of the leftmargin view, if it were located in the center, and the left margin of the transmitted view. I understand correctly? - VolhaGomel
  • one
    @VolhaGomel got it right =) - Lex Hobbit