常见的系统动画

来源:互联网 发布:手机淘宝帐号注册流程 编辑:程序博客网 时间:2024/06/06 05:52

为方便开发人员,安卓提供了一些应用于视图的转换动画,可以在运行时用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);}}}


在一些简单的实践中,用系统动画就可以满足效果。

0 0
原创粉丝点击