Translate and Scale animation in parallel

核能气质少年 提交于 2019-11-29 01:41:34

After quite a few hopeless trials, I made it, which is too astonishing.

Below is the code, it can be used to move any view in a layout to the center of that layout:

private void moveViewToScreenCenter( final View view ){
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics( dm );

    int originalPos[] = new int[2];
    view.getLocationOnScreen( originalPos );

    int xDelta = (dm.widthPixels - view.getMeasuredWidth() - originalPos[0])/2;
    int yDelta = (dm.heightPixels - view.getMeasuredHeight() - originalPos[1])/2;

    AnimationSet animSet = new AnimationSet(true);
    animSet.setFillAfter(true);
    animSet.setDuration(1000);
    animSet.setInterpolator(new BounceInterpolator());
    TranslateAnimation translate = new TranslateAnimation( 0, xDelta , 0, yDelta);
    animSet.addAnimation(translate);
    ScaleAnimation scale = new ScaleAnimation(1f, 2f, 1f, 2f, ScaleAnimation.RELATIVE_TO_PARENT, .5f, ScaleAnimation.RELATIVE_TO_PARENT, .5f);
    animSet.addAnimation(scale);
    view.startAnimation(animSet);
}

The core idea is:

AnimationSet animationSet = new AnimationSet(true);

animationSet.addAnimation(
  new TranslateAnimation(0f, 64f, 0f, 32f),
  new ScaleAnimation(1f, 0f, 1f, 0f, ScaleAnimation.RELATIVE_TO_PARENT, 0f, ScaleAnimation.RELATIVE_TO_PARENT, 0f););

myView.startAnimation(
  animationSet);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!