android 动画

1 补间动画

在assets/anim下新建补间动画的xml文件tween_ani.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0"
        android:duration="6000" />
    <scale android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromXScale="0.0" android:toXScale="1.0" android:fromYScale="0.0"
        android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%"
        android:duration="9000" />
    <translate android:fromXDelta="30" android:toXDelta="0"
        android:fromYDelta="30" android:toYDelta="0" android:duration="10000" />
    <rotate android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromDegrees="0" android:toDegrees="+360" android:pivotX="50%"
        android:pivotY="50%" android:duration="10000" />
</set>

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ImageView android:id="@+id/imageview" android:src="@drawable/img01"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" />
    <Button android:id="@+id/button" android:text="补间动画"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

在程序中加载动画并传给ImageView启动动画:

  button=(Button) findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                ImageView iv=(ImageView) findViewById(R.id.imageview);
                Animation animation=AnimationUtils.loadAnimation(mainActivity.this, R.anim.tween_ani);
                iv.startAnimation(animation);
            }});

imageimage

2 帧动画

在res/anim下新建帧动画xml文件frame_ani.xml,其中包括3个图像帧

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/img01" android:duration="500"
        android:visible="true" />
    <item android:drawable="@drawable/img02" android:duration="500"
        android:visible="true" />
    <item android:drawable="@drawable/img03" android:duration="500"
        android:visible="true" />
</animation-list>

在主函数中调用帧动画启动,注意AnimationDrawable对象的start方法不可以在Activity的onCreate方法中调用,因此如果需要程序在一开始就播放动画,则应该在onWindowFocusChanged()方法中调用。

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button=(Button) findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                ImageView iv=(ImageView) findViewById(R.id.imageview);
                iv.setBackgroundResource(R.anim.frame_ani);
                AnimationDrawable ad=(AnimationDrawable) iv.getBackground();
                ad.start();
            }});
    }

imageimage

posted @ 2011-04-13 12:28  朱旭东  阅读(1064)  评论(0编辑  收藏  举报