Tap the moon to make it cycle through its phases.
Tap it again to stop it.
See
Drawable
Animation
and classes
ImageView
and
AnimationDrawable
.
For the
phases.xml
file and its
<animation-list>
element, see
Frame
animation.
MainActivity.java
R.java
creates the
int
variables
R.drawable.phases
and
R.drawable.phase0
,
R.drawable.phase1
,
R.drawable.phase2
,
etc.
app/res/layout/activity_main.xml:
a black
RelativeLayout
containing
an
ImageView
.
The
ImageView
has the
android:background
attribute.
app/res/drawable/phases.xml
contains a
frame
animation
consisting of an
<animation-list>
containing eight
<item>
s.
.png
files in the
app/res/drawable
folder
phase0.png
64 × 64 pixels
phase1.png
64 × 64 pixels
phase2.png
64 × 64 pixels
phase3.png
64 × 64 pixels
phase4.png
64 × 64 pixels
phase5.png
64 × 64 pixels
phase6.png
64 × 64 pixels
phase7.png
64 × 64 pixels
AndroidManifest.xml
build.gradle
(Module: app)
I wanted to give the
.png
files the minimal names
0.png
,
1.png
,
2.png
,
etc.
But I had to give them names starting with a letter or underscore
because a Java identifier (variable name)
has to start with one of those characters.
To copy the eight
.png
files into the
app/res/drawable
folder of your project,
put the files on your Desktop.
On Mac, control-click on the first file,
phase0.png
,
and select Copy “phase0.png”.
Control-click on the
app/res/drawable
folder in the Android Studio
project
view,
select Paste,
and press the OK button.
Is there a way to copy all eight files into
drawable
at once?
To create the file
phases.xml
,
select the
app/res/drawable
folder in the
project
view
and pull down
File →
New →
Drawable resource file
New Resource File
File name: phases
OK
phases.xml
,
what happens if you change
android:oneshot
to
true
?
At what frame does the animation end?
android:background="@drawable/phases"
attribute from the
ImageView
element in the
activity_main.xml
file.
Replace it with the following statement in
onCreate
immediately after the
findViewById
.
imageView.setBackgroundResource(R.drawable.phases);
start
say that
start
cannot be called from
onCreate
.
Is this true?