Android 动画之补间动画

1.简介

和前面学的帧动画不同,帧动画是通过连续播放图片来模拟动画效果,而补间动画开发者只需指定动画开始,以及动画结束"关键帧", 而动画变化的"中间帧"则由系统计算并补齐。

 

 

 

2.补间动画的分类

Andoird所支持的补间动画效果有如下这五种,或者说四种吧,第五种是前面几种的组合。

 

2.1.AlphaAnimation:透明度渐变效果,创建时许指定开始以及结束透明度,还有动画的持续 时间,透明度的变化范围(0,1),0是完全透明,1是完全不透明;对应<alpha/>标签。

 

 

2.2.ScaleAnimation:缩放渐变效果,创建时需指定开始以及结束的缩放比,以及缩放参考点, 还有动画的持续时间;对应<scale/>标签。

 

 

2.3.TranslateAnimation:位移渐变效果,创建时指定起始以及结束位置,并指定动画的持续 时间即可;对应<translate/>标签。

 

 

2.4.RotateAnimation:旋转渐变效果,创建时指定动画起始以及结束的旋转角度,以及动画 持续时间和旋转的轴心;对应<rotate/>标签。

 

 

2.5.AnimationSet:组合渐变,就是前面多种渐变的组合,对应<set/>标签。

 

 

 

 

3.代码讲解

 

补:补间动画相关xml文件存放在anim文件夹中

 

 

3.1.AlphaAnimation(透明度渐变) 

anim_alpha.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="2000"
        android:fromAlpha="1.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toAlpha="0.1" />

</set>

属性解释:

fromAlpha :起始透明度。

toAlpha:结束透明度 透明度的范围为:0-1,完全透明-完全不透明。

 

 

 

3.2.ScaleAnimation(缩放渐变)

anim_scale.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <scale xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="2000"
        android:fromXScale="0.2"
        android:fromYScale="0.2"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.5"
        android:toYScale="1.5" />

</set>

属性解释:

fromXScale/fromYScale:沿着X轴/Y轴缩放的起始比例。

toXScale/toYScale:沿着X轴/Y轴缩放的结束比例。

pivotX/pivotY:缩放的中轴点X/Y坐标,即距离自身左边缘的位置,比如50%就是以图像的 中心为中轴点。

 

 

 

3.3.TranslateAnimation(位移渐变)

anim_translate.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="2000"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toXDelta="320"
        android:toYDelta="0" />

</set>

属性解释:

fromXDelta/fromYDelta:动画起始位置的X/Y坐标。

toXDelta/toYDelta:动画结束位置的X/Y坐标。

 

 

 

 

3.4.RotateAnimation(旋转渐变)

anim_rotate.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="1000"
        android:fromDegrees="0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:toDegrees="360" />

</set>

属性解释:

fromDegrees/toDegrees:旋转的起始/结束角度。

repeatCount:旋转的次数,默认值为0,代表一次,假如是其他值,比如3,则旋转4次 另外,值为-1或者infinite时,表示动画永不停止。

repeatMode:设置重复模式,默认restart,但只有当repeatCount大于0或者infinite或-1时 才有效。还可以设置成reverse,表示偶数次显示动画时会做方向相反的运动。

 

 

 

3.5.AnimationSet(组合渐变)前面几个动画组合到一起。

anim_set.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:shareInterpolator="true">

    <scale
        android:duration="2000"
        android:fromXScale="0.2"
        android:fromYScale="0.2"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.5"
        android:toYScale="1.5" />

    <rotate
        android:duration="1000"
        android:fromDegrees="0"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:toDegrees="360" />

    <translate
        android:duration="2000"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="320"
        android:toYDelta="0" />

    <alpha
        android:duration="2000"
        android:fromAlpha="1.0"
        android:toAlpha="0.1" />


</set>

 

 

 

3.6.Activity代码

/**
 * 补间动画
 */

public class CurationAnimationActivity extends AppCompatActivity {

