Android之Animation动画使用技巧

转载请注明出处:http://blog.csdn.net/joker_ya/article/details/39315099

今天带给大家的是Animation动画的一些小技巧。使用动画有很多种方法,接下来我们要使用XML文件定义动画和代码定义动画两种方法来实现动画。

首先让大家了解一下XML定义动画的一些内容

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator" >
    <!--interpolator:
    	liner_interpolator 动画以均匀的速度改变(默认)
    	accelerate_interpolator 动画开始时较慢,然后加速
    	decelerate_interpolator 动画开始时较快,然后减速
    	cycle_interpolator 动画循环播放的次数   
    	accelerate_decelerate_interpolator 在动画开始、结束的地方比较慢,中间加速	
    -->

    <!-- alpha:提供透明化补间动画的功能, 对应的类为AlphaAnimation -->
    <alpha
        android:duration="2000"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />
    
    <!-- rotate:提供旋转补间动画的功能, 对应的类为RotateAnimation -->
    <rotate
        android:duration="2000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="-360" />
    
    <!-- scale:提供缩放补间动画的功能, 对应的类为ScaleAnimation -->
    <scale
        android:duration="2000"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.0"
        android:toYScale="0.0" />
    
    <!-- translate:提供位移补间动画的功能, 对应的类为TranslateAnimation -->    
    <translate />
    
    <!-- set:使用此标签可以将数个补间动画设置成同一群组一同播放,而且可以共享相同的设置,对应的类为AnimationSet -->
    <set>
    </set>

</set>

大致了解了如何用XML编写动画后。接下来就是新建名为AnimationDemo的工程项目,目录结构如下:


在res文件夹下新建一个名为anim的文件夹,我们接下来的动画XML文件就是存放在anim文件夹下。新建名为move.xml的文件,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator" >
    <alpha
        android:duration="2000"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />
    <rotate
        android:duration="2000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="-360" />   
    <scale
        android:duration="2000"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.0"
        android:toYScale="0.0" />

</set>

然后就是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"
    android:gravity="center"
    tools:context=".MainActivity" >
	<LinearLayout 
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:orientation="horizontal"
	    >
		<Button
		    android:id="@+id/show_xml" 
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content"
		    android:text="show by xml"	    
		    />
		<Button
		    android:id="@+id/show_code" 
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content"
		    android:text="show by code"	    
		    />
	</LinearLayout>
	<ImageView 
	    android:id="@+id/image"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:scaleType="fitXY"
	    android:src="@drawable/img"
	    />
    
</LinearLayout>

最后是MainActivity.java

package com.example.animationdemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.ImageView;

/**
 * 
 * @author Joker_Ya
 * 
 */
public class MainActivity extends Activity implements OnClickListener {

	private Button show_xml, show_code;
	private ImageView mImageView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 得到控件
		show_xml = (Button) findViewById(R.id.show_xml);
		show_code = (Button) findViewById(R.id.show_code);
		mImageView = (ImageView) findViewById(R.id.image);
		// 对按钮控件添加点击事件监听
		show_xml.setOnClickListener(this);
		show_code.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.show_xml:// 点击了“show_xml”按钮
			// 用AnimationUtils的loadAnimation()方法加载move.xml动画文件
			Animation mAnimation = AnimationUtils.loadAnimation(
					MainActivity.this, R.anim.move);
			// mAnimation.setFillAfter(true);//保持动画结束后的状态
			mImageView.startAnimation(mAnimation);// 开始动画
			break;
		case R.id.show_code:// 点击了“show_code”按钮
			// 使用ScaleAnimation类来编写缩放动画
			ScaleAnimation mScaleAnimation = new ScaleAnimation(1.0f, 0.0f,
					1.0f, 0.0f);
			// 设置动画持续时间为2秒
			mScaleAnimation.setDuration(2000);
			// mAnimation.setFillAfter(true);
			mImageView.startAnimation(mScaleAnimation);// 开始动画
			break;
		default:
			break;
		}
	}

}

本次内容比较简单,具体的讲解代码上的注释已经说的很清楚了,再此就不在赘述了。

最后就是看看效果了:


源码下载地址

源码下载

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值