Android开发之Animaions(一 )

来源:互联网 发布:windows查看网络端口 编辑:程序博客网 时间:2024/06/06 07:47

什么是Animations?

使用Animations可以实现动画效果。


Animations分为两大类:

第一类:Tweened Animations

该类Animations提供了旋转、移动、伸展和淡出等效果。

Tweened Animations的分类:

1.Alpha:淡入淡出效果

2.Scale:缩放效果

3.Rotate:旋转效果

4.Translate:移动效果

第二类:Frame-by-Frame Animations

这一类Animations可以创建一个Drawable序列,这些Drawable可以按照指定的时间间歇一个一个的显示。


Animations的第一种使用方法(在代码中实现)

1.创建一个AnimationSet对象

2.根据需要创建相应的Animation对象

3.根据软件动画的需求,为Animation对象设置相应的数据

4.将Animation对象添加到AnimationSet对象当中

5.使用控件对象开始执行AnimationSet


Tween Animations的通用属性:

setDuration(long durationMills):设置动画持续时间(单位:毫秒)

setFillAfter(boolean fillAfter):如果fillAfter的值为true,则动画执行后,控件将停留在执行结束的状态

setFillBefore(boolean fillBefore):如果fillBefore的值为true,则动画执行后,控件将回到动画执行之前的状态

setStartOffSet(long startOffSet):设置动画执行之前的等待时间

setRepeatCount(int repeatCount):设置动画重复执行的次数(注意:设置ScaleAnimation对象的循环次数才能实现循环效果,设置AnimationSet对象的循环次数不能实现动画循环效果


activity_main.xml:

<?xml version="1.0" encoding="utf-8"?><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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.mycompany.animations.MainActivity">    <ImageView        android:id="@+id/imageView"        android:src="@drawable/bigdog"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <LinearLayout        android:orientation="vertical"        android:layout_alignParentBottom="true"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <Button            android:id="@+id/alpha"            android:text="淡入淡出动画效果"            android:layout_width="match_parent"            android:layout_height="wrap_content" />        <Button            android:id="@+id/scale"            android:text="缩放动画效果"            android:layout_width="match_parent"            android:layout_height="wrap_content" />        <Button            android:id="@+id/rotate"            android:text="旋转动画效果"            android:layout_width="match_parent"            android:layout_height="wrap_content" />        <Button            android:id="@+id/translate"            android:text="移动效果"            android:layout_width="match_parent"            android:layout_height="wrap_content" />    </LinearLayout></RelativeLayout>

MainActivity.java:

package com.mycompany.animations;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.app.NotificationCompat;import android.view.View;import android.view.animation.AlphaAnimation;import android.view.animation.Animation;import android.view.animation.AnimationSet;import android.view.animation.RotateAnimation;import android.view.animation.ScaleAnimation;import android.view.animation.TranslateAnimation;import android.widget.Button;import android.widget.ImageButton;import android.widget.ImageView;public class MainActivity extends AppCompatActivity {    private ImageView imageView;    private Button alphaButton;    private Button scaleButton;    private Button rotateButton;    private Button translateButton;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        imageView = (ImageView) findViewById(R.id.imageView);        alphaButton = (Button) findViewById(R.id.alpha);        scaleButton = (Button) findViewById(R.id.scale);        rotateButton = (Button) findViewById(R.id.rotate);        translateButton = (Button) findViewById(R.id.translate);        alphaButton.setOnClickListener(new AlphaButtonListener());        rotateButton.setOnClickListener(new RotateButtonListener());        scaleButton.setOnClickListener(new ScaleButtonListener());        translateButton.setOnClickListener(new TranslateButtonListener());    }    class AlphaButtonListener implements View.OnClickListener{        @Override        public void onClick(View v) {            //  创建一个AnimationSet对象            AnimationSet animationSet = new AnimationSet(true);            //  创建一个AlphaAnimation对象,并通过构造函数形参设置透明度            AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);            //  设置动画持续时间            animationSet.setDuration(2000);            //  将AlphaAnimation对象加入到AnimationSet对象中            animationSet.addAnimation(alphaAnimation);            //  将动画效果显示在ImageView对象上            imageView.startAnimation(animationSet);        }    }    class ScaleButtonListener implements View.OnClickListener{        @Override        public void onClick(View v) {            AnimationSet animationSet = new AnimationSet(true);            //  创建ScaleAnimation对象并设置缩放程度和缩放中心            ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.5f, 1, 0.5f,                    Animation.RELATIVE_TO_SELF, 0.5f,                    Animation.RELATIVE_TO_SELF, 0.5f);            animationSet.setDuration(1500);            //  设置循环次数为无限循环            //  需要注意的是:设置ScaleAnimation对象的循环次数才能实现循环效果,            //  设置AnimationSet对象的循环次数不能实现动画循环效果            scaleAnimation.setRepeatCount(Animation.INFINITE);            animationSet.addAnimation(scaleAnimation);            imageView.startAnimation(animationSet);        }    }    class RotateButtonListener implements View.OnClickListener{        @Override        public void onClick(View v) {            AnimationSet animationSet = new AnimationSet(true);            //  创建RotateAnimation对象并通过形参设置旋转中心            RotateAnimation rotateAnimation = new RotateAnimation(0, 360,                    Animation.RELATIVE_TO_SELF, 1,                    Animation.RELATIVE_TO_SELF, 1);            animationSet.setDuration(1500);            animationSet.addAnimation(rotateAnimation);            imageView.startAnimation(animationSet);        }    }    class TranslateButtonListener implements View.OnClickListener{        @Override        public void onClick(View v) {            AnimationSet animationSet = new AnimationSet(true);            //  创建TranslateAnimation对象并设置移动轨迹            TranslateAnimation translateAnimation = new TranslateAnimation(                    Animation.RELATIVE_TO_SELF, 0,                    Animation.RELATIVE_TO_SELF, 0.5f,                    Animation.RELATIVE_TO_SELF, 0,                    Animation.RELATIVE_TO_SELF, 1);            animationSet.setDuration(1500);            animationSet.addAnimation(translateAnimation);            imageView.startAnimation(animationSet);        }    }}


0 0
原创粉丝点击