    private Button btn_alpha;
    private Button btn_scale;
    private Button btn_tran;
    private Button btn_rotate;
    private Button btn_set;
    private ImageView imageView;
    private Animation an;

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

        imageView = findViewById(R.id.img_show);
        //透明度
        btn_alpha = findViewById(R.id.btn_alpha);
        btn_alpha.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                an = AnimationUtils.loadAnimation(CurationAnimationActivity.this, R.anim.anim_alpha);
                imageView.startAnimation(an);
            }
        });


        //缩放
        btn_scale = findViewById(R.id.btn_scale);
        btn_scale.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                an = AnimationUtils.loadAnimation(CurationAnimationActivity.this, R.anim.anim_scale);
                imageView.startAnimation(an);
            }
        });


        //位移
        btn_tran = findViewById(R.id.btn_tran);
        btn_tran.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                an = AnimationUtils.loadAnimation(CurationAnimationActivity.this, R.anim.anim_translate);
                imageView.startAnimation(an);
            }
        });


        //旋转
        btn_rotate = findViewById(R.id.btn_rotate);
        btn_rotate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                an = AnimationUtils.loadAnimation(CurationAnimationActivity.this, R.anim.anim_rotate);
                imageView.startAnimation(an);
            }
        });


        //组合
        btn_set = findViewById(R.id.btn_set);
        btn_set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                an = AnimationUtils.loadAnimation(CurationAnimationActivity.this, R.anim.anim_set);
                imageView.startAnimation(an);
            }
        });

    }
}

 

补:

 

1.说明

Animation an=AnimationUtils.loadAnimation("上下文对象", "四种类型其中一种的布局文件");

任意View(TextView,Imageview).startAnimation(an);

 

2.设置补间动画监听

//透明度
btn_alpha = findViewById(R.id.btn_alpha);
btn_alpha.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {
        an = AnimationUtils.loadAnimation(CurationAnimationActivity.this, R.anim.anim_alpha);
        imageView.startAnimation(an);
        an.setAnimationListener(new Animation.AnimationListener() {
            @Override
             public void onAnimationStart(Animation animation) {
                  Log.d("TAG","补间动画开始!");
             }

             @Override
             public void onAnimationEnd(Animation animation) {
                  Log.d("TAG","补间动画结束!");
             }

             @Override
             public void onAnimationRepeat(Animation animation) {
                   Log.d("TAG","补间动画重复!");
             }
         });
   }
});

 

效果:

 

 

3.7.Activity布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <Button
        android:id="@+id/btn_alpha"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="透明度渐变" />


    <Button
        android:id="@+id/btn_scale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="缩放渐变" />


    <Button
        android:id="@+id/btn_tran"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="位移渐变" />


    <Button
        android:id="@+id/btn_rotate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="旋转渐变" />


    <Button
        android:id="@+id/btn_set"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="组合渐变" />


    <ImageView
        android:id="@+id/img_show"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_gravity="center"
        android:layout_marginTop="50dp"
        android:src="@drawable/progress_loading_image_01" />


</LinearLayout>

 

 

 

 

4.Fragment设置过渡动画

 

这里要注意一点,就是Fragment是使用的v4包还是app包下的Fragment。我们可以调用FragmentTransaction对象的setTransition(int transit) 为Fragment指定标准的过场动画,transit的可选值如下:

TRANSIT_NONE:无动画。

TRANSIT_FRAGMENT_OPEN:打开形式的动画。

TRANSIT_FRAGMENT_CLOSE:关闭形式的动画。

 

上面的标准过程动画是两个都可以调用的,而不同的地方则在于自定义转场动画

setCustomAnimations()方法

app包下的Fragment: setCustomAnimations(int enter, int exit, int popEnter, int popExit) 分别是添加,移除,入栈,以及出栈时的动画! 另外要注意一点的是,对应的动画类型是:属性动画(Property),就是动画文件 的根标签要是:<objectAnimator>,<valueAnimator>或者是前面两者放到一个<set>里。

 

