显示GIF图像

时间:2015-05-23 18:53:24

标签: android android-animation animated-gif

我有一个gif图像(3个点出现并逐个消失),我试图在我的启动画面上设置。如何在xml中只使用load-animation?以下是我的启动画面的代码。

public class MainActivity extends Activity {

    private static String TAG = MainActivity.class.getName();
    private static long SLEEP_TIME = 5; // Sleep for some time

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        // Start timer and launch main activity
        IntentLauncher launcher = new IntentLauncher();
        launcher.start();



    }

    private class IntentLauncher extends Thread {

        @Override
        /**
         * Sleep for some time and than start new activity.
         */
        public void run() {
            try {
                // Sleeping
                Thread.sleep(SLEEP_TIME*1000);
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }

            // Start main activity
            Intent intent = new Intent(MainActivity.this, Option.class);
            MainActivity.this.startActivity(intent);
            MainActivity.this.finish();
        }
    }

}

2 个答案:

答案 0 :(得分:0)

Android本身并不支持这种方式的GIF。要播放gif,您可以将其加载到WebView中或使用Movie类。

有一个有用的Ruby脚本(原始要点here),它将gif转换为animation-list xml文件。

#!/usr/bin/ruby
require 'fileutils'
require 'RMagick'
require 'slop'
require 'builder'

opts = Slop.new do
  banner "gif2animdraw [options]\n"
  on :i, :input=, 'path to animated GIF (or directory of GIFs)', :required => true
  on :o, :outdir=, 'path to root output directory', :required => true
  on :d, :density=, 'density name to use for frames', :required=>true
  on :oneshot, 'if set, animation does not repeat', :required => false, :default=>false
  help
end

begin
  opts.parse
rescue
  puts opts.help
  exit -1
end

if !['ldpi', 'mdpi', 'tvdpi', 'hdpi', 'xhdpi', 'xxhdpi', 'xxxhdpi'].include?(opts[:d])
  puts "Invalid density #{opts[:d]}"
  exit -1
end

glob=File.directory?(opts[:i]) ? File.join(opts[:i], '*.gif') : opts[:i]

Dir[glob].each do |gif|
  input=Magick::ImageList.new(gif).coalesce
  output=File.join(opts[:o], 'drawable')
  output_density=File.join(opts[:o], 'drawable-'+opts[:d])
  basename=File.basename(gif, '.gif').downcase.gsub(/\W/,'_')

  FileUtils.mkdir_p output
  FileUtils.mkdir_p output_density

  Dir.glob(File.join(output_density, basename+'*.png')).each {|f| File.delete(f) }

  input.write File.join(output_density, basename+'_%d.png')

  builder = Builder::XmlMarkup.new(:indent=>2)

  builder.tag!('animation-list',
                'xmlns:android'=>'http://schemas.android.com/apk/res/android',
                'android:oneshot'=>opts[:oneshot]) do
    i=0
    input.each do |frame|
      builder.item('android:drawable'=>"@drawable/#{basename}_#{i}",
                    'android:duration'=>frame.delay*1000/input.ticks_per_second)
      i+=1
    end
  end

  open(File.join(output, basename+'.xml'), 'w') do |f|
    f.puts builder.target!
  end
end

答案 1 :(得分:0)

我认为使用GIF对您的问题来说是一个相当重要的解决方案。您是否考虑使用WaitingDots等库。

作者使用自定义文本视图和文本范围来获得相同的结果。此解决方案将为您提供自定义动画的空间。