android 动画一 (帧动画FrameAnimation)
16lz
2021-01-26
结构如下:
main.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"><!-- 设置一个垂直线性布局 --><Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="开始" android:id="@+id/start"><!-- 声明一个Button控件 --> </Button><ImageView android:layout_width="fill_parent"android:layout_height="fill_parent" android:id="@+id/imageId" android:layout_gravity="center" android:src="@anim/picture_animation"><!--声明一个ImageView控件--> </ImageView></LinearLayout>
picture_animation.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/zzlx1" android:duration="80" /> <item android:drawable="@drawable/zzlx2" android:duration="80" /> <item android:drawable="@drawable/zzlx3" android:duration="80" /> <item android:drawable="@drawable/zzlx4" android:duration="80" /> <item android:drawable="@drawable/zzlx5" android:duration="80" /> <item android:drawable="@drawable/zzlx6" android:duration="80" /> <item android:drawable="@drawable/zzlx7" android:duration="80" /> <item android:drawable="@drawable/zzlx8" android:duration="80" /></animation-list>
FrameAnimActivity.java
package wyf.cjg;import android.app.Activity;import android.graphics.drawable.AnimationDrawable;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class FrameAnimActivity extends Activity { /** Called when the activity is first created. */private AnimationDrawable draw = null;//声明一AnimationDrawableButton start;//声明一个Button控件 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);//设置布局 ImageView imageView = (ImageView)findViewById(R.id.imageId);//获取布局文件中ImageView控件的引用 this.draw=(AnimationDrawable)imageView.getDrawable();//创建AnimationDrawable对象 start=(Button)findViewById(R.id.start);//获取按钮的引用 start.setOnClickListener(//添加监听器 new OnClickListener() { @Override public void onClick(View v) { startAnimation();//调用startAnimation开始动画 } }); } private void startAnimation()//开始动画的方法 { if(draw.isRunning())//如果正在运行 { draw.stop();//停止动画 start.setText("开始");//设置按钮显示文本 } else//如果处于停止状态 { draw.stop();//停止动画 draw.start();//重新开始 start.setText("停止");//设置文本显示内容 } }}
更多相关文章
- 聊聊 RN 中 Android(安卓)提供 View 的那些坑
- Android(安卓)自定义自由选择时间区间的日历控件
- android使用篇(四) 注解依赖注入IOC实现绑定控件
- android 自定义换件--数字图象时钟控件
- Android在线开发工具 App Inventor
- android:自定义圆环形百分比控件,代码简单,加载动画,代码详细说明
- android 相对布局 水平平分父控件容器
- android 打包自己的自定义组件成JAR包
- Android(安卓)fill_parent、wrap_content和match_parent的区别