android動畫之AlphaAnimation(漸變動畫,最簡單的動畫)
阿新 • • 發佈:2019-02-14
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);