TweenAnimation 渐变动画

TweenAnimation

TweenAnimation 又称为(View Animation)是单个对象的动画。

主要由以下几种:

  • AlphaAnimation 渐变动画
  • RotateAnimation 翻转动画
  • ScaleAnimation 缩放动画
  • TranslateAnimation 位移动画

Tween 动画可以通过 xml 编码的方式来实现。

Xml 动画

  • res/anim 文件夹下面建立tween animation xml文件

    可以有 set alpha rotate translate scale 几种方式来作为xml文件的根标签,分别对应Animation对象的几个子类AlphaAnimation, AnimationSet, RotateAnimation, ScaleAnimation, TranslateAnimation

  • 示例

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fillAfter="true"
    android:fromAlpha="1.0"
    android:repeatCount="3"
    android:toAlpha="0.1" >

</alpha>

代码实现

其实Tween 动画 最重要就是一种或几种渐变动画的组合,动画 无外乎

  • duration(持续时间)
  • 初始状态
  • 结束状态
  • 循环次数
  • 结束时显示初始态还是结束态
  • 等等属性

得到Animation对象,传入View.startAnimation()方法,为View设置动画效果!如果要设置延时,只需要 Animation.setStartTime(), 然后传入 View.setAnimation().即可

  • 实例代码
package com.anim.tweenanimation;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
    private Button btnAlpha;
    private Button btnRotate;
    private Button btnScale;
    private Button btnTranslate;

    private ImageView image;

    /**
     * 定义枚举,表示tween动画的四种变换
     * 
     * @author QT
     * 
     */
    enum AnimationType {
        alpha, rotate, scale, translate, group

    }

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

        // 得到对象
        image = (ImageView) findViewById(R.id.imageView1);
        btnAlpha = (Button) findViewById(R.id.alpha);
        btnRotate = (Button) findViewById(R.id.ratate);
        btnScale = (Button) findViewById(R.id.scale);
        btnTranslate = (Button) findViewById(R.id.translate);

        // 分别设置监听事件
        btnAlpha.setOnClickListener(new AnimationClickListenr(
                AnimationType.alpha));

        btnRotate.setOnClickListener(new AnimationClickListenr(
                AnimationType.rotate));

        btnScale.setOnClickListener(new AnimationClickListenr(
                AnimationType.scale));

        btnTranslate.setOnClickListener(new AnimationClickListenr(
                AnimationType.translate));

    }

    /**
     * 重写点击事件,传入 变换类型
     * 
     * @author QT
     * 
     */
    public class AnimationClickListenr implements View.OnClickListener {
        private AnimationType type;

        public AnimationClickListenr(AnimationType animationType) {
            this.type = animationType;
        }

        @Override
        public void onClick(View view) {
            switch (type) {
            case alpha:
                // 初始透明度 最终透明度
                // AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,
                // 0.2f);
                // 设置持续时间
                // alphaAnimation.setDuration(3000);
                // 启动动画
                // image.startAnimation(alphaAnimation);

                /*----**************加载xml文件中定义的动画*************----*/

                Animation animation2 = AnimationUtils.loadAnimation(
                        MainActivity.this, R.anim.alpha_anim);
                image.startAnimation(animation2);

                break;

            case rotate:
                // 反转 初始弧度 结束弧度
                // x pivot 有三个取值 ABSOLUTE RELATIVE_TO_PARENT(父元素)
                // RELATIVE_TO_SELF(本身)
                // x 轴 y轴 取值 面向 0-1 百分比
                RotateAnimation rotateAnimation = new RotateAnimation(0f, 360f,
                        Animation.RELATIVE_TO_SELF, 0.5f,
                        Animation.RELATIVE_TO_SELF, 0.5f);

                rotateAnimation.setDuration(3000);
                image.startAnimation(rotateAnimation);

                break;
            case scale:
                // 缩放变换也需要轴
                ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 2.0f,
                        1.0f, 2.0f, Animation.RELATIVE_TO_SELF, 1.0f,
                        Animation.RELATIVE_TO_SELF, 1.0f);
                scaleAnimation.setDuration(3000);
                image.startAnimation(scaleAnimation);

                break;
            case translate:
                TranslateAnimation translateAnimation = new TranslateAnimation(
                        Animation.RELATIVE_TO_SELF, 1.0f,
                        Animation.RELATIVE_TO_SELF, 1.0f,
                        Animation.RELATIVE_TO_SELF, 1.0f,
                        Animation.RELATIVE_TO_SELF, 1.0f);
                translateAnimation.setDuration(3000);
                image.startAnimation(translateAnimation);
                break;
            default:
                break;
            }

        }

    }

    @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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

运行结果

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值