如何在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提供淡出动画,如下所示 -

单击 此处 下载项目代码

更新于: 2019年7月30日

1K+ 次浏览

开启您的 职业生涯

通过完成课程获得认证

立即开始
广告

© . All rights reserved.