android animation的简单使用

 今天学习了animation的部分内容。包括了四种动画效果,分别是AlphaAnimation(透明度渐变),scaleAnimation(大小渐变),translateAnimation(位置渐变),rotateAnimation(旋转动画)。animation定义有两种方法,一种是在XML文件中定义,另一种是在java代码中定义。通过点击按钮实现各种animation效果。


activity_main.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"
     >
     <LinearLayout android:id="@+id/id_linear1"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal"
         >
	<Button android:id="@+id/btn1"
	    android:layout_height="wrap_content"
	    android:layout_width="wrap_content"
	    android:text="AlphaAnimation"
	    />
	<Button android:id="@+id/btn2"
	    android:layout_height="wrap_content"
	    android:layout_width="wrap_content"
	    android:text="ScaleAnimation"
	    
	    />
	</LinearLayout>
	 <LinearLayout android:id="@+id/id_linear2"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal"
         android:layout_below="@id/id_linear1"
         >
	<Button android:id="@+id/btn3"
	    android:layout_height="wrap_content"
	    android:layout_width="wrap_content"
	    android:text="TranslateAnimation"
	    />
	<Button android:id="@+id/btn4"
	    android:layout_height="wrap_content"
	    android:layout_width="wrap_content"
	    android:text="RotateAnimation"
	    
	    />
	</LinearLayout>
   <ImageView android:id="@+id/id_image"
       android:layout_width="150dp"
       android:layout_height="200dp"
       android:src="@drawable/image7"
       android:layout_below="@id/id_linear2"
       android:layout_centerHorizontal="true"/>
   <Button android:id="@+id/btn5"
       android:layout_below="@+id/id_image"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerHorizontal="true"
       android:text="共同作用"/>
</RelativeLayout>


一.XML中定义animation

        首先在res文件夹下建一个名anim的文件夹(文件夹的名字一定要是anim)

1.AlphaAnimation

    我在anim下建一个名为alpha_anima.xml的文件

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <alpha
        android:duration="3000"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" >
        <!-- 透明度控制动画效果 alpha
        浮点型值:
            fromAlpha 属性为动画起始时透明度
            toAlpha   属性为动画结束时透明度
            说明: 
                0.0表示完全透明
                1.0表示完全不透明
            以上值取0.0-1.0之间的float数据类型的数字
        
        长整型值:
            duration  属性为动画持续时间
            说明:     
                时间以毫秒为单位
-->
    </alpha>
	 
</set>
2.ScaleAnimation

 我在anim下建一个名为scale_animation.xml的文件

<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale  
          android:interpolator=
                     "@android:anim/decelerate_interpolator"
          android:fromXScale="0.0"
          android:toXScale="1.0"
          android:fromYScale="0.0"
          android:toYScale="1.0"
          android:pivotX="50%"
          android:pivotY="50%"
          android:fillAfter="false"
          android:duration="3000" />
    
    <!-- 尺寸伸缩动画效果 scale
       属性:interpolator 指定一个动画的插入器
        在我试验过程中,使用android.res.anim中的资源时候发现
        有三种动画插入器:
            accelerate_decelerate_interpolator  加速-减速 动画插入器
            accelerate_interpolator        加速-动画插入器
            decelerate_interpolator        减速- 动画插入器
        其他的属于特定的动画效果
      浮点型值:
         
            fromXScale 属性为动画起始时 X坐标上的伸缩尺寸    
            toXScale   属性为动画结束时 X坐标上的伸缩尺寸     
        
            fromYScale 属性为动画起始时Y坐标上的伸缩尺寸    
            toYScale   属性为动画结束时Y坐标上的伸缩尺寸    
        
            说明:
                 以上四种属性值    
    
                    0.0表示收缩到没有 
                    1.0表示正常无伸缩     
                    值小于1.0表示收缩  
                    值大于1.0表示放大
        
            pivotX     属性为动画相对于物件的X坐标的开始位置
            pivotY     属性为动画相对于物件的Y坐标的开始位置
        
            说明:
                    以上两个属性值 从0%-100%中取值
                    50%为物件的X或Y方向坐标上的中点位置
        
        长整型值:
            duration  属性为动画持续时间
            说明:   时间以毫秒为单位


        布尔型值:
            fillAfter 属性 当设置为true ,该动画转化在动画结束后被应用
-->


</set>
 

3.TranslateAnimation

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="3000"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="300"
        android:toYDelta="400" />
    <!--
     translate 位置转移动画效果
        整型值:
            fromXDelta 属性为动画起始时 X坐标上的位置    
            toXDelta   属性为动画结束时 X坐标上的位置
            fromYDelta 属性为动画起始时 Y坐标上的位置
            toYDelta   属性为动画结束时 Y坐标上的位置
            注意:
                     没有指定fromXType toXType fromYType toYType 时候,
                     默认是以自己为相对参照物             
        长整型值:
            duration  属性为动画持续时间
            说明:   时间以毫秒为单位

    -->

</set>

