动画

package com.example.com.mydonghua;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button butAlpha;
    private Button butTranslationy;
    private Button butScalex;
    private Button butAnimatorset;
    private Button butAnimation;
    private ImageButton imgbut;

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


        getinit();

        Animator anim = AnimatorInflater.loadAnimator(this, R.animator.objectanimator);
        anim.setTarget(imgbut);
        anim.start();
    }

    private void getinit() {


        butAlpha = findViewById(R.id.but_alpha);
        butTranslationy = findViewById(R.id.but_translationy);
        butScalex = findViewById(R.id.but_scalex);
        butAnimatorset = findViewById(R.id.but_Animatorset);
        butAnimation = findViewById(R.id.but_animation);
        imgbut = findViewById(R.id.imgbut);

        //点击事件
        butAlpha.setOnClickListener(this);
        butTranslationy.setOnClickListener(this);
        butScalex.setOnClickListener(this);
        butAnimatorset.setOnClickListener(this);
        butAnimation.setOnClickListener(this);
        imgbut.setOnClickListener(this);

        imgbut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "葫芦娃,葫芦娃,一个头上一朵花", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.but_alpha:
                ObjectAnimator animator = ObjectAnimator.ofFloat(imgbut, "alpha", new float[]{0.0f, 0.2f, 0.5f, 0.6f, 1.0f});
                animator.setDuration(2000);
                animator.setRepeatMode(ObjectAnimator.RESTART);
                animator.setRepeatCount(1);
                animator.start();
                break;

            case R.id.but_translationy:
                ObjectAnimator rotationY = ObjectAnimator.ofFloat(imgbut, "rotationY", new float[]{90f, 180f, 250f, 360f, 1.0f});
                rotationY.setDuration(2000);
                rotationY.setRepeatMode(ObjectAnimator.RESTART);
                rotationY.setRepeatCount(1);
                rotationY.start();
                break;
            case R.id.but_scalex:
                ObjectAnimator scaleX = ObjectAnimator.ofFloat(imgbut, "scaleX", new float[]{90f, 180f, 250f, 360f,400f});
                scaleX.setDuration(2000);
                scaleX.setRepeatMode(ObjectAnimator.RESTART);
                scaleX.setRepeatCount(1);
                scaleX.start();
                break;
            case R.id.but_Animatorset:
                ObjectAnimator translationY = ObjectAnimator.ofFloat(imgbut, "translationY", new float[]{10f, 181f, 25f, 36f, 1.0f});
                translationY.setDuration(2000);
                translationY.setRepeatMode(ObjectAnimator.RESTART);
                translationY.setRepeatCount(1);
                translationY.start();
                break;
            case R.id.but_animation:
                AnimatorSet animatorSet = new AnimatorSet();
                ObjectAnimator rotationX = ObjectAnimator.ofFloat(imgbut, "rotationX", new float[]{90f, 180f, 250f, 360f, 1.0f});
                rotationX.setDuration(3000);
                ObjectAnimator translationX = ObjectAnimator.ofFloat(imgbut, "translationX", new float[]{10f, 181f, 25f, 36f, 1.0f});
                translationX.setDuration(3000);
                animatorSet.playTogether(rotationX,translationX);
                animatorSet.start();
                break;


        }
    }
}

——————————————————————————————————————————————————

package com.example.com.donghua;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_alpha;
    private Button btn_rotation;
    private Button btn_scale;
    private Button btn_translation;
    private ImageButton btn_img;
    private Button btn_translat;
    private ImageView tv_img;

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

    private void initView() {
        btn_alpha = (Button) findViewById(R.id.btn_alpha);
        btn_rotation = (Button) findViewById(R.id.btn_rotation);
        btn_scale = (Button) findViewById(R.id.btn_scale);
        btn_translation = (Button) findViewById(R.id.btn_translation);
        btn_img = (ImageButton) findViewById(R.id.btn_img);

        btn_alpha.setOnClickListener(this);
        btn_rotation.setOnClickListener(this);
        btn_scale.setOnClickListener(this);
        btn_translation.setOnClickListener(this);
        btn_img.setOnClickListener(this);
        btn_translat = (Button) findViewById(R.id.btn_translat);
        btn_translat.setOnClickListener(this);
        tv_img = (ImageView) findViewById(R.id.tv_img);
        tv_img.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            ///透明度动画
            case R.id.btn_alpha:
                ObjectAnimator alpha = ObjectAnimator.ofFloat(btn_img, "alpha", new float[]{0.0f, 0.2f, 0.5f, 0.8f, 1.0f});
                alpha.setDuration(10000);//设置动画时长
                alpha.setRepeatMode(ObjectAnimator.RESTART);//动画执行的模式
                alpha.setRepeatCount(2);//动画执行的次数
                alpha.start();//开启动画
                break;
            //旋转动画
            case R.id.btn_rotation:
                ObjectAnimator rotationY = ObjectAnimator.ofFloat(btn_img, "rotationY", 0f, 360f);
                rotationY.setDuration(5000);//设置动画时长
                rotationY.setRepeatMode(ObjectAnimator.RESTART);//动画执行的模式
                rotationY.setRepeatCount(2);//动画执行的次数
                rotationY.start();//开启动画
                break;
            //缩放动画
            case R.id.btn_scale:
                ObjectAnimator scaleX = ObjectAnimator.ofFloat(btn_img, "scaleX", 1f, 3f, 1f);
                scaleX.setDuration(5000);//设置动画时长
                scaleX.setRepeatMode(ObjectAnimator.RESTART);//动画执行的模式
                scaleX.setRepeatCount(2);//动画执行的次数
                scaleX.start();//开启动画
                break;
            //平移动画
            case R.id.btn_translation:
                float curTranslationX = btn_img.getTranslationX();
                ObjectAnimator translationX = ObjectAnimator.ofFloat(btn_img, "translationX", curTranslationX, -500, curTranslationX);
                translationX.setDuration(5000);//设置动画时长
                translationX.setRepeatMode(ObjectAnimator.RESTART);//动画执行的模式
                translationX.setRepeatCount(2);//动画执行的次数
                translationX.start();//开启动画
                break;
            //组合动画
            case R.id.btn_img:
                ObjectAnimator moveIn = ObjectAnimator.ofFloat(btn_img, "translationX", -500f, 0f);
                ObjectAnimator rotate = ObjectAnimator.ofFloat(btn_img, "rotation", 0f, 360f);
                ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(btn_img, "alpha", 1f, 0f, 1f);
                AnimatorSet animaSet = new AnimatorSet();
                animaSet.play(rotate).with(fadeInOut).after(moveIn);
                animaSet.setDuration(5000);
                animaSet.start();
                Toast.makeText(this, "啦啦啦,吐司啦", Toast.LENGTH_SHORT).show();
                break;
            //补间动画
            case R.id.btn_translat:
                TranslateAnimation translateAnimation = new TranslateAnimation(0,300,0,300);
                //指定动画的时长
                translateAnimation.setDuration(3000);
                //设置重复的次数....总共运行了重复次数+1
                translateAnimation.setRepeatCount(1);
                //设置动画结束以后保持最后的状态
                translateAnimation.setFillAfter(true);
                tv_img.setAnimation(translateAnimation);
                tv_img.startAnimation(translateAnimation);

                break;
            case R.id.tv_img:
                Toast.makeText(this, "您点击了动画!!!", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值