`
frand_feng
  • 浏览: 19727 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Android animation学习笔记之view/drawable animation

 
阅读更多

   前一章中总结了android animation中property animation的知识和用法,这一章总结View animation和Drawable animation的有关知识:

    View animation就是网上常讲的tweened animation,android.view.animation包包括了tweened animation用到的所有的类.

     不论是xml的动画还是android code编写的动画,都可以定义一个连续播放的动画.建议用xml文件动画,因为它更容易完成,更容易重运用,更容易修改.    xml动画文件放在res/anim文件夹当中.这个文件必须有一个跟元素(<alpha/scale/translate/rotateinterpolator element/set>),默认所有的动画是同时进行的,为了让他们一个接一个进行,你可以定义startoffset属性来控制,就像下边的代码一样:

<set android:shareInterpolator="false">
    <scale
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromXScale="1.0"
        android:toXScale="1.4"
        android:fromYScale="1.0"
        android:toYScale="0.6"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="false"
        android:duration="700" />
    <set android:interpolator="@android:anim/decelerate_interpolator">
        <scale
           android:fromXScale="1.4"
           android:toXScale="0.0"
           android:fromYScale="0.6"
           android:toYScale="0.0"
           android:pivotX="50%"
           android:pivotY="50%"
           android:startOffset="700"
           android:duration="400"
           android:fillBefore="false" />
        <rotate
           android:fromDegrees="0"
           android:toDegrees="-45"
           android:toYScale="0.0"
           android:pivotX="50%"
           android:pivotY="50%"
           android:startOffset="700"
           android:duration="400" />
    </set>
</set>

     屏幕坐标为左上角是(0,0),往下往右依次增加. 有一些值,比如pivoitX可以被赋值为相对值,比如50%,意思是相对于自己的50%.50意思是想对于父控件的50%. 也可以定义Interpolator,就是速度插入器,在上篇property animation中有详细介绍.

 

ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage); 
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump); 
spaceshipImage.startAnimation(hyperspaceJumpAnimation); 

 

也可以不调用startAnimation函数,直接定义:animation.setStarttime(),当到达那个时间点后,此动画会自动进行.

 

    Drawable Animation 可以帮助你将一个一个Drawable resources一个接一个的播放出来,就像传统的动画一样.AnimationDrawable类是Drawable animation的基础.

     Drawable Animation 虽然是动画,但是还是由Drawable resources组合而成的,所以其xml文件一般放在res/drawable,xml文件是由<animation-list>元素组成其根节点,<item>组成其框架,下面是例子:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
    <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
    <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
</animation-list>

 

android:oneshot="true"表示其只进行一次播放,然后停在最后一个drawable,oneshot被设为false的时候,它将会一直循环播放.下边是一个例子,当屏幕被触摸时,会将此动画加载在这个imageview上并且运行
AnimationDrawable rocketAnimation;
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
  rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
  rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
}
public boolean onTouchEvent(MotionEvent event) {
  if (event.getAction() == MotionEvent.ACTION_DOWN) {
    rocketAnimation.start();
    return true;
  }
  return super.onTouchEvent(event);
}

     在上面的例子中,我们可以看到,方法start()的调用不能在onCreate()方法中调用,因为要动画的view还未被添加到窗口当中,如果想立刻运行动画的话,你可以重写onWindowfocuschanged()方法,当此activity被放到最前端的时候自动执行.

分享到:
评论

相关推荐

    Android Animation示例(View Animation, Drawable Animation)

    View Animation Only animate View objects If animate non-view objects, you have to implement your own code takes less time to setup and requires less code to write Drawable Animation Load drawable ...

    博客《Android动画之一:Drawable Animation》附带源码

    &lt;animation-list xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:drawable="@drawable/frame1" android:duration="100" /&gt; &lt;item android:drawable="@drawable/frame2" android:...

    android动画之帧动画(drawable animation)和补间动画(view animation)

    &lt;animation-list xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:drawable="@drawable/earth1" android:duration="100" /&gt; &lt;item android:drawable="@drawable/earth2" android:...

    博客《Android动画之一:Drawable Animation》附带源码 SwipeWithAnim

    这篇博客《Android动画之一:Drawable Animation》深入探讨了如何使用Drawable Animation来实现平滑的界面过渡效果,同时附带了源码供读者实践和学习。 Drawable Animation基于Drawable对象,它可以是一组连续的...

    Android AnimationDemo_DrawableAnimation帧动画

    android:background="@drawable/animation_list" /&gt; ``` 4. **启动和控制动画**:在Java代码中,你需要获取到`AnimationDrawable`实例并启动或停止动画: ```java ImageView animationView = findViewById(R.id....

    android修改控件外观(使用drawable资源)

    &lt;item android:state_pressed="true" android:drawable="@drawable/button_pressed"/&gt; &lt;item android:state_focused="true" android:drawable="@drawable/button_focused"/&gt; &lt;item android:drawable="@drawable/...

    Android的Drawable学习Demo

    &lt;animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/anim_drawable"&gt; &lt;item android:drawable="@drawable/frame1" android:duration="100"/&gt; &lt;item android:drawable=...

    Android中的Animation的使用

    &lt;animation-list xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:drawable="@drawable/frame1" android:duration="100" /&gt; &lt;item android:drawable="@drawable/frame2" android:...

    android安卓APP之13种Drawable位图片资源.zip_android客户端是什么

    本资源包“android安卓APP之13种Drawable位图片资源.zip”涵盖了Android开发中常见的13种Drawable类型,这些类型极大地丰富了Android应用的视觉表现和动态效果。下面将详细介绍每种Drawable的特性和用法。 1. **...

    Android开发者学习笔记——View、Canvas、bitmap

    Android开发者学习笔记——View、Canvas、bitmap Android 开发者学习笔记——View、Canvas、bitmap 是 Android 开发中常用的类,本文将通过实例讲解 View、Canvas 等相关知识点。 从资源中获取位图 在 Android ...

    android帧动画,

    &lt;animation-list xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:drawable="@drawable/frame1" android:duration="100" /&gt; &lt;item android:drawable="@drawable/frame2" android:...

    Android animation_list demo,imageview实现动画效果

    &lt;animation-list xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:drawable="@drawable/frame1" android:duration="100" /&gt; &lt;item android:drawable="@drawable/frame2" android:...

    Drawable的基础demo

    &lt;animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/animation_drawable" android:oneshot="false"&gt; &lt;item android:drawable="@drawable/frame1" android:duration=...

    DrawableAnimation简单示例

    &lt;animation-list xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:drawable="@drawable/frame1" android:duration="100" /&gt; &lt;item android:drawable="@drawable/frame2" android:...

    Android Animation Demo

    通过`Android Animation Demo`,你可以亲自尝试和学习这些动画,加深对Android动画系统的理解,从而提升应用的视觉效果和用户体验。同时,参考提供的博客文章,会有更多实践经验和技巧等待你去探索。

    FrameAnimation

    android:background="@drawable/frameanimation" /&gt; ``` 4. **启动和控制动画**:在Activity或Fragment的代码中,获取`AnimationDrawable`实例并启动动画: ```java ImageView animationView = findViewById(R.id...

    Android Animation相关

    &lt;animation-list xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:drawable="@drawable/frame1" android:duration="100" /&gt; &lt;item android:drawable="@drawable/frame2" android:...

    Android 序列帧动画,开始,结束监听的解决Demo.zip

    android:background="@drawable/frame_animation" /&gt; ``` 5. **注意事项** - 确保所有的帧图片尺寸相同,以避免在播放动画时出现缩放或拉伸的问题。 - `duration`属性的值决定了动画的流畅度,数值越小,动画...

    android 自定义view drawable

    本篇将深入探讨如何在Android中自定义View并结合Drawable实现更丰富的界面效果。 首先,自定义View通常涉及以下几个步骤: 1. 创建一个新的Java类,继承自View或ViewGroup。例如,我们可以创建一个名为`...

    Android动画合集之帧动画

    android:src="@drawable/animation_list" /&gt; ``` 四、在代码中控制帧动画 3. 在Activity或Fragment的Java代码中,获取ImageView并启动或停止帧动画: ```java ImageView animationView = findViewById(R.id....

Global site tag (gtag.js) - Google Analytics