2016-05-25 9 views
0

アニメーションGIF画像用のカスタムImageViewがあります。私はGIFイメージを表示したい、私はしようとしたが、この場合、非同期でURLが含まれている代わりに、グライドを使用せずに生のフォルダからGIFイメージを表示したい。誰でもどのようにイメージを表示するか考えている?この問題を解決するためにguyzの助けてください! GIF画像をカスタムImageViewに設定する

は私がGIF画像を再生し、一時停止しなければならなかった

new GifStaticData() { 
      @Override 
      protected void onPostExecute(Resource drawable) { 
       super.onPostExecute(drawable); 
       gifImageView.setImageResource(R.raw.earth_tilt_animation); 
//    Log.d(TAG, "GIF width is " + gifImageView.getGifWidth()); 
       // Log.d(TAG, "GIF height is " + gifImageView.getGifHeight()); 
      } 

     }.execute(R.raw.earth_tilt_animation); 

GifStaticData.java

public class GifStaticData extends AsyncTask<Resource, Void, Resource> { 
    private static final String TAG = "GifDataDownloader"; 

    @Override protected Resource doInBackground(final Resource... params) { 
    final Resource gifUrl = params[0]; 

    if (gifUrl == null) 
     return null; 

    try { 
//  return ByteArrayHttpClient.get(gifUrl); 
     return gifUrl; 
    } catch (OutOfMemoryError e) { 
     Log.e(TAG, "GifDecode OOM: " + gifUrl, e); 
     return null; 
    } 
    } 
} 

GifImageView.java

public class GifImageView extends ImageView implements Runnable { 

    private static final String TAG = "GifDecoderView"; 
    private GifDecoder gifDecoder; 
    private Bitmap tmpBitmap; 
    private final Handler handler = new Handler(Looper.getMainLooper()); 
    private boolean animating; 
    private boolean shouldClear; 
    private Thread animationThread; 
    private OnFrameAvailable frameCallback = null; 
    private long framesDisplayDuration = -1L; 
    private OnAnimationStop animationStopCallback = null; 

    private final Runnable updateResults = new Runnable() { 
    @Override 
    public void run() { 
     if (tmpBitmap != null && !tmpBitmap.isRecycled()) { 
     setImageBitmap(tmpBitmap); 
     } 
    } 
    }; 

    private final Runnable cleanupRunnable = new Runnable() { 
    @Override 
    public void run() { 
     tmpBitmap = null; 
     gifDecoder = null; 
     animationThread = null; 
     shouldClear = false; 
    } 
    }; 

    public GifImageView(final Context context, final AttributeSet attrs) { 
    super(context, attrs); 
    } 

    public GifImageView(final Context context) { 
    super(context); 
    } 

    public void setBytes(final byte[] bytes) { 
    gifDecoder = new GifDecoder(); 
    try { 
     gifDecoder.read(bytes); 
     gifDecoder.advance(); 
    } catch (final OutOfMemoryError e) { 
     gifDecoder = null; 
     Log.e(TAG, e.getMessage(), e); 
     return; 
    } 

    if (canStart()) { 
     animationThread = new Thread(this); 
     animationThread.start(); 
    } 
    } 

    public long getFramesDisplayDuration() { 
    return framesDisplayDuration; 
    } 

    /** 
    * Sets custom display duration in milliseconds for the all frames. Should be called before {@link 
    * #startAnimation()} 
    * 
    * @param framesDisplayDuration Duration in milliseconds. Default value = -1, this property will 
    *        be ignored and default delay from gif file will be used. 
    */ 
    public void setFramesDisplayDuration(long framesDisplayDuration) { 
    this.framesDisplayDuration = framesDisplayDuration; 
    } 

    public void startAnimation() { 
    animating = true; 

    if (canStart()) { 
     animationThread = new Thread(this); 
     animationThread.start(); 
    } 
    } 

    public boolean isAnimating() { 
    return animating; 
    } 

    public void stopAnimation() { 
    animating = false; 

    if (animationThread != null) { 
     animationThread.interrupt(); 
     animationThread = null; 
    } 
    } 

    public void clear() { 
    animating = false; 
    shouldClear = true; 
    stopAnimation(); 
    handler.post(cleanupRunnable); 
    } 

    private boolean canStart() { 
    return animating && gifDecoder != null && animationThread == null; 
    } 

    public int getGifWidth() { 
    return gifDecoder.getWidth(); 
    } 

    public int getGifHeight() { 
    return gifDecoder.getHeight(); 
    } 

