Android三种动画的实现

1、帧动画

主要涉及到 AnimationDrawable 类。


(1)通过 XML 文件实现 
现在drawable 目录下定义好 XML 文件(假设文件名为xml_name):

<?xml version="1.0" encoding="utf-8"?>
<animation-list 
    android:oneshot="true"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/a_0"
        android:duration="100" />
    <item
        android:drawable="@drawable/a_1"
        android:duration="100" />
    <item
        android:drawable="@drawable/a_2"
        android:duration="100" />
</animation-list>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

(android:oneshot=”false” ,这个oneshot 的含义就是动画执行一次(true)还是循环执行多次。)

然后用Java代码开启:

ImageView imageview = (ImageView) findViewById(R.id.imageview);
imageview.setImageResource(R.drawable.xml_name);
AnimationDrawable animationDrawable = (AnimationDrawable) imageview.getDrawable();
animationDrawable.start();//animationDrawable.stop();
 
 
  • 1
  • 2
  • 3
  • 4

(2)只通过 Java 代码实现

AnimationDrawable animationDrawable = new AnimationDrawable();
int[] mipmaps = new int[]{R.drawable.a_0,R.drawable.a_1,R.drawable.a_2};
for (int i = 0; i < 3; i++) {
    int id=mipmaps[i];
    //或者使用下面方式,注意如果图片资源放在mipmap下时将drawable修改下
    //int id = getResources().getIdentifier("a_" + i, "drawable",getPackageName());
    Drawable drawable = getResources().getDrawable(id);
    animationDrawable.addFrame(drawable, 200);
}
animationDrawable.setOneShot(false);
imageView.setBackgroundDrawable(animationDrawable);
animationDrawable.start();
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

2、补间画

补间动画又可以分为四种形式,分别是 alpha(淡入淡出),translate(位移),scale(缩放大小),rotate(旋转)。

主要涉及到 Animation 类。


通过 XML 文件实现时需要在res/anim/ 文件夹下定义相关文件。 
可以是单独的以<alpha /><scale />等标签实现的单独的动画,也可以使用使用set 标签将多个动画组合

宁波整形美容医院http://www.iyestar.com/
宁波整容医院http://www.lyxcl.org/

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@[package:]anim/interpolator_resource"
    android:shareInterpolator=["true" | "false"] >
    <alpha
        android:fromAlpha="float"
        android:toAlpha="float" />
    <scale
        android:fromXScale="float"
        android:toXScale="float"
        android:fromYScale="float"
        android:toYScale="float"
        android:pivotX="float"
        android:pivotY="float" />
    <translate
        android:fromXDelta="float"
        android:toXDelta="float"
        android:fromYDelta="float"
        android:toYDelta="float" />
    <rotate
        android:fromDegrees="float"
        android:toDegrees="float"
        android:pivotX="float"
        android:pivotY="float" />
    <set>
        ...
    </set>
</set>
 
 
  • 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

有涉及到: 
Interpolator 主要作用是可以控制动画的变化速率 ,就是动画进行的快慢节奏。Android 系统已经为我们提供了一些Interpolator ,比如 accelerate_decelerate_interpolatoraccelerate_interpolator等。更多的interpolator 及其含义可以在Android SDK 中查看。同时这个Interpolator也是可以自定义的。

pivot 决定了当前动画执行的参考位置,这个属性主要是在translate 和 scale 动画中,这两种动画都牵扯到view 的“物理位置“发生变化,所以需要一个参考点。而pivotXpivotY就共同决定了这个点;它的值可以是float或者是百分比数值。

然后在 Java 代码中:

Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.xml_name);
img = (ImageView) findViewById(R.id.img);
img.startAnimation(animation);
 
 
  • 1
  • 2
  • 3

另外也可以直接使用 Java 代码实现。 
借助RotateAnimationAlphaAnimation等类。

具体请参考: 
https://lrh1993.gitbooks.io/android_interview_guide/content/android/basis/animator.html


3、属性动画

主要涉及到 ObjectAnimator 和 AnimatorSet 类。

ObjectAnimator anim = ObjectAnimator.ofFloat(myView, "rotation", 0f, 360f);
anim.setDuration(1000);
anim.start();
 
 
  • 1
  • 2
  • 3
ObjectAnimator anim = ObjectAnimator.ofFloat(myView, "alpha", 1.0f, 0.8f, 0.6f, 0.4f, 0.2f, 0.0f);
anim.setRepeatCount(-1);
anim.setRepeatMode(ObjectAnimator.REVERSE);
anim.setDuration(2000);
anim.start();
 
 
  • 1
  • 2
  • 3
  • 4
  • 5

属性动画也是可以组合实现的:

通过 AnimatorSet

ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(myView, "alpha", 1.0f, 0.5f, 0.8f, 1.0f);
ObjectAnimator scaleXAnim = ObjectAnimator.ofFloat(myView, "scaleX", 0.0f, 1.0f);
ObjectAnimator scaleYAnim = ObjectAnimator.ofFloat(myView, "scaleY", 0.0f, 2.0f);
ObjectAnimator rotateAnim = ObjectAnimator.ofFloat(myView, "rotation", 0, 360);
ObjectAnimator transXAnim = ObjectAnimator.ofFloat(myView, "translationX", 100, 400);
ObjectAnimator transYAnim = ObjectAnimator.ofFloat(myView, "tranlsationY", 100, 750);
AnimatorSet set = new AnimatorSet();
set.playTogether(alphaAnim, scaleXAnim, scaleYAnim, rotateAnim, transXAnim, transYAnim);
//set.playSequentially(alphaAnim, scaleXAnim, scaleYAnim, rotateAnim, transXAnim, transYAnim);
set.setDuration(3000);
set.start();
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

通过 PropertyValuesHolder 以及 ObjectAnimator.ofPropertyValuesHolder() 方法

PropertyValuesHolder translationX = PropertyValuesHolder.ofFloat("translationX", -200, -100, 100, 200, 300);
PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f, 2.0f);
PropertyValuesHolder rotate = PropertyValuesHolder.ofFloat("rotation", 0f, 360f);
PropertyValuesHolder rotationX = PropertyValuesHolder.ofFloat("rotationX", 0f, 180f);
ObjectAnimator together = ObjectAnimator.ofPropertyValuesHolder(imageView, translationX, rotate, scaleX, rotationX);
together.setDuration(3000);
together.start();
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

具体请参考: 
https://lrh1993.gitbooks.io/android_interview_guide/content/android/basis/animator.html

也可以通过 XML 文件实现:http://www.jianshu.com/p/87373134481b

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值