Android Studio——飞入文字动画演示

目的

设计一个飞入文字的动画演示

工具及环境

使用java语言,在Android studio平台上进行开发

功能设计

实现属性动画移动效果,补间动画移动效果,可通过两种移动的效果分析两种动画的异同。同时将属性动画旋转属性和移动属性结合起来成为新的组合效果

设计思路

先设计界面视图和功能按钮,实现功能

代码

activity_main.xml




        

        

            

MainActivity.java

package com.example.firstapp;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    Button Btn1,Btn2,Btn3;
    private TextView txt;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt=(TextView) findViewById(R.id.textView2);
        Btn1 = (Button) findViewById(R.id.button1);
        Btn2 = (Button) findViewById(R.id.button2);
        Btn3 = (Button) findViewById(R.id.button3);
        Btn1.setOnClickListener(new mClick());
        Btn2.setOnClickListener(new mClick());
        Btn3.setOnClickListener(new mClick());
    }

    public class mClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            if (v == Btn1) {
                AnimationSet animationSet=new AnimationSet(true);
                TranslateAnimation translateAnimation=new TranslateAnimation(
                        Animation.RELATIVE_TO_SELF,0f,
                        Animation.RELATIVE_TO_SELF,0f,
                        Animation.RELATIVE_TO_SELF,0f,
                        Animation.RELATIVE_TO_SELF,5f);
                translateAnimation.setDuration(2000);
                animationSet.addAnimation(translateAnimation);
                txt.startAnimation(animationSet);
            }
            else if (v == Btn2) {
                ObjectAnimator translationX = ObjectAnimator.ofFloat(txt, "translationX",0,820f );
                translationX.setDuration(2000);
                translationX.start();

            }
            else if (v == Btn3) {
                ObjectAnimator translationX = ObjectAnimator.ofFloat(txt, "translationX",0,820f );
                ObjectAnimator ra = ObjectAnimator.ofFloat(txt, "rotation",0,720.0F );
                AnimatorSet animatorSet = new AnimatorSet();  //组合动画
                animatorSet.playTogether(translationX,ra); //设置动画
                animatorSet.setDuration(2000);
                animatorSet.start();
            }
        }
    }
}

界面效果:

Android Studio——飞入文字动画演示_第1张图片

你可能感兴趣的:(计算机)