android基本动画,代码构建动画

使用代码构建android基本动画,基本动画有以下:

AlphaAnimation:渐变透明度动画

RotateAnimation:旋转动画

ScaleAnimation:伸缩动画

TranslateAnimation:平移动画

可以定义出一些常用的动画效果,也可以自定义一些动画,把参数预留出来。

可以定义一些组合动画效果,例如:从里到外的效果。对于组合的动画效果,使用基本的动画效果构建起来。

使用AnimationSet添加到此集合中,让其里面的动画一起起效果,可以看到意想不到的效果。

代码工具类

/**
 * 使用代码设计的动画
 *
 */
public class AnimationCodeUtils {
	private static Animation anim;

	/**
	 * 默认的是: 1.透明度从1.0~0, 2.动画时间3000毫秒 3.不停在动画结束状态
	 *
	 * @return 渐变透明度动画
	 */
	public static AlphaAnimation alphaAnimation1To0() {
		AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.0f);
		alpha.setDuration(3000);
		return alpha;
	}

	/**
	 * 默认的是: 1.透明度从0.0~1, 2.动画时间3000毫秒 3.不停在动画结束状态
	 *
	 * @return 渐变透明度动画
	 */
	public static AlphaAnimation alphaAnimation0To1() {
		AlphaAnimation alpha = new AlphaAnimation(0.0f, 1.0f);
		alpha.setDuration(3000);
		return alpha;
	}

	/**
	 * @param fromAlpha
	 *            开始透明度(0~1.0)
	 * @param toAlpha
	 *            结束透明度(0~1.0)
	 * @param durationMillis
	 *            动画执行时间
	 * @param fillAfter
	 *            是否停留在动画结束状态
	 * @return
	 */
	public static AlphaAnimation alphaAnimationCustom(float fromAlpha,
			float toAlpha, long durationMillis, boolean fillAfter) {
		AlphaAnimation alpha = new AlphaAnimation(fromAlpha, toAlpha);
		alpha.setDuration(durationMillis);
		alpha.setFillAfter(fillAfter);
		return alpha;
	}

	/**
	 * 旋转动画,顺时针360 默认:1.旋转角度:0~360 2.相对于自己的中心位置 3.旋转时间为30004.不停留在动画结束状态
	 *
	 * @return
	 */
	public static RotateAnimation rotateAnimation0to360() {
		RotateAnimation rotate = new RotateAnimation(0f, 360f,
				RotateAnimation.RELATIVE_TO_SELF, 0.5f,
				RotateAnimation.RELATIVE_TO_SELF, 0.5f);
		rotate.setDuration(3000);
		rotate.setFillAfter(false);
		return rotate;
	}

	/**
	 * 旋转动画,逆时针360 默认:1.旋转角度:360-0 2.相对于自己的中心位置 3.旋转时间为30004.不停留在动画结束状态
	 *
	 * @return
	 */
	public static RotateAnimation rotateAnimation360to0() {
		RotateAnimation rotate = new RotateAnimation(360f, 0f,
				RotateAnimation.RELATIVE_TO_SELF, 0.5f,
				RotateAnimation.RELATIVE_TO_SELF, 0.5f);
		rotate.setDuration(3000);
		rotate.setFillAfter(false);
		return rotate;
	}

	/**
	 *
	 * 旋转动画,自定义
	 *
	 * @return
	 */
	/**
	 * @param fromDegrees
	 *            开始旋转的角度
	 * @param toDegrees
	 *            结束的角度
	 * @param pivotXType
	 *            X方向相对位置类型
	 * @param pivotXValue
	 *            X方向相对位置的值
	 * @param pivotYType
	 *            Y方向相对位置类型
	 * @param pivotYValue
	 *            Y方向相对位置的值
	 * @param durationMillis
	 *            动画执行时间
	 * @param fillAfter
	 *            是否停留在动画结束时间
	 * @return
	 */
	public static RotateAnimation rotateAnimationCustom(float fromDegrees,
			float toDegrees, int pivotXType, float pivotXValue, int pivotYType,
			float pivotYValue, long durationMillis, boolean fillAfter) {
		RotateAnimation rotate = new RotateAnimation(fromDegrees, toDegrees,
				pivotXType, pivotXValue, pivotYType, pivotYValue);
		rotate.setDuration(durationMillis);
		rotate.setFillAfter(fillAfter);
		return rotate;
	}

	/**
	 * 伸缩动画 默认:相对于自己,向左上角缩小,从有到无,动画时间3000,不停留在动画结束状态
	 */
	public static ScaleAnimation scaleAnimation1to0() {
		ScaleAnimation scale = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f,
				ScaleAnimation.RELATIVE_TO_SELF, 0f,
				ScaleAnimation.RELATIVE_TO_SELF, 0f);
		scale.setDuration(3000);
		scale.setFillAfter(false);
		return scale;
	}

	/**
	 * 伸缩动画 默认:相对于自己,向左上角放大,从无到有,动画时间3000,不停留在动画结束状态
	 */
	public static ScaleAnimation scaleAnimation0to1() {
		ScaleAnimation scale = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
				ScaleAnimation.RELATIVE_TO_SELF, 0f,
				ScaleAnimation.RELATIVE_TO_SELF, 0f);
		scale.setDuration(3000);
		scale.setFillAfter(false);
		return scale;
	}

	/**
	 * 伸缩动画
	 */
	/**
	 * @param fromX
	 *            X方向开始缩放的角度(0-1) 0是不显示,1是全部显示
	 * @param toX
	 *            X方向结束缩放的角度(0-1) 0是不显示,1是全部显示
	 * @param fromY
	 *            Y方向开始缩放的角度(0-1) 0是不显示,1是全部显示
	 * @param toY
	 *            Y方向结束缩放的角度(0-1) 0是不显示,1是全部显示
	 * @param pivotXType
	 *            X方向相对类型
	 * @param pivotXValue
	 *            X方向相对于的值
	 * @param pivotYType
	 *            Y方向相对类型
	 * @param pivotYValue
	 *            Y方向相对于的值
	 * @param durationMillis
	 *            动画执行时间
	 * @param fillAfter
	 *            是否停留在动画结束的状态
	 * @return
	 */
	public static ScaleAnimation scaleAnimationCustom(float fromX, float toX,
			float fromY, float toY, int pivotXType, float pivotXValue,
			int pivotYType, float pivotYValue, long durationMillis,
			boolean fillAfter) {
		ScaleAnimation scale = new ScaleAnimation(fromX, toX, fromY, toY,
				pivotXType, pivotXValue, pivotYType, pivotYValue);
		scale.setDuration(durationMillis);
		scale.setFillAfter(fillAfter);
		return scale;
	}

	/**
	 * 平移动画:从左向右 平移
	 *
	 * @return
	 */
	public static TranslateAnimation translateAnimationLeftToRight() {
		TranslateAnimation translate = new TranslateAnimation(
				TranslateAnimation.RELATIVE_TO_SELF, 0.0f,
				TranslateAnimation.RELATIVE_TO_SELF, 1.0f,
				TranslateAnimation.RELATIVE_TO_SELF, 0.0f,
				TranslateAnimation.RELATIVE_TO_SELF, 0.0f);
		translate.setDuration(3000);
		translate.setFillAfter(false);
		return translate;
	}

	/**
	 * 平移动画:从右向左 平移
	 *
	 * @return
	 */
	public static TranslateAnimation translateAnimationRightToLeft() {
		TranslateAnimation translate = new TranslateAnimation(
				TranslateAnimation.RELATIVE_TO_SELF, 1.0f,
				TranslateAnimation.RELATIVE_TO_SELF, 0.0f,
				TranslateAnimation.RELATIVE_TO_SELF, 0.0f,
				TranslateAnimation.RELATIVE_TO_SELF, 0.0f);
		translate.setDuration(3000);
		translate.setFillAfter(false);
		return translate;
	}

	/**
	 * 平移动画:自定义
	 *
	 * @return
	 */
	/**
	 * @param fromXType
	 *            X方向开始平移相对类型
	 * @param fromXValue
	 *            X方向开始平移相对值
	 * @param toXType
	 *            X方向结束平移相对类型
	 * @param toXValue
	 *            X方向结束平移相对值
	 * @param fromYType
	 *            Y方向开始平移相对类型
	 * @param fromYValue
	 *            Y方向开始平移相对值
	 * @param toYType
	 *            Y方向结束平移相对类型
	 * @param toYValue
	 *            Y方向结束平移相对值
	 * @param durationMillis
	 *            动画执行时间
	 * @param fillAfter
	 *            是否停留在动画结束状态
	 * @return
	 */
	public static TranslateAnimation translateAnimationCustom(int fromXType,
			float fromXValue, int toXType, float toXValue, int fromYType,
			float fromYValue, int toYType, float toYValue, long durationMillis,
			boolean fillAfter) {
		TranslateAnimation translate = new TranslateAnimation(fromXType,
				fromXValue, toXType, toXValue, fromYType, fromYValue, toYType,
				toYValue);
		translate.setDuration(durationMillis);
		translate.setFillAfter(fillAfter);
		return translate;
	}

	/**
	 * 动画集合:渐变-缩放-旋转;效果,从里向外旋转放大
	 *
	 * @return
	 */
	public static Animation animationSet() {
		AnimationSet aniset = new AnimationSet(true);// true表示一起起作用
		aniset.addAnimation(alphaAnimation0To1());
		aniset.addAnimation(AnimationCodeUtils.scaleAnimationCustom(0f, 1f, 0f,
				1f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
				ScaleAnimation.RELATIVE_TO_SELF, 0.5f, 3000, false));
		aniset.addAnimation(rotateAnimation0to360());
		return aniset;
	}
}

