android动画之AlphaAnimation(渐变动画,最简单的动画)
来源:互联网 发布:陈乔恩长相知乎 编辑:程序博客网 时间:2024/05/01 08:59
AlphaAnimation渐变动画只需要设置从一可见程度,到另一可见程度即可。
示例代码:
package com.hongchou.www.myalphaanimation;import android.app.Activity;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Window;import android.view.animation.AlphaAnimation;import android.widget.ImageView;/** * android AlphaAnimation渐变动画 */public class MainActivity extends Activity { private ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); iv = findViewById(R.id.iv); AlphaAnimation alphaAnimation=new AlphaAnimation(0,1); alphaAnimation.setDuration(2000); iv.startAnimation(alphaAnimation); }}
布局文件代码:
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.hongchou.www.myalphaanimation.MainActivity"> <ImageView android:id="@+id/iv" android:background="@drawable/bk" android:layout_width="match_parent" android:layout_height="match_parent" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /></android.support.constraint.ConstraintLayout>
注意设置动画时间,才能看到动画效果
alphaAnimation.setDuration(2000);
阅读全文