以下是一个简单的 Android View 渐隐动画示例代码:

  1. 创建一个 XML 文件,定义动画效果:
<!-- fade_out.xml -->
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="1.0"
    android:toAlpha="0.0"
    android:duration="500" />
  1. 在 Activity 中使用 AnimationUtils 加载动画:
Animation animFadeOut = AnimationUtils.loadAnimation(getApplicationContext(),
        R.anim.fade_out);
  1. 将动画应用于 View:
myView.startAnimation(animFadeOut);

完整示例代码:

MainActivity.java:

public class MainActivity extends AppCompatActivity {
    private Button mButton;

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

        mButton = findViewById(R.id.button);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Animation animFadeOut = AnimationUtils.loadAnimation(getApplicationContext(),
                        R.anim.fade_out);
                mButton.startAnimation(animFadeOut);
            }
        });
    }
}

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fade Out"
        android:layout_centerInParent="true"/>

</RelativeLayout>

fade_out.xml:

<!-- fade_out.xml -->
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="1.0"
    android:toAlpha="0.0"
    android:duration="500" />
``
Android view渐隐动画示例代码

原文地址: https://www.cveoy.top/t/topic/frB0 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录