源码下载:http://download.csdn.net/detail/forwardyzk/8317823

动画效果图:

时间: 2024-08-26 18:39:25

android基本动画,代码构建动画的相关文章

Android 应用启动动画代码

requestWindowFeature(Window.FEATURE_NO_TITLE);//设置无标题 setContentView(R.layout.activity_main); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏 ImageView welcomeImg = (ImageView) findVi

Android分别通过代码和xml实现动画效果

一.Android动画类型 Android的animation由四种类型组成: XML中 alph 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动画效果 rotate 画面转移旋转动画效果 JavaCode中 AlphaAnimation 渐变透明度动画效果 ScaleAnimation 渐变尺寸伸缩动画效果 TranslateAnimation 画面转换位置移动动画效果 RotateAnimation 画面转移旋转动画效果 二.Android动画模

android 后台代码设置动画

1.设置旋转动画 final RotateAnimation animation =new RotateAnimation(0f,360f,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF,0.5f); animation.setInterpolator(new LinearInterpolator()); // LinearInterpolator 表示均匀速率 animation.setDuration(3000);//设

Android页面的切换动画代码实现

1.项目Src下新建anim包 创建anim包,存放动画xml 2.下一步动画 位移动画 解释-100%p p:代表父窗体,100%:代表整个窗体,-:代码向左移动: 前一页面移出:tran_out.xml(自己创建的要选择translate) <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/re

