Frame Animation + button monitoring (enabling and disabling Windows)

Source: Internet
Author: User

Curtainactivity class:

Enable and close the current activity window

Package temp.com; import android. app. activity; import android. OS. bundle; import android. view. gravity; import android. view. view; import android. widget. button; import android. widget. imageview; import android. widget. linearlayout; import android. widget. toast; public class curtainactivity extends activity {private imageview windowimageview; // the window in the current operation is private int thiswindow = 1; private curtainoperationview curta Inview; private button okbutton; private button openbutton; private button closebutton; private button returnbutton; private Toast toast; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. curtainview); Final linearlayout linearlayoutcurtain = (linearlayout) findviewbyid (R. id. linearlayoutcurtain); // obtain the window control windowimageview = (IMA Geview) findviewbyid (R. id. windowimageview); // obtain the current window getthiswindow (); // return the Id 1 of the current window, which indicates the upper left, 2 indicates the upper right, 3 indicates the lower left, and 4 indicates the lower right. // set the window start interface setwindowface (); // curtain15 to open; curtain1 is disabled // set the animation setcurtainview (true) according to different curtain states; openbutton = (button) findviewbyid (R. id. openbutton); openbutton. setonclicklistener (new view. onclicklistener () {@ override public void onclick (view v) {system. out. println ("Listening to windows to be opened"); If (! Values. invalid wisopen [thiswindow]) {// update the window status updatewindowstate (true); setcurtainview (false); // create and set the window animation display mode curtainview linearlayoutcurtain. removeallviews (); linearlayoutcurtain. addview (curtainview); curtainview. show (true);} else {toast = toast. maketext (getapplicationcontext (), "the window has been opened! ", Toast. length_long); toast. setgravity (gravity. center, 0, 0); toast. setduration (1, 400); toast. show () ;}}); closebutton = (button) findviewbyid (R. id. closebutton); closebutton. setonclicklistener (new view. onclicklistener () {@ override public void onclick (view v) {system. out. println ("Listening to windows to be closed"); If (values. using wisopen [thiswindow]) {// update the window status updatewindowstate (false); setcurtainview (true); // create and set Set the window animation display mode to curtainview linearlayoutcurtain. removeallviews (); linearlayoutcurtain. addview (curtainview); curtainview. show (true);} else {toast = toast. maketext (getapplicationcontext (), "the window is closed! ", Toast. length_long); toast. setgravity (gravity. center, 0, 0); toast. setduration (1, 400); toast. show () ;}}); okbutton = (button) findviewbyid (R. id. okbutton); okbutton. setonclicklistener (new view. onclicklistener () {@ override public void onclick (view v) {system. out. println ("Listening to OK"); If (curtainview! = NULL) {// finish () ;}}); returnbutton = (button) findviewbyid (R. id. returnbutton); returnbutton. setonclicklistener (new view. onclicklistener () {@ override public void onclick (view v) {system. out. println ("listener return button"); // finish ();}});} // set the animation based on different curtain statuses. // If the animation is true or false, set it to public void setcurtainview (Boolean isopen) {curtainview = new curtainoperationview (this, isopen );} public void updatewindowstate (Boolean blean) {values. export wisopen [thiswindow] = blean ;} // obtain the window of the current operation // return the Id 1 of the current window, which indicates the upper left, 2 indicates the upper right, 3 indicates the lower left, and 4 indicates the lower right public int getthiswindow () {thiswindow = values. thisWindow-1. out. println ("this window is" + thiswindow); If (values. using wisopen [thiswindow]) system. out. println ("windows opened"); else {system. out. println ("windows closed") ;}; return thiswindow ;}// set the window start interface // curtain15 to open; curtain1 to close public void setwindowface () {If (values. windowisopen [thiswindow]) windowimageview. setimageresource (R. drawable. curtain15); else {windowimageview. setimageresource (R. drawable. curtain1 );}}}

Curtainoperationview class:

Package temp.com; import android. content. context; import android. graphics. canvas; import android. graphics. drawable. animationdrawable; import android. graphics. drawable. drawable; import android. view. view; public class curtainoperationview extends view {/* defines the animationdrawable animation */private animationdrawable frameanimation = NULL; Context mcontext = NULL;/* defines a drawable object */drawable mbitanimation = NULL; // Public curtainoperationview (context, Boolean isopen) {super (context); mcontext = context;/* instantiate the animationdrawable object */frameanimation = new animationdrawable (); /* load Resources * // here we use a loop to load all resources with the same name. // If (isopen) {// The window is open. Here the animation for (INT I = 15; I> = 1; I --) {int id = getresources () is closed (). getidentifier ("curtain" + I, "drawable", mcontext. getpackagename (); mbitanimati On = getresources (). getdrawable (ID);/* Add a frame for the animation * // The parameter mbitanimation is the image of the frame. // The parameter 300 is the display time of the frame, and frameanimation is calculated in milliseconds. addframe (mbitanimation, 300) ;}} else if (! Isopen) {// The window is closed. Here the animation for (INT I = 1; I <= 15; I ++) {int id = getresources () is opened (). getidentifier ("curtain" + I, "drawable", mcontext. getpackagename (); mbitanimation = getresources (). getdrawable (ID);/* Add a frame for the animation * // The parameter mbitanimation is the image of the frame. // The parameter 500 is the display time of the frame, and frameanimation is calculated in milliseconds. addframe (mbitanimation, 300) ;}/ * sets whether to loop in playback mode. False indicates loop, and true indicates no loop */frameanimation. setoneshot (true);/* set the animation to be displayed in this class */This. setbackgrounddrawable (frameanimation); // frameanimation. setvisible (true, false);/* start playing the animation */frameanimation. stop ();} public void ondraw (canvas) {super. ondraw (canvas);} public void show (Boolean ff) {If (FF)/* start playing the animation */frameanimation. start ();}}

Values class:

Package temp.com; public class values {public static int thiswindow = 1; // the ID of the control in the current operation // corresponds to nine curtains, true indicates that the curtain is open. False indicates that the curtain is closed. Public static Boolean disabled wisopen [] = {true, // Curtain 1 true, // Curtain 2 true, // Curtain 3 true, // curtain 4 true, // curtain 5 true, // curtain 6 true, // curtain 7 true, // curtain 8 true // curtain 9 };}

Curtainview. XML interface layout:

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: id = "@ + ID/curtain" Android: Background = "#000000"> <linearlayout Android: Id = "@ + ID/linearlayoutcurtain" Android: layout_width = "320dip" Android: layout_height = "320dip"> <imageview Android: Id = "@ + ID/windowimageview" Android: Lay Out_width = "320dip" Android: layout_height = "320dip"/> </linearlayout> <relativelayout Android: Id = "@ + ID/linearlayoutcurtainthesecond" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> <button Android: Id = "@ + ID/openbutton" Android: layout_width = "80dip" Android: layout_height = "wrap_content" Android: TEXT = "Open the window" Android: layout_aligntop = "@ ID/linearlayoutcurtainthesecond" Android: layo Ut_alignleft = "@ ID/linearlayoutcurtainthesecond"/> <button Android: Id = "@ + ID/closebutton" Android: layout_width = "80dip" Android: layout_height = "wrap_content" Android: TEXT = "Close Window" Android: layout_torightof = "@ ID/openbutton" Android: layout_aligntop = "@ ID/linearlayoutcurtainthesecond"/> <button Android: id = "@ + ID/okbutton" Android: layout_width = "80dip" Android: layout_height = "wrap_content" Android: text = "OK" Droid: layout_torightof = "@ ID/closebutton" Android: layout_aligntop = "@ ID/linearlayoutcurtainthesecond"/> <button Android: Id = "@ + ID/returnbutton" Android: layout_width = "80dip" Android: layout_height = "wrap_content" Android: text = "back" Android: layout_torightof = "@ ID/okbutton" Android: layout_aligntop = "@ ID/linearlayoutcurtainthesecond"/> </relativelayout> </linearlayout> <! -- Android: layout_abve: place the control on the given ID. the right edge of Android: layout_toleftof is aligned with the other left. Android: layout_torightof: Android: layout_alignbaseline baseline alignbaseline alignalalig: layout_alignbotton align Android: layout_alignleft align Android: layout_alignright align Android: layout_aligntop align Android: layout_alignparentbottom true, the bottom is aligned with the bottom of the parent control. When Android: Alibaba Android: layout_alignparentright Android: Alibaba Android: layout_centerhorizontal is true, it is placed in the center of the horizontal direction, placed in the center of the vertical direction -->

The window is displayed as follows:

The window is being closed:

When the window is closed:

// Download source code: (lost during source code upload)

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.