欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Animation

程序员文章站 2024-01-19 13:47:58
...

来自Sun
首先呢 你的布局得整的明明白白的啊

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











Class页面实现方法
package com.example.j.animation;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView mText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mText = (ImageView) findViewById(R.id.tv_text);
    findViewById(R.id.btn_alpha).setOnClickListener(this);
    findViewById(R.id.btn_translation).setOnClickListener(this);
    findViewById(R.id.btn_rotation).setOnClickListener(this);
    findViewById(R.id.btn_scale).setOnClickListener(this);
    findViewById(R.id.btn_all).setOnClickListener(this);
    mText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "---", Toast.LENGTH_LONG).show();
        }
    });
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btn_alpha://实现透明度
            ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mText, "alpha", 1, 0.8f, 0.6f, 0.2f, 0, 0.2f, 0.5f, 1);
            objectAnimator.setDuration(6000);
            objectAnimator.start();
            break;
        case R.id.btn_translation://平移
            //translationY Y轴平移 translationX  X轴平移
            ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(mText, "translationY", 0, 100, 300, 500, 300, 0);
            objectAnimator1.setDuration(2000);

// objectAnimator1.setRepeatCount(10); 重复平移的次数
objectAnimator1.start();
break;
case R.id.btn_rotation://旋转
ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(mText, “rotation”, 0f, 360f);
objectAnimator2.setDuration(3000);
objectAnimator2.start();
break;
case R.id.btn_scale://缩放
ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(mText, “scaleY”, 1f, 8f, 1f);
ObjectAnimator objectAnimatorY = ObjectAnimator.ofFloat(mText, “scaleX”, 1f, 5f, 1f);
objectAnimator3.setDuration(2000);
objectAnimatorY.setDuration(3000);
objectAnimator3.start();
objectAnimatorY.start();
break;
case R.id.btn_all://组合
int width = getWindowManager().getDefaultDisplay().getWidth();
int height = getWindowManager().getDefaultDisplay().getHeight();

            ObjectAnimator objectAnimator5 = ObjectAnimator.ofFloat(mText, "translationX", 0, width - dip2px(50));
            ObjectAnimator objectAnimator5_Y = ObjectAnimator.ofFloat(mText, "translationY", 0, height - dip2px(175));
            ObjectAnimator objectAnimator8 = ObjectAnimator.ofInt(mText, "backgroundColor", Color.RED, Color.YELLOW, Color.BLACK, Color.BLUE);
            //after 先执行哪个
            //before 后执行哪个
            //with 同时执行
            AnimatorSet animatorSet = new AnimatorSet();
            // animatorSet.playSequentially(objectAnimator5,objectAnimator5_Y);
            animatorSet.play(objectAnimator5).with(objectAnimator5_Y).with(objectAnimator8);
            animatorSet.setDuration(3000);

            animatorSet.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    Toast.makeText(MainActivity.this, "END", Toast.LENGTH_LONG).show();

                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });

            animatorSet.start();

            break;

    }
}

public int dip2px(float dpValue) {
    final float scale = getResources().getDisplayMetrics().density;
    return (int) (dpValue * scale + 0.5f);
}

}

相关标签: Animation动画