Android 补间动画,具体介绍了透明度、平移,旋转,缩放4种动画效果的实现,简单易懂

本文介绍了一款Android应用中视图动画的实现方法,包括透明度、平移、旋转及缩放等基本动画效果,并详细解释了每个动画的参数设置及其效果。
package com.pk.android_tweenanimation;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

   //声明控件
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
       imageView = (ImageView)findViewById(R.id.imageView);
    }

//下面为四个简单的Button点击事件

 //透明度
    public void alpha(View view) {
        /**
         * 取值范围0-1  0:完全透明   1:完全不透明
         * 参数1:起始透明度
         * 参数2:结束透明度
         */
        AlphaAnimation an = new AlphaAnimation(1.0f,0.5f);
        //动画持续的时间:毫秒为单位
        an.setDuration(2000);
        //动画持续的动作  Animation.INFINITE永远执行
        an.setRepeatCount(2);
        //动画的重复模式  Animation.RESTART:重新开始    Animation.REVERSE:折返
        an.setRepeatMode(Animation.RESTART);
        //设置保存动画最后的状态
        an.setFillAfter(true);
        //开始动画
        imageView.startAnimation(an);
    }

    //平移
    public void translate(View view) {
        /**
         * 参数1:fromXDelta:动画开始时x轴的起始点,以像素为单位
         * 参数2:toXDelta:动画结束时x轴的结束点
         * 参3:fromYDelta:
         * 参数4:toYDelta:
         */
       TranslateAnimation an = new TranslateAnimation(0f,100f,0f,100f);

    //旋转
    public void rotate(View view) {

        /**
         * 参1:fromDegress:起始角度
         * 参2:toDegerss:结束角度
         */
        RotateAnimation animation = new RotateAnimation(0,-45);

        /**
         * 参1:fromDegress:起始角度
         * 参2:toDegerss:结束角度
         * 参2:pivotX:动画开始到结束x轴变化的坐标
         * 参3:pivotY:动画开始到结束Y轴变化的坐标
       */
        RotateAnimation animation = new RotateAnimation(0,45,100,100);

        /**
         * 参1:fromDegress:起始角度
         * 参2:toDegerss:结束角度
         * 参3:pivotXType:pivotXValue的值的参考类型
         *        Animation.ABSOLUTE:绝对类型
         *        Animation.RELATIVE_TO_SELF:相对于自己
         *        Animation.RELATIVE_TO_PARENT:相对于父布局
         * 参4:pivotXValue:动画开始到结束x轴变化的坐标
         * 参5:pivotYType:pivotYValue的值的参考类型
         * 参6:pivotYValue:动画开始到结束Y轴变化的坐标
         */
        RotateAnimation animation = new RotateAnimation(0f,360f,Animation.RELATIVE_TO_PARENT,2.0f,Animation.RELATIVE_TO_PARENT,0f);
        animation.setDuration(2000);
        animation.setRepeatCount(2);
        animation.setRepeatMode(Animation.RESTART);
        imageView.startAnimation(animation);
    }

    //缩放
    public void scale(View view) {
        /**
         * 参1:fromX:x轴起始位置
         * 参2:toX:结束时x轴位置
         * 参3:fromY:
         * 参4:toY:
         */
        ScaleAnimation an = new ScaleAnimation(1f,2f,1f,4f);
        an.setDuration(2000);
        an.setRepeatCount(2);
        imageView.startAnimation(an);
    } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奋斗年轻人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值