动画 -- View动画 -- 动画集合

一、概念

动画集合,子类:AnimationSet,标签:<set>
它可以包含若干个动画,并且它的内部也可以嵌套其它动画集合。

二、实现

1. XML实现

//res/anim/set_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fillAfter="true">
    <translate
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="300"
        android:toYDelta="300"
        />
    <scale
        android:fromXScale="0"
        android:fromYScale="0"
        android:toXScale="1"
        android:toYScale="1"
        android:pivotX="0%"
        android:pivotY="0%"
        />
    <alpha
        android:fromAlpha="0.1"
        android:toAlpha="1" />
</set>

//布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="500px"
    android:layout_height="500px"
    android:layout_marginLeft="100px"
    android:background="@color/colorAccent">
    <TextView
        android:id="@+id/animate_tv"
        android:layout_width="wrap_content"
        android:layout_height="100px"
        android:gravity="center"
        android:textColor="@color/colorAccent"
        android:text="Hello Android!"
        android:background="@color/colorPrimaryDark"/>
</RelativeLayout>

//代码,MainActivity
private void setAnimationXML() {
    Animation setAnimation = AnimationUtils.loadAnimation(this, R.anim.set_animation);
    mAnimate_tv.setAnimation(setAnimation);
    mAnimate_tv.startAnimation(setAnimation);
}

private void stopAnimation() {
    mAnimate_tv.clearAnimation();
}

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

@Override
protected void onDestroy() {
    super.onDestroy();
    stopAnimation();
}

2. 代码实现

//布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="500px"
    android:layout_height="500px"
    android:layout_marginLeft="100px"
    android:background="@color/colorAccent">
    <TextView
        android:id="@+id/animate_tv"
        android:layout_width="wrap_content"
        android:layout_height="100px"
        android:gravity="center"
        android:textColor="@color/colorAccent"
        android:text="Hello Android!"
        android:background="@color/colorPrimaryDark"/>
</RelativeLayout>

//代码,MainActivity
private void setAnimationCode() {
    AnimationSet animationSet = new AnimationSet(true); //参数为true表示子动画共用一个插值器
    animationSet.addAnimation(new TranslateAnimation(0, 300, 0, 300));
    animationSet.addAnimation(new ScaleAnimation(0, 1.0f, 0, 1.0f, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0));
    animationSet.addAnimation(new AlphaAnimation(0.1f, 1f));

    animationSet.setDuration(3000);
    animationSet.setFillAfter(true);
    mAnimate_tv.setAnimation(animationSet);
    mAnimate_tv.startAnimation(animationSet);
}

private void stopAnimation() {
    mAnimate_tv.clearAnimation();
}

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

@Override
protected void onDestroy() {
    super.onDestroy();
    stopAnimation();
}

三、属性

android:interpolator:
表示动画集合所采用的插值器,插值器影响动画的速度。比如非匀速动画就需要通过插值器来控制动画的播放过程。这个属性可以不指定,默认为@android:anim/accelerate_decelerate_interpolator,即加速减速插值器。
android:shareInterpolator:
表示集合中的动画是否和集合共享同一个插值器。如果集合不指定插值器,那么子动画就需要单独指定所需的插值器或者使用默认插值器。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,081评论 25 709
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,805评论 18 399
  • 落万千繁华为旧景,现还能有谁的身影。纵然将所向披靡,纵横天下的肆意,亦或是伤春悲秋,温月下酒,诗文洒脱的意境。沙场...
    小森geuveu阅读 342评论 0 3
  • 清晨的窗外 有花莲一支 穿过厚重的黑夜 在绿叶上定姿 住舒奢的温房 与群芳共舞吗? 享精心的美食 受人滋养吗? 不...
    信长工阅读 219评论 0 0
  • Leon and Amy: 每个人都有自己的烦恼,今天我们来学习一下关于烦恼的英语。 烦恼,trouble, wo...
    岭南后生阅读 555评论 0 0