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>

posted @ 2024-11-21 22:45  赵千万  阅读(4)  评论(0)    收藏  举报