基本的动画构成共有四种:透明动画/旋转动画/移动动画/缩放动画。

配置动画的方式有两种,一种是直接使用代码来配置动画效果,另一种是使用xml文档配置动画效果

相比而言,用xml文档写出来的动画效果,写一次可以很多次调用,但代码配置的话则每一次都需要重复配置过程。

具体使用代码:

//这是对一个按钮设置不同的动画
findViewById(R.id.animation).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//使用代码配置透明动画
AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);  //从0到1,代表从完全透明转变为完全不透明
alphaAnimation.setDuration(1000);                //这个变化的时间一共花费1秒
v.startAnimation(alphaAnimation);                //启动此animation
// 使用XML文档配置透明动画
v.startAnimation(AnimationUtils.loadAnimation(Main2Activity.this,R.anim.alphaanimation));
// 使用代码配置旋转动画
        //下面新建的旋转动画的意思是从0度旋转到360度,而旋转的中心点是相对于自身宽高的一半
RotateAnimation rotateAnimation = new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
rotateAnimation.setDuration(1000);
v.startAnimation(rotateAnimation);
// 使用XML文档配置旋转动画
v.startAnimation(AnimationUtils.loadAnimation(Main2Activity.this,R.anim.rotateanimation));
// 使用代码配置移动动画
TranslateAnimation translateAnimation = new TranslateAnimation(0,200,0,200);  //从(0,0)移动到(200,200)
translateAnimation.setDuration(1000);
v.startAnimation(translateAnimation);
// 使用代码配置移动动画
v.startAnimation(AnimationUtils.loadAnimation(Main2Activity.this,R.anim.translateanimation));
// 使用代码配置缩放动画
ScaleAnimation scaleAnimation = new ScaleAnimation(0,1,0,1,0,0);    //按钮的长和宽的百分比从0缩放到1,缩放的原点在(0,0)
scaleAnimation.setDuration(1000);
v.startAnimation(scaleAnimation);
// 使用XML文档配置缩放动画
v.startAnimation(AnimationUtils.loadAnimation(Main2Activity.this,R.anim.scaleanimation));
}
});

创建动画XML文档的方法:

1.在res目录文件下,新建XML文件,选择类型为animation

2.在该文件夹下新建xml文件,用来配置动画的相关信息

透明动画的xml配置文件:

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0"
android:toAlpha="1"
android:duration="1000">
</alpha>

旋转动画的xml配置文件:

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="1000"
android:pivotX="50%"
android:pivotY="50%">
</rotate>

移动动画的xml配置文件:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="200"
android:fromYDelta="0"
android:toYDelta="200"
android:duration="1000"> </translate>

缩放动画的xml配置文件:

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0"
android:toXScale="1"
android:fromYScale="0"
android:toYScale="1"
android:duration="1000"> </scale>

二、混合动画效果的实现:
1.新建一个animationSet容器,用来储存各种需要混合的动画效果数据

2.分别单独设计混合动画的各部分内容,然后将每种动画都添加进animationSet容器

3.通过startAnimation方法启动混合动画

实际代码:

AnimationSet animationSet = new AnimationSet(true);     //true容器内的每个动画都被使用

//透明动画
AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);
alphaAnimation.setDuration(1000);
animationSet.addAnimation(alphaAnimation);

//旋转动画
RotateAnimation rotateAnimation = new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
rotateAnimation.setDuration(1000);
animationSet.addAnimation(rotateAnimation);

//缩放动画
ScaleAnimation scaleAnimation = new ScaleAnimation(0,1,0,1,0,0);
scaleAnimation.setDuration(1000);
animationSet.addAnimation(scaleAnimation);
//以上的三种动画就是上面的简单动画的复用 v.startAnimation(animationSet);

使用XML文件配置混合动画的方法:

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

为动画设置监听事件:

动画的监听事件包含了三个时间的调用方法:动画开始,动画重复,动画结束。

实际代码:

Animation a = AnimationUtils.loadAnimation(Main2Activity.this,R.anim.multianimation);
a.setAnimationListener(new Animation.AnimationListener() {
  @Override
public void onAnimationStart(Animation animation) {
  Toast.makeText(getApplicationContext(),"动画开始",Toast.LENGTH_SHORT).show();
} @Override
public void onAnimationEnd(Animation animation) {
Toast.makeText(getApplicationContext(),"动画结束",Toast.LENGTH_SHORT).show();
} @Override
public void onAnimationRepeat(Animation animation) {
Toast.makeText(getApplicationContext(),"动画重复调用",Toast.LENGTH_SHORT).show();
}
});
v.startAnimation(a);

自定义动画的实现方法步骤:

1.自定义class,继承Animation

2.根据实际需要重写父方法,设计代码实现实际需求

3.通过类名调用(使用方法与系统动画一致)

实际代码:

import android.view.animation.Animation;
import android.view.animation.Transformation; /**
* Created by Andrew on 2016/7/27.
*/
public class CustomAnimation extends Animation { @Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
//这是一个可以获取动画控件的宽高和父容器的宽高的方法
super.initialize(width, height, parentWidth, parentHeight);
} //需要特别注意的是,此方法是会执行无数次,期间interpolatedTime会从0到1变化无数次,所有动画的实现基本都与这个数值的变化有关
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
// interpolatedTime 这是一个与动画运行周期相关的数值,在动画运行过程中,它会从0变成1
// t参数是接受变化的控件对象 //自定义动画效果:透明渐变
t.setAlpha(interpolatedTime); //由于interpolatedTime是从0变化到1的,将他的值设置进去技能实现透明度渐变 //自定义动画效果:X轴的晃动效果
t.getMatrix().setTranslate((float) (Math.sin(interpolatedTime)*50),0); super.applyTransformation(interpolatedTime, t);
}
}

