java - How to setListner to stop Animation and send callback to MainActivity Android -


how use call here check if animation completed or stopped giving me animationstoped listner null.

i want set buttons in main class when animation stopped giving me nullpointer. having hard time understand call

public interface animationstopchecklistner {     void callback(boolean result); }      public class animationscontainer implements animationstopchecklistner {     public int fps = 30;  // animation fps      // single instance procedures     private static animationscontainer minstance;     boolean endanim = false;      private animationscontainer() {     }      public static animationscontainer getinstance() {         if (minstance == null)             minstance = new animationscontainer();         return minstance;     }       public final int[] image_resources = {r.drawable.menu_000,             r.drawable.menu_0001, r.drawable.menu_0002, r.drawable.menu_0003,             r.drawable.menu_0004, r.drawable.menu_0005, r.drawable.menu_0006,             r.drawable.menu_0007, r.drawable.menu_0008,             r.drawable.menu_0009, r.drawable.menu_00010     };       /**      * @param imageview      * @return splash screen animation      */     public framessequenceanimation createsplashanim(imageview imageview) {         return new framessequenceanimation(imageview, image_resources, fps);     }      @override     public void callback(boolean result) {      }      /**      * animationplayer. plays animation frames sequence in loop      */      public class framessequenceanimation {         private int[] mframes; // animation frames         private int mindex; // current frame         private boolean mshouldrun; // true if animation should continue running. used stop animation         private boolean misrunning; // true if animation running. prevents starting animation twice         private softreference<imageview> msoftreferenceimageview; // used prevent holding imageview when should dead.         private handler mhandler;         private int mdelaymillis;         private bitmap mbitmap = null;         context mcontext;         private bitmapfactory.options mbitmapoptions;         animationstopchecklistner animationstopchecklistner;           public void setonanimationframessequenceanimation(animationstopchecklistner listener) {             this.animationstopchecklistner = listener;             log.e("setinterface", "setting");         }          public framessequenceanimation(imageview imageview, int[] frames, int fps) {             mhandler = new handler();             mframes = frames;             mindex = -1;             msoftreferenceimageview = new softreference<>(imageview);             mshouldrun = false;             misrunning = false;             mdelaymillis = 100 / fps;              imageview.setimageresource(mframes[0]);              // use in place bitmap save gc work (when animation images same size & type)             if (build.version.sdk_int >= 11) {                 bitmap bmp = ((bitmapdrawable) imageview.getdrawable()).getbitmap();                 int width = bmp.getwidth();                 int height = bmp.getheight();                 bitmap.config config = bmp.getconfig();                 mbitmap = bitmap.createbitmap(width, height, config);                 mbitmapoptions = new bitmapfactory.options();                 // setup bitmap reuse options.                 mbitmapoptions.inbitmap = mbitmap;                 mbitmapoptions.inmutable = true;                 mbitmapoptions.insamplesize = 1;             }         }          private int getnext() {             mindex++;             if (mindex >= mframes.length)                 mindex = 0;              log.e("total frames", string.valueof(mindex));              if (mindex == 58) {                  animationscontainer.minstance.endanim = true;                 stop();                 setonanimationframessequenceanimation(animationstopchecklistner);                 if (animationstopchecklistner != null) {                     // null                     animationstopchecklistner.callback(true);                 }             }             return mframes[mindex];         }          /**          * starts animation          */         public synchronized void start() {             mshouldrun = true;             if (misrunning)                 return;              runnable runnable = new runnable() {                 @override                 public void run() {                     imageview imageview = msoftreferenceimageview.get();                     if (!mshouldrun || imageview == null) {                         misrunning = false;                           /*  if (monanimationstoppedlistener != null) {                                 monanimationstoppedlistener.onanimationstopped();                             }*/                         return;                     }                      misrunning = true;                     mhandler.postdelayed(this, mdelaymillis);                      if (imageview.isshown()) {                         int imageres = getnext();                         if (mbitmap != null) { // build.version.sdk_int >= 11                             bitmap bitmap = null;                             try {                                 bitmap = bitmapfactory.decoderesource(imageview.getresources(), imageres, mbitmapoptions);                             } catch (exception e) {                                 e.printstacktrace();                             }                             if (bitmap != null) {                                 imageview.setimagebitmap(bitmap);                             } else {                                 imageview.setimageresource(imageres);                                 mbitmap.recycle();                                 mbitmap = null;                             }                         } else {                             imageview.setimageresource(imageres);                         }                     }                 }             };              mhandler.post(runnable);          }          /**          * stops animation          */         public synchronized void stop() {             mshouldrun = false;          }     }  } 

listner null because may not setting mainactivity

try this.

mainactivty extends appcompatactivity implements animationstopchecklistner  

write in oncreate function

 animationscontainer.getinstance.setonanimationframesequenceanimation(this); 

and then

  public void callback(boolean result){     // stuff } 

Comments