Android创建淡入淡出动画的详解
作者:路宇 时间:2022-12-28 00:12:12
介绍:
淡入淡出动画(也称为“叠化”)逐渐淡出一个 View 或 ViewGroup,同时淡入另一个。此动画适用于您希望在应用中切换内容或视图的情况。
下面我们通过一个例子来创建使用淡入淡出动画。
首先创建一个简单的布局activity_short_anim.xml
<FrameLayout 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=".ShortAnimActivity">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
style="?android:textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lineSpacingMultiplier="1.2"
android:padding="16dp"
android:text="@string/lorem_ipsum" />
</ScrollView>
<ProgressBar
android:id="@+id/progressBar"
style="?android:progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</FrameLayout>
接下来在对应的Activity中实现相应的功能ShortAnimActivity类
public class ShortAnimActivity extends AppCompatActivity {
private ScrollView scrollView;
private ProgressBar progressBar;
private int shortAnimationDuration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_short_anim);
scrollView = findViewById(R.id.scrollView);
progressBar = findViewById(R.id.progressBar);
scrollView.setVisibility(View.GONE);
//长动画的持续时间为500ms
shortAnimationDuration = getResources().getInteger(android.R.integer.config_longAnimTime);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.animation_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.cross:
crossFade();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
private void crossFade() {
//将内容视图设置为0%不透明(就是透明的意思,不可见的状态),正常显示的为100%不透明可见
// 以便在动画期间可见
scrollView.setAlpha(0f);
scrollView.setVisibility(View.VISIBLE);
//将内容视图设置为100%不透明,并清除视图上的任何动画 *
scrollView.animate()
.alpha(1f)
.setDuration(shortAnimationDuration)
.setListener(null);
//将动画视图设置为0%不透明,动画结束后,将视图隐藏
progressBar.animate()
.alpha(0f)
.setDuration(shortAnimationDuration)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
progressBar.setVisibility(View.GONE);
}
});
}
}
通过点击选项菜单,实现一个淡入淡出的效果,我设置的是500ms的一个动画时间,大家可能看的不太明显,但是实际开发中就是这么一个效果,动画时间不会太长。
效果如图所示:
来源:https://blog.csdn.net/lu202032/article/details/121779559
标签:Android,动画
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java之Jackson的基本使用案例讲解
2022-05-27 22:08:49
![](https://img.aspxhome.com/file/2023/5/105965_0s.png)
C#实现输入10个数存入到数组中并求max和min及平均数的方法示例
2023-11-30 05:58:54
Java实现解出世界最难九宫格问题
2022-06-14 19:47:10
c#获取当前年的周数及当前月的天数示例代码
2021-10-04 09:42:19
Android USB转串口通信开发实例详解
2022-01-05 15:28:17
Android开发之完成登陆界面的数据保存回显操作实例
2022-07-05 19:13:40
IDEA最新版2020.1的maven工程本地依赖仓库无法使用问题(已解决)
2023-09-21 17:57:00
![](https://img.aspxhome.com/file/2023/1/60831_0s.png)
Java @RequestMapping注解功能使用详解
2022-08-15 11:06:10
![](https://img.aspxhome.com/file/2023/9/90039_0s.png)
C# 获取动态key的json对象的值案例
2023-03-03 22:02:08
C# websocket及时通信协议的实现方法示例
2023-11-16 05:14:48
![](https://img.aspxhome.com/file/2023/5/113895_0s.png)
C++实现leetcode(3.最长无重复字符的子串)
2023-06-25 03:17:22
使用C#实现读取系统配置文件的代码实例讲解
2023-02-18 10:36:56
c# 引用Nlog插件的步骤
2021-08-10 01:03:43
![](https://img.aspxhome.com/file/2023/7/111257_0s.png)
Android实现输入法弹出时把布局顶上去和登录按钮顶上去的解决方法
2022-11-10 15:07:26
![](https://img.aspxhome.com/file/2023/7/126707_0s.jpg)
25个最好的免费Eclipse插件
2021-09-21 10:56:24
![](https://img.aspxhome.com/file/2023/2/67642_0s.jpg)
Java中高效判断数组中是否包含某个元素的几种方法
2022-02-21 05:46:39
C#将html table 导出成excel实例
2023-12-20 04:58:55
登录验证全局控制的几种方式总结(session)
2022-09-03 15:25:41
SpringBoot集成Shiro进行权限控制和管理的示例
2022-12-31 19:23:21
用java在web环境下上传和下载文件的技巧
2021-06-09 18:17:17