android之帧动画

帧动画

在res下新建一个drawble文件夹下新建该xml文件
新建xml,名字随意,类型一定要选择anim....

anim.xml
注:图片自行放置,girl_…为我自己的图片名称


<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
 
    <!-- 使用Item包含每一张图 -->
 
    <item
        android:drawable="@drawable/girl_1"
        android:duration="200"/>
    <item
        android:drawable="@drawable/girl_2"
        android:duration="200"/>
    <item
        android:drawable="@drawable/girl_3"
        android:duration="200"/>
    <item
        android:drawable="@drawable/girl_4"
        android:duration="200"/>
    <item
        android:drawable="@drawable/girl_5"
        android:duration="200"/>
    <item
        android:drawable="@drawable/girl_6"
        android:duration="200"/>
    <item
        android:drawable="@drawable/girl_7"
        android:duration="200"/>
    <item
        android:drawable="@drawable/girl_8"
        android:duration="200"/>
    <item
        android:drawable="@drawable/girl_9"
        android:duration="200"/>
    <item
        android:drawable="@drawable/girl_10"
        android:duration="200"/>
    <item
        android:drawable="@drawable/girl_11"
        android:duration="200"/>
</animation-list>

布局代码:

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >
 
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:src="@drawable/anim" />
 
    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="btn1"
        android:text="播放" />
 
    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="btn2"
        android:text="暂停" />
 
</LinearLayout>

Java代码:

public class MainActivity extends Activity {
	private ImageView imageView;
	private AnimationDrawable animationDrawable;
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
 
		imageView = (ImageView) findViewById(R.id.imageView);
		// 找到需要播放的图片资源
		animationDrawable = (AnimationDrawable) imageView.getDrawable();
	}
 
	// 开启动画
	public void btn1(View view) {
		animationDrawable.start();
 
	}
 
	// 暂停动画
	public void btn2(View view) {
		animationDrawable.stop();
 
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中,动画是一种通过连续播放一系列预定义的图像来模拟动画效果的技术。在动画中,每个图像都被称为一,而整个动画则由这些组成。 以下是在Android中使用动画的基本步骤: 1. 定义动画资源:在res/drawable目录下创建一个XML文件来定义动画资源。在这个文件中,你需要指定每一所对应的图像资源,并设置动画的持续时间和重复次数等属性。 2. 加载动画资源:在Activity的代码中通过调用getResources().getDrawable()方法来加载动画资源。 3. 设置动画:将加载的动画资源设置到ImageView或其他View组件中,调用setBackgroudDrawable()方法即可。 4. 启动动画:通过调用AnimationDrawable的start()方法来启动动画。 以下是一个简单的示例: 1. 定义动画资源。在res/drawable目录下创建一个名为"animation.xml"的XML文件,内容如下: ``` <animation-list android:id="@+id/selected" android:oneshot="false"> <item android:drawable="@drawable/frame1" android:duration="50" /> <item android:drawable="@drawable/frame2" android:duration="50" /> <item android:drawable="@drawable/frame3" android:duration="50" /> <item android:drawable="@drawable/frame4" android:duration="50" /> </animation-list> ``` 2. 加载动画资源。在Activity的代码中通过调用getResources().getDrawable()方法来加载动画资源。 ``` AnimationDrawable animation = (AnimationDrawable) getResources().getDrawable(R.drawable.animation); ``` 3. 设置动画。将加载的动画资源设置到ImageView或其他View组件中,调用setBackgroudDrawable()方法即可。 ``` ImageView imageView = (ImageView) findViewById(R.id.image_view); imageView.setBackgroundDrawable(animation); ``` 4. 启动动画。通过调用AnimationDrawable的start()方法来启动动画。 ``` animation.start(); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值