平行翻译和缩放动画

时间:2013-12-02 03:14:03

标签: android parallel-processing scale translate-animation

我想将一些视图从任何位置移动到屏幕的中心并且并行缩放。如果这太复杂,那么顺序翻译比例也是可以接受的。但我无法实现。我认为这是枢轴点的问题。但抱歉,我没有找到解决办法。请帮忙。对于那些开发游戏的人来说,这可能很容易,我什么也没做。

以下是我的代码:

private void moveViewToScreenCenter( final View view ){
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics( dm );
    int statusBarOffset = dm.heightPixels - rootLayout.getMeasuredHeight();

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

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

    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);
}

更新 上面的代码可以处理来自左上角区域的图像视图,来自右下角区域的代码可以对角地穿过屏幕并消失。

2 个答案:

答案 0 :(得分:21)

经过几次无望的试验,我做到了,这太令人惊讶了。

下面是代码,它可用于将布局中的任何视图移动到该布局的中心:

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);
}

答案 1 :(得分:0)

核心思想是:

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);