本文主要简单介绍补间动画使用代码实现,

关于使用xml实现补间动画,

可以参看:自定义控件三部曲之动画篇(一)——alpha、scale、translate、rotate、set的xml属性及用法

1. 由于这个View动画的逻辑很简单,我这里就直接先附上代码:

activity_main.xml:

 <RelativeLayout 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"
tools:context=".MainActivity" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="alpha"
android:text="alpha" /> <Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="scale"
android:text="scale" /> <Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="trans"
android:text="trans" /> <Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="rotate"
android:text="rotate" /> <Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="set"
android:text="set" />
</LinearLayout> <ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" /> </RelativeLayout>

接下来MainActivity.java:

package com.itheima.tweenanim;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView; public class MainActivity extends Activity {
private ImageView iv; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
} /**
* 透明度变化的动画
*
* @param view
*/
public void alpha(View view) {
// 声明动画 完全透明--》完全不透明
AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);
// 设置动画播放的时间
aa.setDuration(2000);
// 重复播放的次数
aa.setRepeatCount(2);
// 倒序播放
aa.setRepeatMode(Animation.REVERSE);
iv.startAnimation(aa);
} /**
* 缩放动画
*
* @param view
*/
public void scale(View view) {
ScaleAnimation sa = new ScaleAnimation(0.1f, 2.0f, 0.1f, 2.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
// 设置动画播放的时间
sa.setDuration(2000);
// 重复播放的次数
sa.setRepeatCount(2);
// 倒序播放
sa.setRepeatMode(Animation.REVERSE);
iv.startAnimation(sa);
} /**
* 位移动画
*
* @param view
*/
public void trans(View view) {
TranslateAnimation ta = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, -0.5f,
Animation.RELATIVE_TO_PARENT, 0.5f,
Animation.RELATIVE_TO_PARENT, -0.5f,
Animation.RELATIVE_TO_PARENT, 0.5f);
// 设置动画播放的时间
ta.setDuration(2000);
// 重复播放的次数
ta.setRepeatCount(2);
// 倒序播放
ta.setRepeatMode(Animation.REVERSE);
iv.startAnimation(ta);
} public void rotate(View view) {
RotateAnimation ra = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
0.0f);
// 设置动画播放的时间
ra.setDuration(2000);
// 重复播放的次数
ra.setRepeatCount(2);
// 倒序播放
ra.setRepeatMode(Animation.REVERSE);
iv.startAnimation(ra);
}
/**
* 动画集合
* @param view
*/
public void set(View view){
AnimationSet set = new AnimationSet(false);//每个动画时间变化的情况都是独立的
ScaleAnimation sa = new ScaleAnimation(0.1f, 2.0f, 0.1f, 2.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
// 设置动画播放的时间
sa.setDuration(2000);
// 重复播放的次数
sa.setRepeatCount(2);
// 倒序播放
sa.setRepeatMode(Animation.REVERSE);
TranslateAnimation ta = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, -0.2f,
Animation.RELATIVE_TO_PARENT, 0.2f,
Animation.RELATIVE_TO_PARENT, -0.2f,
Animation.RELATIVE_TO_PARENT, 0.2f);
// 设置动画播放的时间
ta.setDuration(2000);
// 重复播放的次数
ta.setRepeatCount(2);
// 倒序播放
ta.setRepeatMode(Animation.REVERSE);
RotateAnimation ra = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
0.0f);
// 设置动画播放的时间
ra.setDuration(2000);
// 重复播放的次数
ra.setRepeatCount(2);
// 倒序播放
ra.setRepeatMode(Animation.REVERSE);
set.addAnimation(ra);
set.addAnimation(ta);
set.addAnimation(sa);
iv.startAnimation(set);
}
}

 2. 构造方法解析

(1)AlphaAnimation 

AlphaAnimation 动画,窗口的动画效果,渐变透明度动画效果 ,有些游戏的欢迎动画,logo的淡入淡出效果就使用AlphaAnimation。

AlphaAnimation(float fromAlpha, float toAlpha);

浮点型值:

             fromAlpha 属性为动画起始时透明度

                 toAlpha  属性为动画结束时透明度

说明:

            0.0表示完全透明

           1.0表示完全不透明

以上值取0.0-1.0之间的float数据类型的数字

(2)ScaleAnimation

ScaleAnimation类是Android系统中的尺寸变化动画类,用于控制View对象的尺寸变化,该类继承于Animation类。ScaleAnimation类中的很多方法都与Animation类一致,该类中最常用的方法便是ScaleAnimation构造方法。

public ScaleAnimation (float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue);

参数说明

fromX:动画起始时,X坐标上的伸缩尺寸。              0.0表示收缩到没有

toX:动画结束时,X坐标上的伸缩尺寸 。                  1.0表示正常无伸缩

fromY:动画起始时,Y坐标上的伸缩尺寸 。            值小于1.0表示收缩

toY:动画结束时,Y坐标上的伸缩尺寸。                  值大于1.0表示放大

这4个参数确定从什么大小缩放到什么大小

pivotXType:动画在X轴相对于物件位置类型 (X轴的伸缩模式),可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。

pivotXValue:动画相对于物件的X坐标的开始位置。

pivotYType:动画在Y轴相对于物件位置类型 (Y轴的伸缩模式),可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。

pivotYValue:动画相对于物件的Y坐标的开始位置 。

这4个参数确定开始缩放的坐标,最后坐标是原来的坐标

有三种默认值:

RELATIVE_TO_PARENT 相对于父控件

