Android开发历程_8(Tween Animation的2种属性设置方法)

  在Android开发中,Animation是用来给控件制作效果的。大多数的控件都可以用这个类,这个类包含了4种基本动作,分别为移动,旋转,淡入淡出,缩放。在使用Animation时,可以在.java文件中用java代码对其进行设置,这样的优点是可以方便调试程序效果;另外一种方法就是在xml中对控件的属性做设置,好处是代码的重用性比较高,缺点是不方便调试。本节中就简单介绍下这2种方法的使用流程。参考资料为mars老师的教程。

 

一、在java代码中使用Animation.

  在java代码中使用Animation主要分为下面4个步骤。

  创建一个AnimationSet类,AnimationSet类是一个Animation集合,里面可以许多Animation,且在AnimationSet中设置的属性适用于里面的所有Animation。

  根据我们需要的动态效果创建一个Animation类,主要有4个这样的类,分别为AlphaAnimation,ScaleAnimation,RotateAnimation,TranslateAnimation,分别对应着一种动画效果。

  将上面建立好的Animation设置相应的动态属性,然后加入到AnimationSet中。

  最后在需要适用这个动态的效果的控件中加载这个AnimationSet。

 

  这里,做了一个简单的实验,分别试了下上面的动态效果。实验室对一个image图标进行动态演示,下面有4个按钮,每个按钮对应一个动态演示的效果。这4中效果分别是:image图标由完全透明到完全不透明变化,持续时间为1s;image图标由原来大小尺寸沿自身尺寸中心逐渐缩放到0,持续时间为1s;image图标以自身中心为圆心旋转360度,持续时间为1s;image图标从自身位置开始同时向右和向下移动了imageView控件的宽和高长度。

  界面如下所示(动态效果就不一张一张截图演示了):

  Android开发历程_8(Tween Animation的2种属性设置方法)

  

  在设置Animation属性的时候,有一点需要注意的是,在进行尺度缩放,平移,旋转会涉及到中心点以及移动的距离那些参数,这些参数的值的设置是需要依据它值属性来的,如果值属性为Animation.RELATIVE_TO_SELF,那么就是参考控件自身的坐标,如果是Animation.RELATIVE_TO_PARENT,那么就是参考程序界面的坐标。

 

  实验代码(附录有工程code下载链接):

Java文件代码如下:

package com.example.anim_1;



import android.app.Activity;

import android.view.View;

import android.view.View.OnClickListener;

import android.os.Bundle;

import android.view.Menu;

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.ImageView;



public class MainActivity extends Activity {



    private ImageView image  = null;

    private Button alpha = null;

    private Button scale = null;

    private Button rotate = null;

    private Button translate = null;

    

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        

        image = (ImageView)findViewById(R.id.image);

        alpha = (Button)findViewById(R.id.alpha);

        scale = (Button)findViewById(R.id.scale);

        rotate = (Button)findViewById(R.id.rotate);

        translate = (Button)findViewById(R.id.translate);

        

        alpha.setOnClickListener(new AlphaButtonListener());

        scale.setOnClickListener(new ScaleButtonListener());

        rotate.setOnClickListener(new RotateButtonListener());