v4包下的Fragment: v4包下的则支持两种setCustomAnimations()。

 

 

 

 

5.补间动画使用举例

 

5.1.Activity跳转目的Activity从底部弹出

 

 

 

activity_bottomopen.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

<translate
    android:duration="800"
    android:fromYDelta="100%p"
    android:toYDelta="0" />


</set>

 

activity_bottomclose.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:duration="800"
        android:fromYDelta="0"
        android:toYDelta="100%p" />


</set>

 

启动Activity关键代码

Intent intent=new Intent(this,MessageModeActivity.class);
intent.putExtra("receiverName",receiverName);
intent.putExtra("loginAccount",loginAccount);
startActivityForResult(intent,MODE);
overridePendingTransition(R.anim.activity_bottomopen,0);

overridePendingTransition(R.anim.activity_bottomopen,0);

 

目标Activity关键代码

finish();
overridePendingTransition(0,R.anim.activity_bottomclose);

overridePendingTransition(0,R.anim.activity_bottomclose);

 

 

 

 

5.2.APP启动页面(整个Activity进入时缩放效果)

 

 

 

Activity关键代码

 

private View view;
private Animation animation;

view=View.inflate(this, R.layout.activity_splash, null);
setContentView(view);

 

 /**
     * 跳转activity
     * */

    public void gotoActivity(){
        // 设置动画效果是alpha 在anim目录下的anim_main.xml文件中定义动画效果
        animation= AnimationUtils.loadAnimation(this, R.anim.anim_splash);
        // 给view设置动画效果
        view.startAnimation(animation);
        animation.setAnimationListener(new Animation.AnimationListener() {

            //动画开始
            @Override
            public void onAnimationStart(Animation arg0) {
                progressBar.setVisibility(View.VISIBLE);
                textView.setText(StringConstant.Splashing4);
            }

            //动画重复

            @Override
            public void onAnimationRepeat(Animation arg0) {

            }

            /**
             * 这里监听动画结束的动作,在动画结束的时候开启一个线程,这个线程中绑定一个Handler
             * 并在这个Handler中调用goHome方法,而通过postDelayed方法使这个方法延迟1000毫秒执行
             * 达到持续显示第一屏1000毫秒的效果
             * */

            @Override
            public void onAnimationEnd(Animation arg0) {
                progressBar.setVisibility(View.GONE);
                textView.setText("");
                //跳转页面进入APP
                Intent intent=new Intent(SplashActivity.this,MainTabActivity.class);
                startActivity(intent);
                finish();
                overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
            }
        });
    }

 

即:将Activity的布局当成View

 

 

完成代码:https://github.com/wujianning/AndroidAnimation

 

 

补:

 

Interpolator讲解:用来控制动画的变化速度,可以理解成动画渲染器,当然我们也可以自己实现Interpolator 接口,自行来控制动画的变化速度,而Android中已经为我们提供了五个可供选择的实现类。

 

LinearInterpolator:动画以均匀的速度改变。

 

AccelerateInterpolator:在动画开始的地方改变速度较慢,然后开始加速。

 

AccelerateDecelerateInterpolator:在动画开始、结束的地方改变速度较慢,中间时加速。

 

CycleInterpolator:动画循环播放特定次数,变化速度按正弦曲线改变 Math.sin(2 * mCycles * Math.PI * input)。

 

DecelerateInterpolator:在动画开始的地方改变速度较快,然后开始减速。

 

AnticipateInterpolator:反向,先向相反方向改变一段再加速播放。

 

AnticipateOvershootInterpolator:开始的时候向后然后向前甩一定值后返回最后的值。

 

BounceInterpolator: 跳跃,快到目的值时值会跳跃,如目的值100,后面的值可能依次为85,77,70,          80,90,100。

 

OvershottInterpolator:回弹,最后超出目的值然后缓慢改变到目的值。

 

 

 

附1:帧动画讲解:Android 动画之帧动画

附2:属性动画讲解:Android 动画之属性动画

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值