#Android学习#Animation简单使用

来源:互联网 发布:mac dwg文件怎么打开 编辑:程序博客网 时间:2024/06/05 21:24

合理地使用动画效果,可以会用户带来更好的体验,Android也自带了很多的动画效果,如透明、移动、旋转、缩放,同时也能将这些动画效果混合起来使用。
Android使用动画效果可以有两种方式,一种是通过调用动画资源文件,另一种是直接在代码里编写。
我们通过上面提到的几种动画效果,来简单说下Animation的使用.
通过Android Studio新建一个工程,取名LearnAnimation,(PS. Android Studio让安卓开发变得更加简单,尤其是代码提示的功能,很赞,如果刚开始接触Android开发,推荐用Android Studio作为开发工具,个人觉得比Eclipse好用)
在布局文件里,增加几个按钮,点击的时候显示不同的动画效果

<?xml version="1.0" encoding="utf-8"?><LinearLayout 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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:orientation="vertical"    app:layout_behavior="@string/appbar_scrolling_view_behavior"    tools:context="com.cyfloel.learnanimation.MainActivity"    tools:showIn="@layout/activity_main">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/btnAlphaAnimation"        android:layout_gravity="center_horizontal"        android:text="透明"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/btnRotateRect"        android:layout_gravity="center"        android:text="旋转"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/btnTranslate"        android:layout_gravity="center"        android:text="移动"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/btnScaleAnimation"        android:layout_gravity="center"        android:text="缩放"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/btnAnimate"        android:layout_gravity="center"        android:text="混合动画"/></LinearLayout>

布局文件搞定后,我们就可以在MainActivity里添加Button的监听事件了,这里前面三个按钮的动画效果通过代码来实现,而后两个按钮的动画效果通过资源文件来实现,我们先看看通过代码是如何实现动画效果的

// MainActivity onCreate函数        // 透明动画效果        findViewById(R.id.btnAlphaAnimation).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                alphaAnimation = new AlphaAnimation(0, 1);                alphaAnimation.setDuration(1000);                v.startAnimation(alphaAnimation);            }        });        // 旋转动画效果        findViewById(R.id.btnRotateRect).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 以自身为中心旋转360度                // 通过使用Animation.RELATIVE_TO_SELF,0.5f来控制中心点                rotateAnimation = new RotateAnimation(0, 360,                        Animation.RELATIVE_TO_SELF, 0.5f,                        Animation.RELATIVE_TO_SELF, 0.5f);                rotateAnimation.setDuration(1000);                v.startAnimation(rotateAnimation);            }        });        // 移动动画效果        findViewById(R.id.btnTranslate).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {//                translateAnimation = new TranslateAnimation(0,200,0,200);//                translateAnimation.setDuration(1000);//                v.startAnimation(translateAnimation);                v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate_animation));            }        });

上面的是通过代码的方式来实现,如果是通过资源文件的方式,我们需要新建名为anim的资源文件夹,然后再建立相关动画效果的资源文件,如缩放效果

<!-- 缩放效果 scale_animation.xml--><?xml version="1.0" encoding="utf-8"?><scale xmlns:android="http://schemas.android.com/apk/res/android"    android:fromXScale="0"    android:toXScale="1"    android:fromYScale="0"    android:toYScale="1"    android:pivotX="50%"    android:pivotY="50%"    android:duration="1000"></scale>
<!-- 混合动画效果 mix_animation.xml--><?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="1000"    android:shareInterpolator="true">    <alpha        android:fromAlpha="0"        android:toAlpha="1" />    <scale        android:fromXScale="0"        android:fromYScale="0"        android:pivotX="50%"        android:pivotY="50%"        android:toXScale="1"        android:toYScale="1" />    <rotate        android:fromDegrees="0"        android:pivotX="50%"        android:pivotY="50%"        android:toDegrees="360" /></set>

然后在MainActivity的onCreate函数里,找到点击触发该效果的Button,并添加监听事件。
混合动画效果,实际就是将单个的动画效果放到set标签下,同时设置shareInterpolator属性为true

// MainActivity onCreate// 缩放动画效果        findViewById(R.id.btnScaleAnimation).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                     v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,                     R.anim.scale_animation));            }        });       findViewById(R.id.btnAnimate).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick( View v) {                //v.startAnimation(animationSet);                // 调用资源文件方法                Animation a = AnimationUtils.loadAnimation(MainActivity.this, R.anim.mix_animation);                // 添加动画监听事件                a.setAnimationListener(new Animation.AnimationListener() {                    @Override                    public void onAnimationStart(Animation animation) {                    }                    @Override                    public void onAnimationEnd(Animation animation) {                        Snackbar.make(findViewById(R.id.container),"混合效果",Snackbar.LENGTH_SHORT).show();                    }                    @Override                    public void onAnimationRepeat(Animation animation) {                    }                });                v.startAnimation(a);            }        });     

在混合动画效果里,还增加了对动画效果的监听事件,当点击该按钮时,动画显示完毕后,显示消息提示。
注意,如果是通过代码的方式来实现混合动画效果的,则是通过AnimationSet来控制

        animationSet = new AnimationSet(true);        animationSet.setDuration(1000);        AlphaAnimation aa = new AlphaAnimation(0,1);        aa.setDuration(1000);        animationSet.addAnimation(aa);        TranslateAnimation ta = new TranslateAnimation(200,0,200,0);        ta.setDuration(1000);        animationSet.addAnimation(ta);        // 在startAnimation的时候,将animationSet作为参数传入        v.startAnimation(animationSet);
0 0
原创粉丝点击