准备六张wifi不同信号强度的素材图片,复制到drawable目录中
在drawable目录中新建frame文件,并编写代码
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/icon1"
android:duration="200" />
<item
android:drawable="@drawable/icon2"
android:duration="200" />
<item
android:drawable="@drawable/icon3"
android:duration="200" />
<item
android:drawable="@drawable/icon4"
android:duration="200" />
<item
android:drawable="@drawable/icon5"
android:duration="200" />
<item
android:drawable="@drawable/icon6"
android:duration="200" />
</animation-list>
在Activity中编写布局文件,添加Button和ImageView控件,Button添加onClick方法,ImageView添加src为 frame文件
<Button
android:onClick="signal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/startsignal"
/>
<ImageView
android:id="@+id/act_frame_signal_iv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/frame"
/>
初始化ImageView控件
ImageView signalIv = findViewById(R.id.act_frame_signal_iv);
public void signal(View view) {
//通过IV控件获取AnimationDrawable对象
AnimationDrawable ad = (AnimationDrawable) signalIv.getDrawable();
//判断动画是否在运行
if (!ad.isRunning()) {
ad.start();
} else {
ad.stop();
}
}