Android中的动画效果学习之二---Tween动画(使用XML文件进行配置动画效果)

           上一篇学习用硬编码直接进行对动画效果进行设置(Android中的动画效果学习之---Tween动画(硬编码方法创建)),今天学习一下使用XML文件进行配置动画效果:

            其中里面主要参数说明已经在上一篇讲过了,接下来进行设置动画的具体步骤:

            第一步:在项目的res文件下面新建一个文件夹名字是anim  【注意】这个名字必要要是anim

            第二步: 在anim文件夹下面,新建新的XMl文件 在xml文件中具体设置动画效果

            第三步: 在Activity中 使用AnimationUtils.loadAnimation(Demo_Animation_second.this, R.anim.xxxxx);进行获取

            

具体看下面的配置代码:

            base_alpha配置:

         

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

    <alpha
        android:duration="3000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />

</set>
              base_rotate配置:

  

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

    <rotate
        android:duration="3000"
        android:fromDegrees="0.0"
        android:pivotX="80%"
        android:pivotY="80%"
        android:toDegrees="180" />

</set>

            base_scale配置:

   

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

    <scale
        android:duration="3000"
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:pivotX="80%"
        android:pivotY="80%"
        android:toXScale="10.0"
        android:toYScale="10.0" />

</set>
              base_translate配置:

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


    <translate
        android:fromXDelta="0.0"
        android:fromYDelta="0.0"
        android:toXDelta="10.0"
        android:toYDelta="10.0" 
        android:duration="3000"/>


</set>
           
         

  Demo  Activity源代码如下:

               

package com.android.animation;

import com.android.R;

import android.app.Activity;
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.AnimationUtils;
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 Demo_Animation_second extends Activity {

	// 定义四个按钮,分别对用四种动画效果操作
	private Button alpha, roate, scale, translate;

	// 声明ImageView组件
	private ImageView animation_test;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.animation);

		animation_test = (ImageView) this.findViewById(R.id.test_animation);
		alpha = (Button) this.findViewById(R.id.alpha);
		roate = (Button) this.findViewById(R.id.roate);
		scale = (Button) this.findViewById(R.id.scale);
		translate = (Button) this.findViewById(R.id.translate);

		// 为按钮组件添加监听事件
		alpha.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				// 使用AnimationUitls工具类进行常见动画,使用到XML配置文件
				Animation alphaAnimation = AnimationUtils.loadAnimation(
						Demo_Animation_second.this, R.anim.base_alpha);
				// 为图片添加效果,开始动画
				animation_test.setAnimation(alphaAnimation);
			}
		});

		roate.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 使用AnimationUitls工具类进行常见动画,使用到XML配置文件
				Animation roateAnimation = AnimationUtils.loadAnimation(
						Demo_Animation_second.this, R.anim.base_rotate);
				roateAnimation.setDuration(3000);
				// 为图片添加效果,开始动画
				animation_test.setAnimation(roateAnimation);
			}
		});

		scale.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 使用AnimationUitls工具类进行常见动画,使用到XML配置文件
				Animation scaleAnimation = AnimationUtils.loadAnimation(
						Demo_Animation_second.this, R.anim.base_scale);
				// 为图片添加效果,开始动画
				animation_test.setAnimation(scaleAnimation);
			}
		});
		translate.setOnClickListener(new OnClickListener() {

			/**
			 * 位置变化
			 */
			@Override
			public void onClick(View v) {
				// 使用AnimationUitls工具类进行常见动画,使用到XML配置文件
				Animation translateAnimation = AnimationUtils.loadAnimation(
						Demo_Animation_second.this, R.anim.base_translate);
				// 为图片添加效果,开始动画
				animation_test.setAnimation(translateAnimation);
			}
		});
	}
}

   截图效果:




     上面两个截图分别是 alpha和rotate的效果

     下面两个截图分别是 scale 和translate的效果

     

   

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值