Example #1
0
 @SuppressWarnings("deprecation")
 public GifAnimationDrawable(InputStream is, boolean inline) throws IOException {
   super();
   InputStream bis = is;
   if (!BufferedInputStream.class.isInstance(bis)) bis = new BufferedInputStream(is, 32768);
   decoded = false;
   mGifDecoder = new GifDecoder();
   mGifDecoder.read(bis);
   mTmpBitmap = mGifDecoder.getFrame(0);
   android.util.Log.v(
       "GifAnimationDrawable",
       "===>Lead frame: ["
           + width
           + "x"
           + height
           + "; "
           + mGifDecoder.getDelay(0)
           + ";"
           + mGifDecoder.getLoopCount()
           + "]");
   height = mTmpBitmap.getHeight();
   width = mTmpBitmap.getWidth();
   addFrame(new BitmapDrawable(mTmpBitmap), mGifDecoder.getDelay(0));
   setOneShot(mGifDecoder.getLoopCount() != 0);
   setVisible(true, true);
   if (inline) {
     loader.run();
   } else {
     new Thread(loader).start();
   }
 }
Example #2
0
  @Override
  protected void onDraw(Canvas canvas) {
    // super.onDraw(canvas);
    final Paint p = new Paint();

    Bitmap rebitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);

    canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), p);
    //
    if (decodeStatus == DECODE_STATUS_UNDECODE) {

      canvas.drawBitmap(bitmap, 0, 0, null);
      if (playFlag) {
        decode();
        invalidate();
      }
    } else if (decodeStatus == DECODE_STATUS_DECODING) {
      canvas.drawBitmap(bitmap, 0, 0, null);
      invalidate();
    } else if (decodeStatus == DECODE_STATUS_DECODED) {
      if (imageType == IMAGE_TYPE_STATIC) {
        canvas.drawBitmap(bitmap, 0, 0, null);
      } else if (imageType == IMAGE_TYPE_DYNAMIC) {
        if (playFlag) {
          long now = System.currentTimeMillis();

          if (time + decoder.getDelay(index) < now) {
            time += decoder.getDelay(index);
            incrementFrameIndex();
          }

          Bitmap bitmap = decoder.getFrame(index);

          if (bitmap != null) {
            canvas.drawBitmap(bitmap, 0, 0, null);
          }
          invalidate();
        } else {
          Bitmap bitmap = decoder.getFrame(index);
          canvas.drawBitmap(bitmap, 0, 0, null);
        }
      } else {
        canvas.drawBitmap(bitmap, 0, 0, null);
      }
    }
  }
Example #3
0
 /*
  * creates an int-array of frame delays in the gifDecoder object
  */
 private static int[] extractDelays(GifDecoder gifDecoder) {
   int n = gifDecoder.getFrameCount();
   int[] delays = new int[n];
   for (int i = 0; i < n; i++) {
     delays[i] = gifDecoder.getDelay(i); // display duration of frame in
     // milliseconds
   }
   return delays;
 }
Example #4
0
 public void run() {
   mGifDecoder.complete();
   int i, n = mGifDecoder.getFrameCount(), t;
   for (i = 1; i < n; i++) {
     mTmpBitmap = mGifDecoder.getFrame(i);
     t = mGifDecoder.getDelay(i);
     android.util.Log.v("GifAnimationDrawable", "===>Frame " + i + ": " + t + "]");
     addFrame(new BitmapDrawable(mTmpBitmap), t);
   }
   decoded = true;
   mGifDecoder = null;
 }
Example #5
0
 public void run() {
   final int n = mGifDecoder.getFrameCount();
   final int ntimes = mGifDecoder.getLoopCount();
   int repetitionCounter = 0;
   do {
     for (int i = 0; i < n; i++) {
       mTmpBitmap = mGifDecoder.getFrame(i);
       int t = mGifDecoder.getDelay(i);
       mHandler.post(mUpdateResults);
       try {
         Thread.sleep(t);
       } catch (InterruptedException e) {
         e.printStackTrace();
       }
     }
     if (ntimes != 0) {
       repetitionCounter++;
     }
   } while (mIsPlayingGif && (repetitionCounter <= ntimes));
 }
Example #6
0
  /** Load the images */
  private void loadImages() {
    GifDecoder decode = new GifDecoder();
    decode.read(file);
    int numFrames = decode.getFrameCount();
    if (numFrames > 0) {
      images = new GreenfootImage[numFrames];
      delay = new int[numFrames];
    } else {
      images = new GreenfootImage[1];
      images[0] = new GreenfootImage(1, 1);
    }

    for (int i = 0; i < numFrames; i++) {
      GreenfootImage image =
          new GreenfootImage(decode.getFrame(i).getWidth(), decode.getFrame(i).getHeight());
      BufferedImage frame = image.getAwtImage();
      Graphics2D g = (Graphics2D) frame.getGraphics();
      g.drawImage(decode.getFrame(i), null, 0, 0);
      delay[i] = decode.getDelay(i);
      images[i] = image;
    }
    time = System.currentTimeMillis();
  }