个性化阅读
专注于IT技术分析

android动画的例子

点击下载

Android为动画开发提供了大量的类和接口。大多数类和接口在android.animation包中提供。

Android动画使你可以在运行时更改对象属性和行为。有多种方法可以在android中制作动画。

AnimationDrawable类提供了开始和结束动画的方法。甚至,你也可以使用基于时间的动画。

让我们看一下android动画的简单示例。

activity_main.xml

你只需要查看。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <View
             />

</RelativeLayout>

仅具有图像视图。

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/anm"
     >

</ImageView>

MainActivity类

package com.srcmini.animation;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.widget.ImageView;

public class MainActivity extends Activity {

	ImageView anm;
	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.logo);
	        anm = (ImageView)findViewById(R.id.anm);
	        
	        anm.setBackgroundResource(R.drawable.animation);
		// the frame-by-frame animation defined as a xml file within the drawable folder
	        
	        /*
	         * NOTE: It's not possible to start the animation during the onCreate.
	         */
	    }
	 public void onWindowFocusChanged (boolean hasFocus) {
			super.onWindowFocusChanged(hasFocus);
			AnimationDrawable frameAnimation = 
				(AnimationDrawable) anm.getBackground();
			if(hasFocus) {
				frameAnimation.start();
			} else {
				frameAnimation.stop();
			}
		}

}

你需要在res / drawable-hdpi目录中创建animation.xml文件。

你需要有很多图像。在这里,我们使用14张图像,所有14张图像都位于res / drawable-mdpi目录中。

<?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/frame0" android:duration="120" />
	<item android:drawable="@drawable/frame1" android:duration="120" />
	<item android:drawable="@drawable/frame2" android:duration="120" />
	<item android:drawable="@drawable/frame3" android:duration="120" />
	<item android:drawable="@drawable/frame4" android:duration="120" />
	<item android:drawable="@drawable/frame5" android:duration="120" />
	<item android:drawable="@drawable/frame6" android:duration="120" />
	<item android:drawable="@drawable/frame7" android:duration="120" />
	<item android:drawable="@drawable/frame8" android:duration="120" />
	<item android:drawable="@drawable/frame9" android:duration="120" />
	<item android:drawable="@drawable/frame10" android:duration="120" />
	<item android:drawable="@drawable/frame11" android:duration="120" />
	<item android:drawable="@drawable/frame12" android:duration="120" />
	<item android:drawable="@drawable/frame13" android:duration="120" />
	<item android:drawable="@drawable/frame14" android:duration="120" />
	<item android:drawable="@drawable/frame14" android:duration="120" />
	<item android:drawable="@drawable/frame13" android:duration="120" />
	<item android:drawable="@drawable/frame12" android:duration="120" />
	<item android:drawable="@drawable/frame11" android:duration="120" />
	<item android:drawable="@drawable/frame10" android:duration="120" />
	<item android:drawable="@drawable/frame9" android:duration="120" />
	<item android:drawable="@drawable/frame8" android:duration="120" />
	<item android:drawable="@drawable/frame7" android:duration="120" />
	<item android:drawable="@drawable/frame6" android:duration="120" />
	<item android:drawable="@drawable/frame5" android:duration="120" />
	<item android:drawable="@drawable/frame4" android:duration="120" />
	<item android:drawable="@drawable/frame3" android:duration="120" />
	<item android:drawable="@drawable/frame2" android:duration="120" />
	<item android:drawable="@drawable/frame1" android:duration="120" />
	<item android:drawable="@drawable/frame0" android:duration="120" />
	
</animation-list>

输出:

赞(0)
未经允许不得转载:srcmini » android动画的例子

评论 抢沙发

评论前必须登录!