平移和缩放动画并行 [英] Translate and Scale animation in parallel

查看:181
本文介绍了平移和缩放动画并行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想移动从任意位置的一些观点为中心的屏幕和规模同步发展。如果这太复杂了,翻译比例顺序也是可以接受的。但无论是我能做到。我认为这是与支点的问题。但是,对不起,我没有找到解决办法。请帮忙。也许很容易给那些谁开发游戏,对此我什么也没做。

I want to move some view from any position to center of screen and scale in parallel. If that's too complicated, translate and scale sequentially is also acceptable. But neither I could achieve. I think it's the problem with the pivot point. But sorry I found no solution. Please help. Maybe it's easy to those who develop games, about which I did nothing.

下面是我的code:

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

更新: code以上可以从左上方区域处理ImageView的,那些来自右下区域走对角线通过屏幕消失。

Update: code above can handle imageview from top left region, those from bottom right region go diagonally through the screen and disappear.

推荐答案

不少绝望的试验后,我做到了,这实在太令人惊讶。

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

下面是code,它可以用来将任何视图中的布局的中心时,布局的:

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

这篇关于平移和缩放动画并行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