UI设计篇·入门篇·简单动画的实现,透明动画/旋转动画/移动动画/缩放动画,混合动画效果的实现,为动画设置监听事件,自定义动画的方法的更多相关文章

  1. (原)android中的动画(三)之动画监听&页面切换动画

    1.动画也可以设置监听事件,例如在动画结束时需要执行某操作 把要执行的代码写在onAnimationEnd()回调方法中即可: anim.setAnimationListener(new Animat ...

  2. Android属性动画的监听事件

    整体很简单,直接上代码吧.activity_main.xml: <?xml version="1.0" encoding="utf-8"?> < ...

  3. Android 属性动画监听事件与一个菜单的例子

    简单监听事件 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 3 ...

  4. jquery监听事件on写法以及简单的拖拽效果

    引子——关于jquery的某些写法 我先不对监听事件做解释,我们先来看下jquery的一些写法吧!我们最常用的是jquery的css()方法,相信大家都会用! 假如用css设置一个属性,我们写法如下: ...

  5. 从零开始学Axure原型设计(入门篇)

    如果说Sketch是最美.最简洁的设计软件,那么Axure就是最强大的原型制作软件.Axure不仅能制作静态的视觉稿.页面,还能添加交互动作,是进行原型设计的最佳软件之一.虽然Axure的学习曲线比较 ...

  6. sencha touch 监听视图切换动画(animation)

    var animation = this.getLayout().getAnimation(); //添加监听 animation.on({ scope: this, animationend: 'o ...

  7. transitionend事件 监听 fadeIn fadeOut 两个方法无效(动画结束时无法执行transitionend里面的代码)

    //下面的例子证明 fadeIn() fadeOut() 不能使用transitionend事件进行监听. //说白了在fadeIn fadeOut 后面监听动画结束时,transitionend是不 ...

  8. UI设计篇·入门篇·简单动画的实现,为布局设置动画,用XML布置布局动画

    不仅仅控件可以设置动画,一个布局也可以设置动画, 当给一个布局设置了动画的时候,这个布局里所包含的控件都会依赖执行这些动画. 为布局设置动画的实现步骤: 1.新建一个动画,设置需要实现的形式 2.新建 ...

  9. UI设计篇·入门篇·绘制简单自定义矩形图/设置按钮按下弹起颜色变化/设置图形旋转

    Android的基本控件和图形有限,难以满足所有的实际需要和设计需求,好在Android给出了相对完善的图形绘制和自定义控件的API,利用这些API,可以基本满足设计的需求. 自定义图像和控件的方法: ...

随机推荐

  1. sqlmap的安装

    来自:http://www.51testing.com/html/89/n-3711589.html 一.下载 首先,需下载SqlMap以及适用于Windows系统的Python.下载地址如下: 1. ...

  2. Where 与 Having

    WHERE在数据分组前进行过滤,HAVING在数据分组后过滤. HAVING可以对检索(或计算)出的结果过滤,WHERE则不行. WHERE.聚合函数.HAVING在from后面的执行顺序:WHERE ...

  3. js中this最简单清晰的解释

    引自  https://www.cnblogs.com/huangwentian/p/6854472.html#commentform ① this指向的,永远只可能是对象!       ② this ...

  4. Visual Studio Code Java输出中文乱码的问题

    Visual Studio Code 推出了java插件,最近适用了一把,非常不错,但是有个很明显的bug.就是中文乱码,具体现象有如下: 1.System.out.println 控制台输出乱码. ...

  5. javax.el.PropertyNotFoundException: Property 'XXX' not found on type bean.XXXXX

    javax.el.PropertyNotFoundException: Property 'XXX' not found on type bean.XXXXX 先检查页面语法是否有问题,后在页面的el ...

  6. 如何在本地同时管理github仓库和codingnet仓库?

    本文的前提条件是你在电脑上接入了github或者gitlab的仓库,现在要接入codingnet的仓库. 电脑上已经有了 github 的 ssh key,怎么继续接入codingnet 的git仓库 ...

  7. css 小坑

    1.display:inline-block 内容上下移动 原因:inline-block 默认对齐方式是底部对齐 方法:加一个 vertical-align:top; 属性 把垂直对齐方式改为顶部

  8. python基础知识10---算法

    一.递归 程序本身自己调用自己称之为递归,类似于俄罗斯套娃,体现在代码中:用户执行最外(N)层函数,最外侧调用N-1层函数,N-1层函数调用N-2层函数... 利用函数编写如下数列: 斐波那契数列指的 ...

  9. 如何找出单链表中的倒数第k个元素

    方法一:快慢指针法 在查找过程中,设置两个指针,初始时指向首元结点(第一个元素结点). 然后,让其中一个指针先前移k步. 然后两个指针再同时往前移动.当先行的指针值为NULL时,另一个指针所指的位置就 ...

  10. 通俗易懂理解Linux文件权限修改chmod命令

    chmod g+w filename 给同组用户增加filename文件的写权限 chmod go+rw filename 给同组和组外用户增加写和读的权限 chmod g-w filename 给同 ...