• category: swing folder: DisableAnimatedGif title: JLabelに表示したAnimated Gifのアニメーションを停止する tags: [JLabel, Animation, ImageIcon] author: aterai pubdate: 2013-02-25T00:50:43+09:00 description: JLabelに表示したAnimated GIFのアニメーションを停止します。 image: https://lh6.googleusercontent.com/-pYT15pLG7KY/USoyuJzLxUI/AAAAAAAABfo/JgO7-MbsL5U/s800/DisableAnimatedGif.png

概要

JLabelに表示したAnimated GIFのアニメーションを停止します。

サンプルコード

JLabel label2 = new JLabel() {
  @Override public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h) {
    if (!isEnabled()) {
      infoflags &= ~FRAMEBITS;
    }
    return super.imageUpdate(img, infoflags, x, y, w, h);
  }
};
View in GitHub: Java, Kotlin

解説

上記のサンプルでは、JLabel#isEnabled()falseの場合は、setIcon()メソッドで設定したAnimated Gifのアニメーションを停止するなどのテストを行っています。

  • Default
    • デフォルトのJLabelでは、JLabel#setEnabled(false);としてもアニメーションは停止しない
  • Override imageUpdate(...)
    • JLabel#imageUpdate(...)infoflagsからFRAMEBITSフラグを落とすことでアニメーションを停止
    • JLabelがリサイズされると?、コマが進んでしまう
  • setDisabledIcon
    • 別途用意した静止画像を使って、JLabel#setDisabledIcon(...)を設定
    • GrayFilter.createDisabledImage(Image)でアイコンをグレースケール化

参考リンク

コメント