如何在Java中使用淡入淡出Android動畫?
淡入淡出動畫基於alpha動畫類。此示例演示瞭如何在Java中使用淡入淡出Android動畫。
步驟 1 - 在Android Studio中建立一個新專案,轉到檔案⇒新建專案並填寫所有必需的詳細資訊以建立新專案。
步驟 2 - 將以下程式碼新增到res/layout/login.xml。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/parent" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher"/> <Button android:id="@+id/fadeIn" android:text="Fade In" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/fadeOut" android:text="Fade Out" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
在上面的程式碼中,我們使用了ImageView和兩個按鈕。FadeIn按鈕將為ImageView提供淡入動畫,而FadeOut按鈕將為ImageView提供淡出動畫。
步驟 3 - 將以下程式碼新增到src/MainActivity.java
package com.example.andy.myapplication;
import android.animation.ObjectAnimator;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imageView = findViewById(R.id.imageView);
Button fadeIn = findViewById(R.id.fadeIn);
fadeIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
alphaAnimation.setDuration(1000);
alphaAnimation.setRepeatCount(1);
alphaAnimation.setRepeatMode(Animation.REVERSE);
imageView.startAnimation(alphaAnimation);
}
});
Button fadeOut = findViewById(R.id.fadeOut);
fadeOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator fadeOut = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0);
fadeOut.setDuration(2000);
fadeOut.start();
}
});
}
}要為ImageView提供淡入動畫,請使用以下程式碼 -
AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f); alphaAnimation.setDuration(1000); alphaAnimation.setRepeatCount(1); alphaAnimation.setRepeatMode(Animation.REVERSE); imageView.startAnimation(alphaAnimation);
要為ImageView提供淡出動畫,請使用以下程式碼 -
ObjectAnimator fadeOut = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0); fadeOut.setDuration(2000); fadeOut.start();
讓我們嘗試執行您的應用程式。我假設您已將您的實際Android移動裝置連線到您的計算機。要從Android Studio執行應用程式,請開啟專案中的一個活動檔案,然後單擊執行
工具欄中的圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕 -

現在單擊淡入按鈕,它將為ImageView提供動畫,如下所示 -

現在單擊淡出動畫,它將為ImageView提供淡出動畫,如下所示 -

單擊 此處 下載專案程式碼
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP