11.21
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/selected" android:oneshot="false">
<item android:drawable="@drawable/frame1" android:duration="100" />
<item android:drawable="@drawable/frame2" android:duration="100" />
<item android:drawable="@drawable/frame3" android:duration="100" />
<item android:drawable="@drawable/frame4" android:duration="100" />
<!-- Add more frames as needed -->
</animation-list>
package com.example.myapp;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private AnimationDrawable animationDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = findViewById(R.id.imageView);
imageView.setBackgroundResource(R.drawable.frame_animation);
animationDrawable = (AnimationDrawable) imageView.getBackground();
}
@Override
protected void onStart() {
super.onStart();
if (animationDrawable != null && !animationDrawable.isRunning()) {
animationDrawable.start();
}
}
@Override
protected void onStop() {
super.onStop();
if (animationDrawable != null && animationDrawable.isRunning()) {
animationDrawable.stop();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
本文来自博客园,作者:赵千万,转载请注明原文链接:https://www.cnblogs.com/zhaoqianwan/p/18177840
千万千万赵千万