Android动画 - 翻转

时间:2010-07-29 15:09:50

标签: android animation flip

我需要创建一个动画 - 翻转视图并显示另一个视图。

当前显示的视图的宽度缓慢减小到零,之后,要显示的视图的宽度必须从零增加。

在此期间,高度从当前显示的高度变为略微减小的高度,然后再返回。

如何使用ViewFlipper实现此目的。

4 个答案:

答案 0 :(得分:43)

您可以在ScaleAnimations上设置ViewFlipper时执行此操作。没有第二个比例,我做了类似的事情。我有两个动画,一个用于视图输出,另一个用于视图。我将在此处发布它们作为起点。

<强> shrink_to_middle.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/linear_interpolator"
        android:fromXScale="1.0"
        android:toXScale="1.0"
        android:fromYScale="1.0"
        android:toYScale="0.0"
        android:fillAfter="false"
        android:duration="200" />
    <translate
        android:fromYDelta="0"
        android:toYDelta="50%"
        android:duration="200"/>
</set>

<强> grow_from_middle.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/linear_interpolator"
        android:fromXScale="1.0"
        android:toXScale="1.0"
        android:fromYScale="0.0"
        android:toYScale="1.0"
        android:fillAfter="false"
        android:startOffset="200"
        android:duration="200" />
    <translate
        android:fromYDelta="50%"
        android:toYDelta="0"
        android:startOffset="200"
        android:duration="200"/>
</set>

然后在应用中,我将它们设置为ViewFlipper,如下所示:

mViewFlipper.setInAnimation(context, R.anim.grow_from_middle);
mViewFlipper.setOutAnimation(context, R.anim.shrink_to_middle);

就像我说的那样,这并不是你所描述的,但它非常接近并且会让你开始。

- 编辑 -

以下是使用pivotX和pivotY的代码(好吧,在我的情况下只是pivotY):

<强> shrink_to_middle.xml

<?xml version="1.0" encoding="utf-8"?>
<scale
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:fromXScale="1.0"
    android:toXScale="1.0"
    android:fromYScale="1.0"
    android:toYScale="0.0"
    android:pivotY="50%"
    android:fillAfter="false"
    android:duration="200" />

<强> grow_from_middle.xml

<?xml version="1.0" encoding="utf-8"?>
<scale
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:fromXScale="1.0"
    android:toXScale="1.0"
    android:fromYScale="0.0"
    android:toYScale="1.0"
    android:pivotY="50%"
    android:fillAfter="false"
    android:startOffset="200"
    android:duration="200" />

答案 1 :(得分:3)

只是为了通知我已经开发了一个新的库FlipView,其中包含并扩展了CaseyB描述的特定动画(翻转)。我的意思是一个完全可自定义的库,您可以使用任何类型的动画和形状交换任何类型的视图和布局,包括Gmail图像翻转。

请看一下。

答案 2 :(得分:2)

使用CaseyB使用objectAnimator回答的缩放动画。如果你没有res下的animator文件夹,创建一个,它需要objectAnimator布局驻留在这个动画制作者中。

<强> RES /动画/ shrink_to_middle.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:valueFrom="1.0"
        android:valueTo="0.0"
        android:propertyName="scaleX"
        android:duration="200"/>
</set>

<强> RES /动画/ grow_from_middle.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:valueFrom="0.0"
        android:valueTo="1.0"
        android:propertyName="scaleX"
        android:duration="200"
        android:startOffset="200"/>
</set>

代码:

ImageView iv = (ImageView) findViewById(R.id.my_image);
AnimatorSet shrinkSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.shrink_to_middle);
shrinkSet.setTarget(iv);
shrinkSet.start();

iv.setImageResource(R.drawable.another_image);

AnimatorSet growSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.grow_from_middle);
growSet.setTarget(iv);
growSet.start();

答案 3 :(得分:0)

我知道这是一个老问题,但是以上都不对我有用。我是这样做的。

作为参考,我翻转的图像是纸牌。卡的正面正在显示,我想翻转卡并显示背面。

long duration = getResources().getInteger(R.integer.flip_animation_length).toLong()

// cardDisplay was previously defined as an ImageView in this example
// Set animation and start the animation
cardDisplay.animate().rotationY(180f).setDuration(duration).start()

// wait until the animation is half over
Handler().postDelayed({
    // Change the image at the half-way point
    cardDisplay.setImageDrawable(getResources().getDrawabe(R.drawable.card_back))
},duration/2)

这是做什么的?它是在Y轴上(水平)翻转图像,并在翻转过程中半途更改图像-当卡片在侧面时-这样,当卡片的另一侧开始显示时,新图像(卡片背面设计)开始出现。

我将其放慢了5秒(持续时间= 5000),以确保在整个过程中看起来都是正确的。

相关问题