Android动画基础【2】——(视觉动画系统之透明度动画)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yz_cfm/article/details/82024436

Android中视图动画系统:

和逐帧动画不同的是,视图动画系统操作的对象是视图对象。换句话说,视图动画系统可以让例如文本框、按钮、ImageView等动起来。

视图动画系统中所用的类所在包为: android.view.animation中。

其中包括的类有: Animation类。Animation子类包括: AlphaAnimation、ScaleAnimation、TranslateAnimation、RotateAnimation、AnimationSet等等。

视图动画系统原理: 通过对整个视图不断做图像的变换(平移、缩放、旋转、透明度等)产生的动画效果,它是一种渐进式动画。

什么是补间动画?

通过确定开始的视图样式和结束的视图样式、中间动画变化的过程由系统补全来确定一个动画。其实视图动画系统就是利用补间动画的原理所产生出来的视图动画效果。

视图动画——透明度动画:

我们可以在资源文件中定义透明度动画,也可以在java代码中定义。

如果在资源文件中,我们使用<alpha>标签,每个<alpha>标签都对应一个AlphaAnimation对象,它能控制对象的透明程度。

步骤:

1. 在/res目录下新建一个子目录/res/anim。

2. 在子目录下定义一个动画xml资源文件: /res/anim/alpha.xml。

3.获取AlphaAnimation对象,然后使用它即可。详细请看下面示例。

示例:

/res/anim/alpha.xml文件:

android:duration:控制间隔时间。

android:fromAlpha: 设置起始透明度 (0.0~1.0)

android:toAlpha:设置目标透明度

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="2000"
        android:fromAlpha="1.0"
        android:toAlpha="0.1"/>
</set>

activity_main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/activity_view_animation"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <View
            android:id="@+id/viewAccelerate"
            android:background="@color/colorPrimary"
            android:layout_margin="8dp"
            android:layout_width="300dp"
            android:layout_height="300dp"
            android:layout_gravity="center"/>
    </LinearLayout>
</ScrollView>

ManiActivity.java文件:

public class MainActivity extends AppCompatActivity {
    private View mView;

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

        mView = findViewById(R.id.viewAccelerate);
        mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
                mView.startAnimation(animation);
            }
        });
    }
}

效果图:

猜你喜欢

转载自blog.csdn.net/yz_cfm/article/details/82024436