添加按钮翻转动画 - Android

时间:2016-04-14 10:24:00

标签: android android-animation imagebutton

我有一个使用相机的应用

有一个选项允许切换相机, 当点击该按钮时,我想翻转按钮并使其恢复到原始状态,我似乎无法找到解决方案或这样做。

有什么建议吗?

3 个答案:

答案 0 :(得分:1)

使用ImageView

中的比例属性

单击该按钮时,如果要翻转按钮并使其返回原始状态

android:scaleX="-1" //To flip horizontally 
android:scaleY="-1" //To flip vertically

反之亦然

示例:

float scalingFactor = 0.5f; // scale down to half the size
view.setScaleX(scalingFactor);
view.setScaleY(scalingFactor);

答案 1 :(得分:0)

Android开发人员指南中存在的示例:
https://developer.android.com/training/animation/cardflip.html
https://developer.android.com/training/animation/anim_card_flip.mp4

使用动画到单个视图(不是所有屏幕)使用View.setAnimation()

Animation a = AnimationUtils.loadAnimation(this,R.anim.card_flip_left_in); 
myView.setAnimation(a);
a.startNow();

创建动画xmls:

<强> card_flip_left_in.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Before rotating, immediately set the alpha to 0. -->
    <objectAnimator
        android:valueFrom="1.0"
        android:valueTo="0.0"
        android:propertyName="alpha"
        android:duration="0" />

    <!-- Rotate. -->
    <objectAnimator
        android:valueFrom="-180"
        android:valueTo="0"
        android:propertyName="rotationY"
        android:interpolator="@android:interpolator/accelerate_decelerate"
        android:duration="@integer/card_flip_time_full" />

    <!-- Half-way through the rotation (see startOffset), set the alpha to 1. -->
    <objectAnimator
        android:valueFrom="0.0"
        android:valueTo="1.0"
        android:propertyName="alpha"
        android:startOffset="@integer/card_flip_time_half"
        android:duration="1" />
</set>

<强> card_flip_left_out.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Rotate. -->
    <objectAnimator
        android:valueFrom="0"
        android:valueTo="180"
        android:propertyName="rotationY"
        android:interpolator="@android:interpolator/accelerate_decelerate"
        android:duration="@integer/card_flip_time_full" />

    <!-- Half-way through the rotation (see startOffset), set the alpha to 0. -->
    <objectAnimator
        android:valueFrom="1.0"
        android:valueTo="0.0"
        android:propertyName="alpha"
        android:startOffset="@integer/card_flip_time_half"
        android:duration="1" />
</set>

<强> card_flip_right_in.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Before rotating, immediately set the alpha to 0. -->
    <objectAnimator
        android:valueFrom="1.0"
        android:valueTo="0.0"
        android:propertyName="alpha"
        android:duration="0" />

    <!-- Rotate. -->
    <objectAnimator
        android:valueFrom="180"
        android:valueTo="0"
        android:propertyName="rotationY"
        android:interpolator="@android:interpolator/accelerate_decelerate"
        android:duration="@integer/card_flip_time_full" />

    <!-- Half-way through the rotation (see startOffset), set the alpha to 1. -->
    <objectAnimator
        android:valueFrom="0.0"
        android:valueTo="1.0"
        android:propertyName="alpha"
        android:startOffset="@integer/card_flip_time_half"
        android:duration="1" />
</set>

<强> card_flip_right_out.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Rotate. -->
    <objectAnimator
        android:valueFrom="0"
        android:valueTo="-180"
        android:propertyName="rotationY"
        android:interpolator="@android:interpolator/accelerate_decelerate"
        android:duration="@integer/card_flip_time_full" />

    <!-- Half-way through the rotation (see startOffset), set the alpha to 0. -->
    <objectAnimator
        android:valueFrom="1.0"
        android:valueTo="0.0"
        android:propertyName="alpha"
        android:startOffset="@integer/card_flip_time_half"
        android:duration="1" />
</set>

答案 2 :(得分:0)

像这样使用ObjectAnimator:

    cameraSwitchButton.setImageResource(icon1ResourceId);

    cameraSwitchButton.setOnClickListener({                          
            ObjectAnimator animator = ObjectAnimator.ofFloat(cameraSwitchButton, "scaleX", 0.0f).setDuration(100);
            animator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animator) {
                    cameraSwitchButton.setImageResource(icon2ResourceId);
                    ObjectAnimator.ofFloat(cameraSwitchButton, "scaleX", 1.0f).setDuration(100).start();
                }
            });
            animator.start(); 

            // Camera switch logic here...
    });

如果您想将其翻转,请确定这些行所需的图标:

cameraSwitchButton.setImageResource(icon(1 or 2)ResourceId);