    @Override public void run() { 
    if (shouldClear) { 
     handler.post(cleanupRunnable); 
     return; 
    } 

    final int n = gifDecoder.getFrameCount(); 
    do { 
     for (int i = 0; i < n; i++) { 
     if (!animating) { 
      break; 
     } 
     //milliseconds spent on frame decode 
     long frameDecodeTime = 0; 
     try { 
      long before = System.nanoTime(); 
      tmpBitmap = gifDecoder.getNextFrame(); 
      frameDecodeTime = (System.nanoTime() - before)/1000000; 
      if (frameCallback != null) { 
      tmpBitmap = frameCallback.onFrameAvailable(tmpBitmap); 
      } 

      if (!animating) { 
      break; 
      } 
      handler.post(updateResults); 
     } catch (final ArrayIndexOutOfBoundsException | IllegalArgumentException e) { 
      Log.w(TAG, e); 
     } 
     if (!animating) { 
      break; 
     } 
     gifDecoder.advance(); 
     try { 
      int delay = gifDecoder.getNextDelay(); 
      // Sleep for frame duration minus time already spent on frame decode 
      // Actually we need next frame decode duration here, 
      // but I use previous frame time to make code more readable 
      delay -= frameDecodeTime; 
      if (delay > 0) { 
      Thread.sleep(framesDisplayDuration > 0 ? framesDisplayDuration : delay); 
      } 
     } catch (final Exception e) { 
      // suppress any exception 
      // it can be InterruptedException or IllegalArgumentException 
     } 
     } 
    } while (animating); 
    if (animationStopCallback != null) { 
     animationStopCallback.onAnimationStop(); 
    } 
    } 

    public OnFrameAvailable getOnFrameAvailable() { 
    return frameCallback; 
    } 

    public void setOnFrameAvailable(OnFrameAvailable frameProcessor) { 
    this.frameCallback = frameProcessor; 
    } 

    public interface OnFrameAvailable { 
    Bitmap onFrameAvailable(Bitmap bitmap); 
    } 

    public OnAnimationStop getOnAnimationStop() { 
    return animationStopCallback; 
    } 

    public void setOnAnimationStop(OnAnimationStop animationStop) { 
    this.animationStopCallback = animationStop; 
    } 

    public interface OnAnimationStop { 
    void onAnimationStop(); 
    } 

    @Override 
    protected void onDetachedFromWindow() { 
    super.onDetachedFromWindow(); 
    clear(); 
    } 
} 
+0

_Glide_は、あまりにも、それを使用したくない理由** GIF **アニメーションのサポートを提供します。 – Piyush

+0

私は知っているが、私は開始時にアニメーションを停止して、ビューをクリックするとグライドを使用できないようにする。 –

+0

開始および停止に関するgifイメージの考え方は、グライドが期待される –

答えて

0

設定RAWファイルのためにこれを試してみましたGlide - Cannot stop gif onClick- Getting TransitionDrawable instead of Animate/GifDrawable

アイデアは、それがGifdrawableのインスタンスであり、それを再生し、一時停止かどうかをチェックする、ビューから引き出し可能を取得することです。(GIF画像が既に再生されていることを望ん)

GifImageView

Drawable drawable = ((ImageView) v).getDrawable(); 
if (drawable instanceof GifDrawable) { 
     GifDrawable animatable = (GifDrawable) drawable; 
     if (animatable.isRunning()) { 
       animatable.stop(); 
     } else { 
       animatable.start(); 
     } 
} 
+0

おかげさまで試してみましょう! –

+0

申し訳ありませんが、私はそれを表示することはできません、私の更新されたコードについての任意のアイデアはありますか? –

+0

更新されたコードはどこにありますか?表示されません。 Glideを使用していますか? –

0

のクリック時にこれを追加します。私はGifMovieViewを使って上記の問題の解決策を見つけました!

GifMovieViewer.java

public class GifMovieViewer extends Activity { 
    private Button btnStart; 
    private GifMovieView gif1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.gif_movie_viewer); 

     gif1 = (GifMovieView) findViewById(R.id.gif1); 
     btnStart = (Button) findViewById(R.id.btnStart); 

     btnStart.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       gif1.setMovieResource(R.drawable.earth_tilt_animation); 
       //for pause 
       // gif1.setPaused(gif1.isPaused()); 
      } 
     }); 
    } 

    public void onGifClick(View v) { 
     GifMovieView gif = (GifMovieView) v; 
     gif.setPaused(!gif.isPaused()); 
    } 
} 
関連する問題