Android攻城狮基础动画

Android基础动画 1. Tween Animation 变换动画 2. Frame Animation 帧动画 3. Layout Animation 布局动画 4. Property Animation 属性动画 Tween Animation(变换动画)在Android中又被分为四种: Alpha:渐变透明度动画 Scale:渐变尺寸缩放动画 Translate:位置移动动画 Rotate:旋转动画 Tween Animation共同属性: 1. Duration:动画持续时间(单位:

Android开发实战之补间动画和属性动画

说起动画,其实一点也不陌生,在使用一款app的时候为了优化用户体验,多多少少的,都会加入动画. 安卓中的动画,分为两大类:补间动画和属性动画.本篇博文会详细介绍总结这两大动画,希望本篇博文对你的学习和生活有所帮助. **补间动画** 补间动画分为四类:平移动画,旋转动画,缩放动画和渐变动画.这几类动画用法都差不多,只是对象参数不同这里我统一展示出来.以下是效果图: 实现代码很简单: btn1.setOnClickListener(new View.OnClickListener() { @Ove

Android编程之Fragment使用动画造成Unknown animation name: objectAnimator异常

在为Fragment做切换动画,启动后遇到了一个异常: Caused by: java.lang.RuntimeException: Unknown animation name: objectAnimator 截图如下: 我的代码如下: fragment = Fragment.instantiate(getActivity(), clz.getName()); fragment.setArguments(args); ft.setCustomAnimations(R.animator.frag

Android开发之自定义View-可动画展开收缩View的实现

有时候需要点击一个view可以动画展开和收缩折叠一个View这样的效果,这样就可以直接自定义View来实现. 本例中,采用继承FrameLayout来实现自定义的ExpandView.下面将详细介绍各个部分来实现该类以及如何使用该自定义视图. 效果图如下: 未展开效果: 正在向上折叠收缩中的效果: 已经展开效果: 自定义展开类:ExpandView的实现: package com.czm.customview; import android.content.Context; import and

Android应用开发:动画开发——XML动画

引言 当今,Android.IOS二分天下,什么Tizen.COS blabla的均为蝼蚁,一看就知道是为打发领导或为花研发资金产出的产品,根本不是为了赢得市场,为的只是博得领导一笑而已,完全可以忽视.而Android开发又因为开发语言以Java为主,入门门槛极低导致基本上是个程序员,泡两天EOE,或Android Developer Training都可以过来说"哥会开发Android app了!",那么什么才能将你的App脱颖而出呢?准确的用户痛点.良好的数据结构.简单易用的交互流