package com.example.assignment7drawableexamples; import androidx.appcompat.app.AppCompatActivity; import android.graphics.drawable.AnimationDrawable; import android.graphics.drawable.TransitionDrawable; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.ToggleButton; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // runs the transition drawable final ImageView image = findViewById(R.id.imageView); final ToggleButton button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View v) { TransitionDrawable drawable = (TransitionDrawable) image.getDrawable(); if (button.isChecked()) { drawable.startTransition(500); } else { drawable.reverseTransition(500); } } }); // runs the animation drawable ImageView img = findViewById(R.id.imageViewAnimation); img.setBackgroundResource(R.drawable.animation); // Get the AnimationDrawable object. AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground(); // Start the animation (looped playback by default). frameAnimation.start(); } }