        translate.setOnClickListener(new TranslateButtonListener());

        

    }

    

    private class AlphaButtonListener implements OnClickListener{



        public void onClick(View v) {

            // TODO Auto-generated method stub

            //这里设置ture参数表示共享interpolator

            AnimationSet alpha_animation_set = new AnimationSet(true);

            //从完全不透明到完全透明

            //这里的数字后面用f难道表示浮点型?

            AlphaAnimation alpha_animation = new AlphaAnimation(1.0f, 0.0f);    

            alpha_animation_set.addAnimation(alpha_animation);    

            alpha_animation.setDuration(1000);//1s钟    

            image.startAnimation(alpha_animation_set);

        }

        

    }

    

    private class ScaleButtonListener implements OnClickListener{



        public void onClick(View v) {

            // TODO Auto-generated method stub

            AnimationSet scale_animation_set = new AnimationSet(true);

            //以自身为尺度缩放中心,从原大小尺寸逐渐缩放到0尺寸

            ScaleAnimation scale_animation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f, 

                                            Animation.RELATIVE_TO_SELF, 0.5f,

                                            Animation.RELATIVE_TO_SELF, 0.5f);    

            scale_animation_set.addAnimation(scale_animation);    

            scale_animation.setDuration(1000);//1s钟    

            image.startAnimation(scale_animation_set);

        }

        

    }

    

    private class RotateButtonListener implements OnClickListener{



        public void onClick(View v) {

            // TODO Auto-generated method stub

            AnimationSet rotate_animation_set = new AnimationSet(true);

            //以自身中心为圆心,旋转360度

            RotateAnimation rotate_animation = new RotateAnimation(0, 360, 

                                            Animation.RELATIVE_TO_SELF, 0.5f,

                                            Animation.RELATIVE_TO_SELF, 0.5f);    

            rotate_animation_set.addAnimation(rotate_animation);    

            rotate_animation.setDuration(1000);//1s钟    

            image.startAnimation(rotate_animation_set);

        }

        

    }

    

    private class TranslateButtonListener implements OnClickListener{



        public void onClick(View v) {

            // TODO Auto-generated method stub

            AnimationSet translate_animation_set = new AnimationSet(true);

            //以自身为坐标系和长度单位,从(0,0)移动到(1,1)

            TranslateAnimation translate_animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,

                                                        0.0f, Animation.RELATIVE_TO_SELF, 1.0f, 

                                                        Animation.RELATIVE_TO_SELF, 0.0f, 

                                                        Animation.RELATIVE_TO_SELF, 1.0f);    

            translate_animation_set.addAnimation(translate_animation);    

            translate_animation.setDuration(1000);//1s钟    

            image.startAnimation(translate_animation_set);

        }

        

    }

    

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.activity_main, menu);

        return true;

    }

}



    

 

本次试验的xml布局文件代码如下:

<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" >



    <ImageView 

        android:id="@+id/image"

        android:layout_centerHorizontal="true"

        android:paddingTop="80px"

        android:paddingBottom="80px"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:src="@drawable/girl"

        />  

    

    <LinearLayout 

        android:id="@+id/button_group"

        android:orientation="horizontal"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:gravity="center_horizontal"

        android:layout_below="@id/image"

        >

        <Button 

            android:id="@+id/alpha"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_margin="5px"

            android:text="@string/alpha"

            android:ems="1"

            />

        <Button 

            android:id="@+id/scale"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_margin="5px"

            android:text="@string/scale"

            android:ems="1"

            />

        <Button 

            android:id="@+id/rotate"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_margin="5px"

            android:text="@string/rotate"

            android:ems="1"

            />

        <Button 

            android:id="@+id/translate"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_margin="5px"

            android:text="@string/translate"

            android:ems="1"

            />

    </LinearLayout>



</RelativeLayout>

 

二、在xml文件中使用animation

  在xml中使用animation,只需对xml文件中的动画属性进行设置,这样写好的一个xml文件可以被多次利用。

  完成该功能步骤大概如下:

  首先在res目录下新建anim目录,在anim目录里面建立xml文件,每个xml文件对应里面可以设置各种动画,可以将上面讲到的4种动画都放里面。这些xml文件的属性设置应该都在set标签下面。

  在java语句中,只需要对需要动画显示的控件加载一个animation对象,该animation对象采用AnimationUtils.loadAnimation()方法来获得,该方法里面就有一个参数为anim下的一个xml,因此这个控件也就得到了相应xml里面设置的动画效果了。

  这次试验的效果和第一种情况的一样,只是图片换了一张。

  效果演示界面如下:

  Android开发历程_8(Tween Animation的2种属性设置方法)

 

  在用xml设置动态属性的时候,有些属性比如旋转中心,平移尺寸的设置,有如下规则:

  如果android:pivotX=”N”,则表示绝对坐标比例,即屏幕的坐标比例。

  如果android:pivotX=”N%”,则表示相对自身的坐标比例。

  如果android:pivotX=”N%p”,则表示相对于父控件的坐标比例。

 

  实验代码如下(附录有工程code下载链接):

 

MainActivity.java:

package com.example.anim_2;



import android.app.Activity;

import android.view.View;

import android.view.View.OnClickListener;

import android.os.Bundle;

import android.view.Menu;

import android.view.animation.Animation;

import android.view.animation.AnimationUtils;

import android.widget.Button;

import android.widget.ImageView;



public class MainActivity extends Activity {



    private ImageView image  = null;

    private Button alpha = null;

    private Button scale = null;

    private Button rotate = null;

    private Button translate = null;

    

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        

        image = (ImageView)findViewById(R.id.image);

        alpha = (Button)findViewById(R.id.alpha);

        scale = (Button)findViewById(R.id.scale);

        rotate = (Button)findViewById(R.id.rotate);

        translate = (Button)findViewById(R.id.translate);

        

        alpha.setOnClickListener(new AlphaButtonListener());

        scale.setOnClickListener(new ScaleButtonListener());

        rotate.setOnClickListener(new RotateButtonListener());

        translate.setOnClickListener(new TranslateButtonListener());

        

    }

   

    private class AlphaButtonListener implements OnClickListener{



        public void onClick(View v) {

            // TODO Auto-generated method stub

            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);

            image.startAnimation(animation);

        }

        

    }

    

    private class ScaleButtonListener implements OnClickListener{



        public void onClick(View v) {

            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale);

            image.startAnimation(animation);

        }

        

    }

    

    private class RotateButtonListener implements OnClickListener{



        public void onClick(View v) {

            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);

            image.startAnimation(animation);

        }

        

    }

    

    private class TranslateButtonListener implements OnClickListener{



        public void onClick(View v) {

            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate);

            image.startAnimation(animation);

        }

        

    }

    

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.activity_main, menu);

        return true;

    }

}

 

activity_main.xml:

<LinearLayout 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:orientation="vertical"

    >



    <LinearLayout

        android:id="@+id/image_layout"

        android:layout_width="fill_parent"

        android:layout_height="250dip"

        android:gravity="center"

        >

        <ImageView 

            android:id="@+id/image" 

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:src="@drawable/olympic"

            />           

    </LinearLayout> 

    

    <LinearLayout 

        android:id="@+id/button_group"

        android:orientation="horizontal"

        android:gravity="center_horizontal"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:layout_below="@id/image_layout"

        >

        <Button 

            android:id="@+id/alpha"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_margin="5dip"

            android:text="@string/alpha"

            android:ems="1"

            />

        <Button 

            android:id="@+id/scale"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_margin="5dip"

            android:text="@string/scale"

            android:ems="1"

            />

        <Button 

            android:id="@+id/rotate"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_margin="5dip"

            android:text="@string/rotate"

            android:ems="1"

            />

        <Button 

            android:id="@+id/translate"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_margin="5dip"

            android:text="@string/translate"

            android:ems="1"

            />

    </LinearLayout>

  

</LinearLayout>

 

anim/alpha.xml:

set

    android:interpolator="@android:anim/accelerate_interpolator"

    xmlns:android="http://schemas.android.com/apk/res/android">

    <alpha 

        android:fromAlpha="1.0"

        android:toAlpha="0.0"

        android:duration="1000"

        />   

</set>

 

anim/scale.xml:

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android"

    android:interpolator="@android:anim/accelerate_interpolator">

    <scale 

        android:fromXScale="1.0"

        android:toXScale="0.0"

        android:fromYScale="1.0"

        android:toYScale="0.0"

        android:pivotX="50%"

        android:pivotY="50%"

        android:duration="1000"       

        />



</set>

 

anim/rotate.xml:

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android"

    android:interpolator="@android:anim/accelerate_interpolator"

    >   

    <rotate

        android:fromDegrees="0"

        android:toDegrees="360"

        android:pivotX="50%"

        android:pivotY="50%"

        android:duration="1000"

        />



</set>

 

anim/translate.xml:

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">

    android:interpolator="@android:anim/accelerate_interpolator"

    >

    <translate 

        android:fromXDelta="0%"

        android:toXDelta="100%"

        android:fromYDelta="0%"

        android:toYDelta="100%"

        android:duration="1000"

        />

</set>

 

 

  总结:本次实验主要讲android中animation这个类对控件的动画效果设置,对用java代码和用xml代码2种方式都做了个简单的实验。懂得其使用方法的大体流程。

 

  附:实验工程code下载

 

 

 

你可能感兴趣的:(animation)