Animations使用(二)

本文介绍如何在Android中通过XML文件配置动画,包括旋转、缩放、淡入淡出及移动等效果的实现方法。提供了完整的代码示例,涵盖XML动画文件及Activity中动画的加载与播放。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在代码中使用Animations可以很方便的调试、运行,但是代码的可重用性差,重复代码多。同样可以在xml文件中配置Animations,这样做可维护性变高了,只不过不容易进行调试。

一、在xml中使用Animations步骤

1.在res文件夹下建立一个anim文件夹;

2.创建xml文件,并首先加入set标签,更改标签如下:

Xml代码
<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android"  
    android:interpolator="@android:anim/accelerate_interpolator">  
</set>  
3.在该标签当中加入rotatealphascale或者translate标签;
xml代码:
<alpha  
        android:fromAlpha="1.0"  
        android:toAlpha="0.0"  
        android:startOffset="500"  
        android:duration="500"/>  

4.在代码当中使用AnimationUtils当中装载xml文件,并生成Animation对象。因为AnimationAnimationSet的子类,所以向上转型,用Animation对象接收。
java代码:
//使用AnimationUtils装载动画配置文件  
Animation animation = AnimationUtils  
    .loadAnimation(AnimationActivity.this, R.anim.alpha);  
//启动动画  
image.startAnimation(animation);  

 二、完整代码

       目录结构:

 Main.xml
xml代码:
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    >  
    <LinearLayout  
        android:orientation="horizontal"  
        android:layout_height="wrap_content"   
        android:layout_width="wrap_content">  
        <Button   
            android:id="@+id/rotateButton"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="旋转"/>  
        <Button   
            android:id="@+id/scaleButton"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="缩放"/>  
        <Button   
            android:id="@+id/alphaButton"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="淡入淡出"/>  
        <Button   
            android:id="@+id/translateButton"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="移动"/>  
    </LinearLayout>  
    <LinearLayout  
        android:orientation="vertical"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent">  
        <ImageView   
            android:id="@+id/image"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_centerInParent="true"  
            android:src="@drawable/image"/>    
    </LinearLayout>  
</LinearLayout>  

Alpha.xml
xml代码:
<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android"  
    android:interpolator="@android:anim/accelerate_interpolator">  
    <!-- fromAlpha和toAlpha是起始透明度和结束时透明度 -->  
    <alpha  
        android:fromAlpha="1.0"  
        android:toAlpha="0.0"  
        android:startOffset="500"  
        android:duration="500"/>  
</set> 

Rotate.xml
xml代码:
<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android"  
    android:interpolator="@android:anim/accelerate_interpolator">  
    <!--  
        fromDegrees:开始的角度  
        toDegrees:结束的角度,+表示是正的  
        pivotX:用于设置旋转时的x轴坐标  
        例  
            1)当值为"50",表示使用绝对位置定位  
            2)当值为"50%",表示使用相对于控件本身定位  
            3)当值为"50%p",表示使用相对于控件的父控件定位  
        pivotY:用于设置旋转时的y轴坐标  
      -->  
    <rotate   
        android:fromDegrees="0"  
        android:toDegrees="+360"  
        android:pivotX="50%"  
        android:pivotY="50%"  
        android:duration="1000"/>  
</set>  

Translate.xml
xml代码:
<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android"  
    android:interpolator="@android:anim/accelerate_interpolator">  
    <!--  
            始x轴坐标  
            止x轴坐标  
            始y轴坐标  
            止y轴坐标  
      -->  
    <translate   
        android:fromXDelta="0%"  
        android:toXDelta="100%"  
        android:fromYDelta="0%"  
        android:toYDelta="100%"  
        android:duration="2000"/>  
</set> 

Scale.xml
xml代码:
<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android"  
    android:interpolator="@android:anim/accelerate_interpolator">  
   <!--  
        起始x轴坐标  
            止x轴坐标  
            始y轴坐标  
            止y轴坐标  
            轴的坐标  
            轴的坐标  
     -->  
   <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> 

AnimationActivity.java
Java代码:
package com.android.activity;  
import android.app.Activity;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.view.animation.Animation;  
import android.view.animation.AnimationUtils;  
import android.widget.Button;  
import android.widget.ImageView;  
public class AnimationActivity extends Activity {  
    private Button rotateButton = null;  
    private Button scaleButton = null;  
    private Button alphaButton = null;  
    private Button translateButton = null;  
    private ImageView image = null;  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
          
        rotateButton = (Button)findViewById(R.id.rotateButton);  
        scaleButton = (Button)findViewById(R.id.scaleButton);  
        alphaButton = (Button)findViewById(R.id.alphaButton);  
        translateButton = (Button)findViewById(R.id.translateButton);  
        image = (ImageView)findViewById(R.id.image);  
          
        rotateButton.setOnClickListener(new RotateButtonListener());  
        scaleButton.setOnClickListener(new ScaleButtonListener());  
        alphaButton.setOnClickListener(new AlphaButtonListener());  
        translateButton.setOnClickListener(  
             TranslateButtonListener());  
    }  
    class AlphaButtonListener implements OnClickListener{  
        public void onClick(View v) {  
            //使用AnimationUtils装载动画配置文件  
            Animation animation = AnimationUtils  
                .loadAnimation(AnimationActivity.this, R.anim.alpha);  
            //启动动画  
            image.startAnimation(animation);  
        }  
    }  
    class RotateButtonListener implements OnClickListener{  
        public void onClick(View v) {  
            Animation animation = AnimationUtils  
                .loadAnimation(AnimationActivity.this, R.anim.rotate);  
            image.startAnimation(animation);  
        }  
    }  
    class ScaleButtonListener implements OnClickListener{  
        public void onClick(View v) {  
            Animation animation = AnimationUtils  
                .loadAnimation(AnimationActivity.this, R.anim.scale);  
            image.startAnimation(animation);  
        }  
    }  
      
    class TranslateButtonListener implements OnClickListener{  
        public void onClick(View v) {  
            Animation animation = AnimationUtils  
            <span style="white-space: pre;">    </span>.loadAnimation(AnimationActivity.this, R.anim.translate);  
            image.startAnimation(animation);  
        }  
    }  
}  

 运行结果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值