RELATIVE_TO_SELF 相对于符自己

RELATIVE_TO_ABSOLUTE 绝对坐标

(3)TranslateAnimation 

在android动画中,最常用的一个莫不是TranslateAnimation了,这个类主要负责实现控件的动态位移,经常被用做指示器的移动动画。比如qq安卓客户端的指示器,如下图。

 

(4)RotateAnimation 

public RotateAnimation (float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue);

参数说明

fromDegrees:旋转的开始角度。

toDegrees:旋转的结束角度。

pivotXType:X轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。

pivotXValue:X坐标的伸缩值。

pivotYType:Y轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。

pivotYValue:Y坐标的伸缩值。

Android(java)学习笔记199:Android中补间动画(Tween Animation)的更多相关文章

  1. Android动画总结#补间动画(Tween Animation/View Animation) #帧动画(Frame Animation/Drawable Animation)#属性动画(PropertyAnimation)

    1.共有三种动画,英文名字多种叫法如下 第一种动画:补间动画(Tween Animation/View Animation) 四个:RotateAnimation旋转. AlphaAnimation透 ...

  2. Android开发UI之补间动画-Tween Animation

    Tween Animation-补间动画 官网链接-http://developer.android.com/reference/android/view/animation/Animation.ht ...

  3. android 补间动画和Animation

    介绍: 补间动画是一种设定动画开始状态.结束状态,其中间的变化由系统计算补充.这也是他叫做补间动画的原因. 补间动画由Animation类来实现具体效果,包括平移(TranslateAnimation ...

  4. Android(java)学习笔记142:Android中补间动画(Tween Animation)

    本文主要简单介绍补间动画使用代码实现, 关于使用xml实现补间动画, 可以参看:自定义控件三部曲之动画篇(一)——alpha.scale.translate.rotate.set的xml属性及用法 1 ...

  5. Android开发(26)--补间动画(Tween)的实现

    补间动画(Tween Animation) 补间动画与逐帧动画在本质上是不同的,逐帧动画通过连续播放图片来模拟动画的效果,而补间动画则是通过在两个关键帧之间补充渐变的动画效果来实现的.补间动画的优点是 ...

  6. Android开发学习笔记-关于Android的消息推送以及前后台切换

    下面是最简单的Android的消息推送的实现方法 package com.example.shownotic; import java.util.Random; import android.supp ...

  7. ListView的淡入淡出和Activity的淡入淡出补间动画效果Animation

    //=========主页面======================= package com.bw.lianxi7; import android.os.Bundle;import androi ...

  8. Java学习笔记——可视化Swing中JTable控件绑定SQL数据源的两种方法

    在 MyEclipse 的可视化 Swing 中,有 JTable 控件. JTable 用来显示和编辑常规二维单元表. 那么,如何将 数据库SQL中的数据绑定至JTable中呢? 在这里,提供两种方 ...

  9. Android基础笔记(十)- 帧动画、补间动画具体解释、对话框

    帧动画 补间动画Tween Animation 对话框以及面试中的注意点 帧动画 帧动画非常easy,我们首先看一下Google官方解释This is a traditional animation ...

随机推荐

  1. winfrom拷贝文件

    //File.Copy(@"C:\Users\Administrator\Pictures\bg.png", @"g:\images\bg.png", true ...

  2. 算术编码JM实现

    h.264标准中,CABAC的算术编码部分(9.3.4)只是一个参考,实际编码器中并不一定会按照它来实现,像JM中就有自己的算术编码实现方案. 在上篇文章CABAC中有详细的算术编码描述,在了解算术编 ...

  3. quartz定时任务时间配置

    quartz定时任务时间设置描述(2011-03-03 16:23:50)转载▼标签: quartz时间it 分类: 凌乱小记  这些星号由左到右按顺序代表 :     *    *     *    ...

  4. Array.asList()注意

    api: public static <T> List<T> asList(T... a) 返回一个受指定数组支持的固定大小的列表.(对返回列表的更改会“直接写”到数组.)此方 ...

  5. Android 正则表达式匹配汉字中文

    关于中文的正则表达式, 应该是^[\\u4E00-\\u9FFF]+$, 和论坛里常被人提起的^[\\u4E00-\\u9FA5]+$很接近需要注意的是论坛里说的^[\\u4E00-\\u9FA5]+ ...

  6. 【转】Android Fragment 基本介绍--不错

    原文网址:http://www.cnblogs.com/mengdd/archive/2013/01/08/2851368.html Fragment Android是在Android 3.0 (AP ...

  7. TimePicker,TimePickerDialog以及自定义timepicker(一)

    场景:在activity上点击,弹出一个dialog,然后点击dialog上的一个按钮,在弹出时间.以及自定义dialog 懒,要用到一个选择时间的需求,只要求小时和分钟,弹出式,第一时间想到了tim ...

  8. poj-1782 Run Length Encoding

    http://poj.org/problem?id=1782 Run Length Encoding Time Limit: 1000MS   Memory Limit: 30000K Total S ...

  9. oracle查询语句2【转载】

    本文使用的实例表结构与表的数据如下: scott.emp员工表结构如下:   SQL> DESC SCOTT.EMP; Name     Type         Nullable Defaul ...

  10. Bzoj 2718: [Violet 4]毕业旅行 && Bzoj 1143: [CTSC2008]祭祀river 传递闭包,二分图匹配,匈牙利,bitset

    1143: [CTSC2008]祭祀river Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1878  Solved: 937[Submit][St ...