2016-08-01 7 views
1
senden

Wie Rückruf hier zu verwenden, um zu überprüfen, ob die Animation abgeschlossen oder gestoppt ist, gibt es mir AnimationStoped Listner als null.Wie SetListner Animation zu stoppen und Rückruf zu MainActivity Android

Ich möchte Schaltflächen in Meine Hauptklasse festlegen, wenn die Animation gestoppt ist, aber es gibt mir Nullpointer. Ich habe eine harte Zeit, diesen Aufruf zu verstehen zurück

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 the animation should continue running. Used to stop the animation 
     private boolean mIsRunning; // true if the animation currently running. prevents starting the animation twice 
     private SoftReference<ImageView> mSoftReferenceImageView; // Used to prevent holding ImageView when it should be 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 to save GC work (when animation images are the 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) { 
        // this is null 
        animationStopCheckListner.callback(true); 
       } 
      } 
      return mFrames[mIndex]; 
     } 

     /** 
     * Starts the 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) { // so 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 the animation 
     */ 
     public synchronized void stop() { 
      mShouldRun = false; 

     } 
    } 

} 
+0

listner ist null, weil Sie vielleicht nicht Ihre MainActivity Einstellung werden. – Abhishek

Antwort

1

listner null ist, weil Sie nicht einstellen werden können Ihre MainActivity

es so versuchen.

MainActivty extends AppCompatActivity implements AnimationStopCheckListner 

schreiben Sie es in Ihrer onCreate Funktion

AnimationsContainer.getInstance.setOnAnimationFrameSequenceAnimation(this); 

und dann

public void callback(boolean result){ 
    // do your stuff 
} 
+0

danke das hat perfekt funktioniert –

Verwandte Themen