本文主要是介绍常见的系统动画,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
随时随地技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)
为方便开发人员,安卓提供了一些应用于视图的转换动画,可以在运行时用AnimationUtils类加载这些动画。
1、滑入和渐显
AnimationUtils.makeInAnimation(context, fromLeft); 第二个参数决定滑入的方向是左侧还是右侧
2、向上滑入和渐显
AnimationUtils.makeInChildBottomAnimation(context);
视图总是从屏幕的底部向上滑入
3、滑出和渐隐
AnimationUtils.makeOutAnimation(context, toRight);
第二个参数决定滑出的方向是左侧还是右侧
4、渐隐
AnimationUtils.loadAnimation(context, android.R.anim.fade_out); 直接加载系统的渐隐动画。
5、渐显
AnimationUtils.loadAnimation(context, android.R.anim.fade_in);直接加载系统的渐显动画。
这些变换动画只会临时改变视图的显示方式,如果需要永久显示或者隐藏需要结合Visibility参数
下面举个简单示例
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><Button android:id="@+id/main_btn"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="点击试试"/><View android:layout_width="match_parent"android:layout_height="100dp"android:id="@+id/main_view"android:background="#CCC"/></LinearLayout>
Java代码:
public class MainActivity extends Activity implements OnClickListener{private View animateView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);animateView=findViewById(R.id.main_view);findViewById(R.id.main_btn).setOnClickListener(this);}@Overridepublic void onClick(View view) {if(animateView.getVisibility()==View.VISIBLE){Animation out=AnimationUtils.makeOutAnimation(this, true);//从右滑出animateView.startAnimation(out);animateView.setVisibility(View.INVISIBLE);}else {Animation in=AnimationUtils.makeInChildBottomAnimation(this);//底部滑入animateView.startAnimation(in);animateView.setVisibility(View.VISIBLE);}}}
在一些简单的实践中,用系统动画就可以满足效果。
这篇关于常见的系统动画的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!