2016-04-14 34 views
0

我有一個使用攝像頭的應用程序,添加按鈕翻轉動畫 - Android電子

有一個選項存在,它允許切換攝像頭, 當用戶點擊該按鈕,我想翻轉按鈕,併爲它回到原來的狀態,我似乎找不到解決辦法或這樣做。

有什麼建議嗎?在ImageView

當如果你想翻轉按鈕按鈕被點擊

+0

顯示一些代碼,更具體。你試過什麼了? –

回答

0

利用規模屬性和它返回到原來的狀態

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

這個答案對您有幫助嗎? @ 2D3D –

0

示例存在於And固醇開發人員指南:
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(); 

創建動畫個XML:

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> 
+0

你不明白我想達到什麼,只有按鈕不會翻轉整個屏幕 – 2D3D

+0

這不是必需的。動畫a = AnimationUtils.loadAnimation(this,R.anim.card_flip_left_in); myView.setAnimation(a); a.startNow(); –

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