Java Reference
In-Depth Information
Note
Don'tconfusewidgetinthiscontextwithwidgetsshownontheAndroidhome
screen. Although the same term isused,userinterface widgets andhome screen wid-
gets are different.
Continuing,
onCreate(Bundle)
executes
ImageView dukeImage =
(ImageView) findViewById(R.id.duke);
.Thisstatementfirstcalls
View
's
View findViewById(int id)
method to find the
an-
droid.widget.ImageView
element declared in
main.xml
and identified as
duke
,andinstantiate
ImageView
andinitialize ittoitsdeclarative information. The
statement then saves this object's reference in local variable
dukeImage
.
The subsequent
dukeImage.setBackgroundResource(R.drawable.duke_wave);
state-
ment invokes
ImageView
's inherited (from
View
)
void setBackgroundRe-
sourceMethod(int resID)
methodtosettheview'sbackgroundtotheresource
identified by
resID
. The
R.drawable.duke_wave
argument identifies an XML
file named
duke_wave.xml
(presented later), which stores information on the an-
imation, and which is stored in
res
's
drawable
subdirectory. The
setBack-
groundResource()
calllinksthe
dukeImage
viewtothesequenceofimagesde-
scribedby
duke_wave.xml
andthatwillbedrawnonthisview;theinitialimageis
drawn as a result of this method call.
ImageView
lets an app animate a sequence of drawables by calling
Anima-
tionDrawable
methods. Before the app can do this, it must obtain
ImageView
's
AnimationDrawable
. The
dukeAnimation = (AnimationDrawable)
dukeImage.getBackground();
assignmentstatementthatfollowsaccomplishes
this task by invoking
ImageView
's inherited (from
View
)
Drawable getBack-
ground()
methodtoreturnthe
AnimationDrawable
forthis
ImageView
,which
issubsequentlyassignedtothe
dukeAnimation
field.The
AnimationDrawable
instance is used to start and stop an animation (discussed shortly).
onCreate(Bundle)
nowturnsitsattentiontocreatingtheWavebutton.Itinvokes
findByViewId(int)
to obtain the button information from
main.xml
, and then
instantiate the
android.widget.Button
class.
The
View
class's nested
onClickListener
interface is then employed to create
a listener object whose
void onClick(View v)
method is invoked whenever
theuserclicksthebutton.Thelistener isregistered withits
Button
objectbycalling
View
's
void setOnClickListener(AdapterView.OnClickListener
listener)
method.