利用animation-list做每隔几秒显示一张图片的动画效果

本文介绍如何在Android应用中使用animation-list组件创建一个每隔一定时间自动切换显示图片的动画效果。通过XML布局文件设置动画序列,然后在Java代码中控制动画的开始、停止和切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先贴上布局文件

<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/slider_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/animal1"
        android:contentDescription="@string/app_name"/>
    <Button 
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn1"
        android:onClick="showAnimation"/>
    
    <Button 
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn2"
        android:onClick="showAnimation"/>
    
    <Button 
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn3"
        android:onClick="showAnimation"/>


</LinearLayout>


在drawable文件夹下放置所需的图片

在drawable文件夹下建立animal1.xml  animal2.xml

animal1.xml内容如下

<?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/icon1" android:duration="150"></item>
<item android:drawable="@drawable/icon2" android:duration="150"></item>
<item android:drawable="@drawable/icon3" android:duration="150"></item>
<item android:drawable="@drawable/icon4" android:duration="150"></item>
<item android:drawable="@drawable/icon5" android:duration="150"></item>
<item android:drawable="@drawable/icon6" android:duration="150"></item>
</animation-list>

 android:oneshot 表示是否重复播放动画


animal2.xml内容如下

<?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/icon6" android:duration="150"></item>
<item android:drawable="@drawable/icon5" android:duration="150"></item>
<item android:drawable="@drawable/icon4" android:duration="150"></item>
<item android:drawable="@drawable/icon3" android:duration="150"></item>
<item android:drawable="@drawable/icon2" android:duration="150"></item>
<item android:drawable="@drawable/icon1" android:duration="150"></item>
</animation-list>


main.java内容如下

package com.example.animation;


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


public class MainActivity extends Activity {
private ImageView sliderImg;
private AnimationDrawable animationDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sliderImg = (ImageView)this.findViewById(R.id.slider_image);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

public void showAnimation(View v) {
int btnId = v.getId();
switch (btnId) {
case R.id.btn1:
sliderImg.setImageResource(R.drawable.animal1);
animationDrawable = (AnimationDrawable)sliderImg.getDrawable();
animationDrawable.start();
break;

case R.id.btn2:
animationDrawable = (AnimationDrawable)sliderImg.getDrawable();
animationDrawable.stop();
break;

case R.id.btn3:
sliderImg.setImageResource(R.drawable.animal2);
animationDrawable = (AnimationDrawable)sliderImg.getDrawable();
animationDrawable.start();
break;

default:
break;
}
}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值