旋转动画无法正常工作

时间:2013-11-25 12:22:28

标签: java android xml rotation android-animation

我需要为TextView实现'旋转'动画。以下是代码。

MainActivity.java

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView words;
    private Map<String, String> wordsMap;
    private Animation rotate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        words = (TextView)findViewById(R.id.word);

        rotate = AnimationUtils.loadAnimation(this, R.anim.rotate);

        wordsMap = new HashMap<String, String>();
        wordsMap.put("Dog", "Perro");
        wordsMap.put("Cat", "Gato");
        wordsMap.put("Universe", "Universo");
        wordsMap.put("Telephone", "Teléfono");
        wordsMap.put("Key Board", "Teclado Del");
        wordsMap.put("Country", "País");

        //Registering Listeners
        words.setOnClickListener(new TextClicked());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private OnTouchListener textViewSwiped = new OnSwipeTouchListener()
    {
        public boolean onSwipeLeft() 
         {
            return true;
         }
    };

    private class TextClicked implements OnClickListener
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            words.setAnimation(rotate);
                    words.setText("Rotated");

        }

    }

}

rotate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="600"
        android:interpolator="@android:anim/cycle_interpolator"/>

</set>

当我点击TextView时,它不起作用。除此之外,我需要的旋转动画与我在这里所做的有点不同。这种旋转就像一个圆圈。我需要的是颠倒并显示新文本的东西。例如,您可以翻转一张游戏卡中的卡片以显示另一侧的卡片吗?我需要同样的东西。

更新

我不是在寻求90度旋转。这应该就像你可以“翻转”一张卡片(我正在谈论卡片组中的真卡片)到另一边。

6 个答案:

答案 0 :(得分:1)

“我需要的是颠倒过来并显示新文字......”

为此,您需要使用API​​ 11支持的ObjectAnimator,您可以从here为此下载supprort库。

并查看沿Y轴旋转TextView的示例代码...

    ObjectAnimator animator = ObjectAnimator.ofFloat(textView, "rotationY", 0,90,180,270,360);
    animator.setDuration(3000);
    animator.setInterpolator(new LinearInterpolator());
    animator.start();

答案 1 :(得分:1)

<?xml version="1.0" encoding="utf-8"?>
 <set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate android:fromDegrees="0"
        android:toDegrees="90" // changable
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="600"
        android:interpolator="@android:anim/cycle_interpolator"/>
</set>

答案 2 :(得分:1)

将您的xml代码更改为

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<rotate
    android:duration="1000"
    android:fromDegrees="0"
     android:toDegrees="90"
    android:interpolator="@android:anim/cycle_interpolator"
    android:pivotX="50%"
    android:pivotY="50%" />

</set>

并在textview上单击开始动画

words.startAnimation(rotate);
words.setText("Rotated");

onCreate()

中的动画监听器
rotate.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            // TODO Auto-generated method stub
            words.setText("Rotated");
        }
    });

答案 3 :(得分:0)

我找到了答案。在这里。

<?xml version="1.0" encoding="utf-8"?>

<scale

    xmlns:android="http://schemas.android.com/apk/res/android"

       android:fromXScale="1.0" android:toXScale="0.0"

       android:pivotX="50%"

       android:fromYScale="1.0" android:toYScale="1.0"

       android:pivotY="50%"

       android:duration="250" />

答案 4 :(得分:0)

试试这个:

public class FlipAnimation extends Animation {
    private View view;
    private Drawable drawable;
    private boolean backSide;
    private Camera camera;

    public FlipAnimation(View v, Drawable d) {
        this.view = v;
        this.drawable = d; // this needs to be a mirrored Drawable
        this.camera = new Camera();
        this.camera.save();
        this.backSide = false;
        setDuration(3000);
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        float degrees = 180 * interpolatedTime;
        if (degrees > 90 && !backSide) {
            backSide = true;
            view.setBackgroundDrawable(drawable);
        }
        camera.restore();
        camera.save();
        camera.rotateX(degrees);
        Matrix m = t.getMatrix();
        camera.getMatrix(m);
        float w2 = view.getWidth() / 2f;
        float h2 = view.getHeight() / 2f;
        m.preTranslate(-w2, -h2);
        m.postTranslate(w2, h2);
    }
}

答案 5 :(得分:0)

在res中创建一个动画文件夹。并添加.xml文件,将其命名为from_middle并粘贴以下代码

 
<scale
    xmlns:android="http://schemas.android.com/apk/res/android"
       android:fromXScale="0.0" android:toXScale="1.0"
       android:pivotX="50%"
       android:fromYScale="1.0" android:toYScale="1.0"
       android:pivotY="50%"
       android:duration="250" />

创建另一个xml文件并将其命名为to_middle并粘贴以下内容:

<?xml version="1.0" encoding="utf-8"?>

<scale

    xmlns:android="http://schemas.android.com/apk/res/android"
       android:fromXScale="1.0" android:toXScale="0.0"
       android:pivotX="50%"
       android:fromYScale="1.0" android:toYScale="1.0"
       android:pivotY="50%"
       android:duration="250" />

在您的活动中:

将此代码粘贴到“创建:

    animation1 = AnimationUtils.loadAnimation(this, R.anim.to_middle);
    animation1.setAnimationListener(this);
    animation2 = AnimationUtils.loadAnimation(this, R.anim.from_middle);
    animation2.setAnimationListener(this);

和onAnimationEnd:

@Override
    public void onAnimationEnd(Animation animation) {
         if (animation==animation1) {
             if (isBackOfCardShowing) { // boolean variable
             mtextView1.setBackgroundResource(R.drawable.surfboard_memory);

               } else {
             mtextView1.setBackgroundResource(R.drawable.back_memory);// any pic
               }
             mtextView1.clearAnimation();
             mtextView1.setAnimation(animation2);
             mtextView1.startAnimation(animation2);
         } else {
                isBackOfCardShowing=!isBackOfCardShowing;
                mtextView1.setEnabled(true);
         }
    }

它确实有效并经过测试.. :)