Android Animation之补间动画

Android Animation(一)

 Android提供两种机制来实现简单的动画效果,【补间动画】和【逐帧动画】。在补间动画中可以使用一系列简单的转换以达到位置、旋转、大小等动画状态的改变。
这里主要先记录和学习补间动画。Github源代码地址

知识点:
控制的状态: alpaha【透明度】、scale【缩放】、translate【移动】、rotate【旋转】
共同的属性: duration【持续时间】、repeatCount【循环次数】、repeatMode【循环类型】、fillAfter【停止后状态】

代码:
MainActivity:
package com.weizhengzhou.androidsdfjdsstudy.animation_demo.view;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.ImageView;

import com.weizhengzhou.androidsdfjdsstudy.R;

public class AnimationActivity extends AppCompatActivity implements View.OnClickListener {
    private Animation mAnimation;

    private android.widget.Button alphabn;
    private android.widget.Button scalebn;
    private android.widget.Button translatebn;
    private android.widget.Button rotatebn;
    private android.widget.ImageView testanim;
    private Button setanimbn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animation);
        this.setanimbn = (Button) findViewById(R.id.set_anim_bn);
        this.testanim = (ImageView) findViewById(R.id.test_anim);
        this.rotatebn = (Button) findViewById(R.id.rotate_bn);
        this.translatebn = (Button) findViewById(R.id.translate_bn);
        this.scalebn = (Button) findViewById(R.id.scale_bn);
        this.alphabn = (Button) findViewById(R.id.alpha_bn);

        alphabn.setOnClickListener(this);
        scalebn.setOnClickListener(this);
        translatebn.setOnClickListener(this);
        rotatebn.setOnClickListener(this);
        setanimbn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.alpha_bn:
                //mAnimation = AnimationUtils.loadAnimation(this , R.anim.alpha_anim);
                mAnimation = new AlphaAnimation(1.0f, 0.1f);
                mAnimation = getInstanceAnimation(mAnimation, 1000, 5, AlphaAnimation.REVERSE, true);
                testanim.startAnimation(mAnimation);
                mAnimation.reset();
                break;
            case R.id.scale_bn:
                //mAnimation = AnimationUtils.loadAnimation(this , R.anim.scale_anim);
                /**
                 * pivotXType : 缩放类型
                 * 有Animation.ABSOLUTE 绝对值
                 * Animation.RELATIVE_TO_SELF
                 * Animation.RELATIVE_TO_PARENT
                 */
                mAnimation = new ScaleAnimation(1.0f, 0.1f, 1.0f, 0.1f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
                mAnimation = getInstanceAnimation(mAnimation, 1000, 5, ScaleAnimation.REVERSE, true);
                testanim.startAnimation(mAnimation);
                mAnimation.reset();
                break;
            case R.id.translate_bn:
                mAnimation = AnimationUtils.loadAnimation(this, R.anim.translate_anim);
                //mAnimation = new TranslateAnimation(1 , 0.0f , 1 , -1.0f , 1 , 0.0f , 1 , 0.0f);
                //mAnimation = getInstanceAnimation(mAnimation , 1000 , 5 , TranslateAnimation.REVERSE , true);
                testanim.startAnimation(mAnimation);
                mAnimation.reset();
                break;
            case R.id.rotate_bn:
                mAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);
                // mAnimation = new RotateAnimation(0 ,720 , 1 , 0.5f , 1 , 0.5f);
                // mAnimation = getInstanceAnimation(mAnimation , 1500 , 5 , RotateAnimation.REVERSE , true);
                testanim.startAnimation(mAnimation);
                mAnimation.reset();
                break;
            case R.id.set_anim_bn:
                mAnimation = AnimationUtils.loadAnimation(this, R.anim.map_anim);
                testanim.startAnimation(mAnimation);
                mAnimation.reset();
                break;
        }
    }

    /**
     * @param animation   动画
     * @param duration    时间
     * @param repeatCount 循环次数
     * @param repeatMode  循环方式
     * @param fillAfter   状态
     * @return
     */
    public static Animation getInstanceAnimation(Animation animation, int duration, int repeatCount, int repeatMode, boolean fillAfter) {
        animation.setFillAfter(fillAfter);
        animation.setRepeatCount(repeatCount);
        animation.setRepeatMode(repeatMode);
        animation.setDuration(duration);
        return animation;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值