android动画之补间动画
2016-05-31 15:54
459 查看
补间动画(移动补间动画,缩放补间动画,旋转补间动画,透明补间动画):
1.移动补间动画(TranslateAnimation):
指定移动的绝对位置(以自己的左上角为参照点):
TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, 0, 500);
translateAnimation.setDuration(5000);
imageview.startAnimation(translateAnimation);
2.指定移动的相对位置(相对父控件):
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT,
0.0f,
Animation.RELATIVE_TO_PARENT,
0.0f,
Animation.RELATIVE_TO_PARENT,
0.0f,
Animation.RELATIVE_TO_PARENT,
0.5f);
translateAnimation.setFillAfter(true);//停留在动画最后一帧,不指定的话,动画播放完毕会回到初始状态
translateAnimation.setDuration(5000);
imageview.startAnimation(translateAnimation);
3.指定移动的相对位置(相对自己)
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF,
0.0f,
Animation.RELATIVE_TO_SELF,
0.0f,
Animation.RELATIVE_TO_SELF,
0.0f,
Animation.RELATIVE_TO_SELF,
4.0f);
translateAnimation.setFillAfter(true);
translateAnimation.setDuration(5000);
imageview.startAnimation(translateAnimation);
4.在xml中定义动画:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="5000"
android:fromXDelta="0"
android:toXDelta="0"
android:fromYDelta="0"
android:toYDelta="200"
android:fillAfter="true">
</translate>
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="5000"
android:fromXDelta="0%"
android:toXDelta="0%"
android:fromYDelta="0%"
android:toYDelta="200%"
android:fillAfter="true">
</translate>
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="5000"
android:fromXDelta=“0%p”
android:toXDelta=“0%p”
android:fromYDelta=“0%p”
android:toYDelta="200%p”
android:fillAfter="true">
</translate>
TranslateAnimation translateAnimation = (TranslateAnimation) AnimationUtils.loadAnimation(this, R.anim.anim_translate);
imageview.startAnimation(translateAnimation);
2.缩放补间动画:
以自己的左上角为圆心缩放:
ScaleAnimation scaleAnimation = new ScaleAnimation(
1.0f,
1.5f,
1.0f,
1.5f);
scaleAnimation.setFillAfter(true);
scaleAnimation.setDuration(5000);
imageview.startAnimation(scaleAnimation);
xml:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1.0"
android:toXScale="1.5"
android:fromYScale="1.0"
android:toYScale="1.5"
>
</scale>
以自己的中心点为圆心进行缩放:
ScaleAnimation scaleAnimation = new ScaleAnimation(
1.0f,
1.5f,
1.0f,
1.5f,
Animation.RELATIVE_TO_SELF,
0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
scaleAnimation.setFillAfter(true);
scaleAnimation.setDuration(5000);
imageview.startAnimation(scaleAnimation);
xml:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1.0"
android:toXScale="1.5"
android:fromYScale="1.0"
android:toYScale="1.5"
android:pivotX="50%"
android:pivotY="50%" >
</scale>
以父控件为参照:
ScaleAnimation scaleAnimation = new ScaleAnimation(
1.0f,
1.5f,
1.0f,
1.5f,
Animation.RELATIVE_TO_PARENT,
0.5f,
Animation.RELATIVE_TO_PARENT,
0.5f);
scaleAnimation.setFillAfter(true);
scaleAnimation.setDuration(5000);
imageview.startAnimation(scaleAnimation);
xml:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1.0"
android:toXScale="1.5"
android:fromYScale="1.0"
android:toYScale="1.5"
android:pivotX="50%p"
android:pivotY="50%p" >
</scale>
如果imageview在父控件中间,上面两种设置的效果并不一样,后面这个中心点并不在父控件中心,它是以imageview的左上角为参照,偏移父控件宽高的一半,这时候,动画的中心点并不在父控件的中心
3.旋转补间动画
以左上角为原点旋转:
RotateAnimation rotateAnimation = new RotateAnimation(
0,
180);
rotateAnimation.setFillAfter(true);
rotateAnimation.setDuration(5000);
imageview.startAnimation(rotateAnimation);
以自己的中心为原点旋转:
RotateAnimation rotateAnimation =
new RotateAnimation(
0,
180,
Animation.RELATIVE_TO_SELF,
0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
rotateAnimation.setFillAfter(true);
rotateAnimation.setDuration(5000);
imageview.startAnimation(rotateAnimation);
4.透明补间动画:
Animation animation = new AlphaAnimation(1f,0.1f);
animation.setFillAfter(true);
animation.setDuration(5000);
imageview.startAnimation(animation);
5.多个动画同时进行:
AnimationSet set = new AnimationSet(false);
AlphaAnimation alphaAnimation = new AlphaAnimation(1f,0.1f);
alphaAnimation.setDuration(5000);
set.addAnimation(alphaAnimation);
TranslateAnimation translateAnimation = new TranslateAnimation(0,0,0,300);
translateAnimation.setDuration(5000);
set.addAnimation(translateAnimation);
set.setFillAfter(true);
imageview.startAnimation(set);
xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.5"
android:toYScale="1.5"
android:duration="5000" >
</scale>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="5000"
android:fromXDelta="100"
android:fromYDelta="0"
android:toXDelta="100"
android:toYDelta="300" >
</translate>
</set>
AnimationSet animation = (AnimationSet) AnimationUtils.loadAnimation(this, R.anim.anim_set);
animation.setFillAfter(true);
imageview.startAnimation(animation);
1.移动补间动画(TranslateAnimation):
指定移动的绝对位置(以自己的左上角为参照点):
TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, 0, 500);
translateAnimation.setDuration(5000);
imageview.startAnimation(translateAnimation);
2.指定移动的相对位置(相对父控件):
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT,
0.0f,
Animation.RELATIVE_TO_PARENT,
0.0f,
Animation.RELATIVE_TO_PARENT,
0.0f,
Animation.RELATIVE_TO_PARENT,
0.5f);
translateAnimation.setFillAfter(true);//停留在动画最后一帧,不指定的话,动画播放完毕会回到初始状态
translateAnimation.setDuration(5000);
imageview.startAnimation(translateAnimation);
3.指定移动的相对位置(相对自己)
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF,
0.0f,
Animation.RELATIVE_TO_SELF,
0.0f,
Animation.RELATIVE_TO_SELF,
0.0f,
Animation.RELATIVE_TO_SELF,
4.0f);
translateAnimation.setFillAfter(true);
translateAnimation.setDuration(5000);
imageview.startAnimation(translateAnimation);
4.在xml中定义动画:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="5000"
android:fromXDelta="0"
android:toXDelta="0"
android:fromYDelta="0"
android:toYDelta="200"
android:fillAfter="true">
</translate>
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="5000"
android:fromXDelta="0%"
android:toXDelta="0%"
android:fromYDelta="0%"
android:toYDelta="200%"
android:fillAfter="true">
</translate>
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="5000"
android:fromXDelta=“0%p”
android:toXDelta=“0%p”
android:fromYDelta=“0%p”
android:toYDelta="200%p”
android:fillAfter="true">
</translate>
TranslateAnimation translateAnimation = (TranslateAnimation) AnimationUtils.loadAnimation(this, R.anim.anim_translate);
imageview.startAnimation(translateAnimation);
2.缩放补间动画:
以自己的左上角为圆心缩放:
ScaleAnimation scaleAnimation = new ScaleAnimation(
1.0f,
1.5f,
1.0f,
1.5f);
scaleAnimation.setFillAfter(true);
scaleAnimation.setDuration(5000);
imageview.startAnimation(scaleAnimation);
xml:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1.0"
android:toXScale="1.5"
android:fromYScale="1.0"
android:toYScale="1.5"
>
</scale>
以自己的中心点为圆心进行缩放:
ScaleAnimation scaleAnimation = new ScaleAnimation(
1.0f,
1.5f,
1.0f,
1.5f,
Animation.RELATIVE_TO_SELF,
0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
scaleAnimation.setFillAfter(true);
scaleAnimation.setDuration(5000);
imageview.startAnimation(scaleAnimation);
xml:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1.0"
android:toXScale="1.5"
android:fromYScale="1.0"
android:toYScale="1.5"
android:pivotX="50%"
android:pivotY="50%" >
</scale>
以父控件为参照:
ScaleAnimation scaleAnimation = new ScaleAnimation(
1.0f,
1.5f,
1.0f,
1.5f,
Animation.RELATIVE_TO_PARENT,
0.5f,
Animation.RELATIVE_TO_PARENT,
0.5f);
scaleAnimation.setFillAfter(true);
scaleAnimation.setDuration(5000);
imageview.startAnimation(scaleAnimation);
xml:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1.0"
android:toXScale="1.5"
android:fromYScale="1.0"
android:toYScale="1.5"
android:pivotX="50%p"
android:pivotY="50%p" >
</scale>
如果imageview在父控件中间,上面两种设置的效果并不一样,后面这个中心点并不在父控件中心,它是以imageview的左上角为参照,偏移父控件宽高的一半,这时候,动画的中心点并不在父控件的中心
3.旋转补间动画
以左上角为原点旋转:
RotateAnimation rotateAnimation = new RotateAnimation(
0,
180);
rotateAnimation.setFillAfter(true);
rotateAnimation.setDuration(5000);
imageview.startAnimation(rotateAnimation);
以自己的中心为原点旋转:
RotateAnimation rotateAnimation =
new RotateAnimation(
0,
180,
Animation.RELATIVE_TO_SELF,
0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
rotateAnimation.setFillAfter(true);
rotateAnimation.setDuration(5000);
imageview.startAnimation(rotateAnimation);
4.透明补间动画:
Animation animation = new AlphaAnimation(1f,0.1f);
animation.setFillAfter(true);
animation.setDuration(5000);
imageview.startAnimation(animation);
5.多个动画同时进行:
AnimationSet set = new AnimationSet(false);
AlphaAnimation alphaAnimation = new AlphaAnimation(1f,0.1f);
alphaAnimation.setDuration(5000);
set.addAnimation(alphaAnimation);
TranslateAnimation translateAnimation = new TranslateAnimation(0,0,0,300);
translateAnimation.setDuration(5000);
set.addAnimation(translateAnimation);
set.setFillAfter(true);
imageview.startAnimation(set);
xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.5"
android:toYScale="1.5"
android:duration="5000" >
</scale>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="5000"
android:fromXDelta="100"
android:fromYDelta="0"
android:toXDelta="100"
android:toYDelta="300" >
</translate>
</set>
AnimationSet animation = (AnimationSet) AnimationUtils.loadAnimation(this, R.anim.anim_set);
animation.setFillAfter(true);
imageview.startAnimation(animation);
相关文章推荐
- Android序列化之Parcelable接口的用法
- android bug archive
- Android activity 详解二:Activity 跳转
- Android手机信号
- Android 内存分析工具leakcanary
- Android服务二 创建绑定服务
- Android Serializable Parcelable 序列化
- Android:TextView的垂直滚动效果,和上下滚动效果
- Android:TextView的垂直滚动效果,和上下滚动效果
- Android-Image-Loader 图片异步加载类库的使用超(详细配置)
- android 蓝牙SPP协议通信
- Android样式的开发:layer-list篇
- 【Android笔记】Android MediaPlayer的生命周期
- AndroidStudio使用设置
- Android系统启动-SystemServer下篇
- Android 内存泄露简介、典型情景及检测解决
- android解析PDF、XPS文件的第三方类库muPdf
- android LayoutInflater、setContentView、findviewbyid 区分解析
- android 游戏 实战 打飞机 实现主角以及主角相关元素(3)
- Android Invalid maximum heap size: -Xmx 解决方法