4.RotateAnimation

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <rotate
        android:duration="3000"
        android:fromDegrees="0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="270" />
    <!--
     rotate 旋转动画效果
       属性:interpolator 指定一个动画的插入器
             在我试验过程中,使用android.res.anim中的资源时候发现
             有三种动画插入器:
                accelerate_decelerate_interpolator   加速-减速 动画插入器
                accelerate_interpolator               加速-动画插入器
                decelerate_interpolator               减速- 动画插入器
             其他的属于特定的动画效果
                           
       浮点数型值:
            fromDegrees 属性为动画起始时物件的角度    
            toDegrees   属性为动画结束时物件旋转的角度 可以大于360度   

        
            说明:
                     当角度为负数——表示逆时针旋转
                     当角度为正数——表示顺时针旋转              
                     (负数from——to正数:顺时针旋转)   
                     (负数from——to负数:逆时针旋转) 
                     (正数from——to正数:顺时针旋转) 
                     (正数from——to负数:逆时针旋转)       

            pivotX     属性为动画相对于物件的X坐标的开始位置
            pivotY     属性为动画相对于物件的Y坐标的开始位置
                
            说明:        以上两个属性值 从0%-100%中取值
                         50%为物件的X或Y方向坐标上的中点位置

        长整型值:
            duration  属性为动画持续时间
            说明:       时间以毫秒为单位


    -->

</set>

5.多种animation共同作用

我建了一个commom_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:duration="3000"
        android:fromDegrees="0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="+720" />
    
    <translate
        android:duration="3000"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="300"
        android:toYDelta="400" />
	
    <scale  
          android:interpolator=
                     "@android:anim/decelerate_interpolator"
          android:fromXScale="0.0"
          android:toXScale="1.0"
          android:fromYScale="0.0"
          android:toYScale="1.0"
          android:pivotX="50%"
          android:pivotY="50%"
          android:fillAfter="false"
          android:duration="3000" />
</set>

XML文件全部完成

下面是MainActivity中的代码,我为了省事就不把纯java代码的animation和采用xml的在分开写了,有简单的注释,一看就明白

package com.lql.animation;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements OnClickListener {

	private Button btn1, btn2, btn3, btn4, btn5;
	private ImageView imageView;
	private Animation animation, animation2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn1 = (Button) findViewById(R.id.btn1);
		btn2 = (Button) findViewById(R.id.btn2);
		btn3 = (Button) findViewById(R.id.btn3);
		btn4 = (Button) findViewById(R.id.btn4);
		btn5 = (Button) findViewById(R.id.btn5);
		imageView = (ImageView) findViewById(R.id.id_image);
		btn1.setOnClickListener(this);
		btn2.setOnClickListener(this);
		btn3.setOnClickListener(this);
		btn4.setOnClickListener(this);
		btn5.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn1:
			// XML中定义
			// animation = AnimationUtils.loadAnimation(this,
			// R.anim.alpha_anima);//得到xml文件中定义的动画
			// imageView.startAnimation(animation);

			// 采用纯java代码定义
			animation = new AlphaAnimation(0.0f, 1.0f);
			animation.setDuration(3000);
			imageView.setAnimation(animation);// 开启动画
			animation.setAnimationListener(new Animation.AnimationListener() {

				@Override
				public void onAnimationStart(Animation animation) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onAnimationRepeat(Animation animation) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onAnimationEnd(Animation animation) {
					// TODO Auto-generated method stub
					Toast.makeText(MainActivity.this, "动画执行完毕",
							Toast.LENGTH_SHORT).show();
				}
			});
			break;
		case R.id.btn2:
			// XML定义
			// animation = AnimationUtils.loadAnimation(this,
			// R.anim.scale_animation);

			animation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
					Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.5f);
			animation.setDuration(3000);
			imageView.startAnimation(animation);
			break;
		case R.id.btn3:
			// animation = AnimationUtils.loadAnimation(this,
			// R.anim.translate_anim);

			animation = new TranslateAnimation(30.0f, 300.0f, 40.0f, 400.0f);
			animation.setDuration(3000);
			
			imageView.startAnimation(animation);
			break;
		case R.id.btn4:
			animation = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);
			animation.setStartOffset(1000);//等待1000毫秒才开始执行动画
			animation.setFillAfter(true);//动画执行完毕后保持,执行后的状态
			imageView.startAnimation(animation);
			break;
		case R.id.btn5:
			// animation = AnimationUtils.loadAnimation(this,
			// R.anim.common_anim);
			AnimationSet animationSet = new AnimationSet(true);// 动画集
			animation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
					Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.5f);
			animation2 = new TranslateAnimation(30.0f, 300.0f, 40.0f, 400.0f);
			animation.setDuration(3000);
			animation2.setDuration(3000);
			animationSet.addAnimation(animation);// 将animation添加到动画集中
			animationSet.addAnimation(animation2);// 将animation2添加到动画集中
			imageView.startAnimation(animationSet);
			
			break;
		}
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值