您的位置:首页 > 移动开发 > Android开发

Android入门笔记 - 界面开发 - Animation

2014-12-08 22:51 369 查看
今天我们来看看Android中的动画效果,实例比较简单:

AlphaAnimation:透明度动画

ScaleAnimation:缩放动画
TranslateAnimation:移动位置动画
RotateAnimation:旋转角度动画

先贴代码:

这个实例完全使用代码实现的,当然也可以使用xml文件实现,我们先来看这个实例:

package com.example.demo5_03_animation;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
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 mImg;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mImg = (ImageView) findViewById(R.id.img);
		
		Button btn_alpha = (Button) findViewById(R.id.btn_alpha);
		btn_alpha.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Animation animAlpha = new AlphaAnimation(1.0f, 0.0f);
				animAlpha.setDuration(1000*3);
				mImg.startAnimation(animAlpha);
			}
		});
		
		Button btn_scale = (Button) findViewById(R.id.btn_scale);
		btn_scale.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Animation animScale = new ScaleAnimation(1.0f, 1.3f, 1.0f, 1.3f);
				animScale.setDuration(1000*3);
				mImg.startAnimation(animScale);
			}
		});
		
		Button btn_translate = (Button) findViewById(R.id.btn_translate);
		btn_translate.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Animation animAlpha = new TranslateAnimation(-mImg.getWidth(), 0, 0, 0);
				animAlpha.setDuration(1000*1);
				mImg.startAnimation(animAlpha);
			}
		});
		
		Button btn_rotate = (Button) findViewById(R.id.btn_rotate);
		btn_rotate.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Animation animAlpha = new RotateAnimation(0.0f, 90.0f);
				animAlpha.setDuration(1000*3);
				mImg.startAnimation(animAlpha);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
也没有什么好讲的,这里主要是给大家提供实例。

然后我们来看看用xml文件怎么实现:

1. 使用xml来配置 animation, 首先需要在 res/ 下创建 anim/ 文件夹, 创建想要的动画配置文件,比如: res/anim/anim_alpha.xml

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

    <alpha
        android:duration="2000"
        android:fromAlpha="1.0"
        android:toAlpha="0.5" />

</set>


2. 然后就是在Activity中加载这个动画就完成了:

Button btn_alpha = (Button) findViewById(R.id.btn_alpha);
btn_alpha.setOnClickListener(new View.OnClickListener() {
	@Override
	public void onClick(View v) {
//		Animation animAlpha = new AlphaAnimation(1.0f, 0.0f);
//		animAlpha.setDuration(1000*3);
//		mImg.startAnimation(animAlpha);
		Animation animationAlpha = AnimationUtils.loadAnimation(mContext, R.anim.anim_alpha);
		mImg.startAnimation(animationAlpha);
	}
});
这里是把上一个例子中的透明度动画注释了,然后用xml加载方式写的。

好了,动画其实使用起来很简单,其他的请大家自己去研究!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: