动画仅在第一次触发

时间:2016-04-08 20:07:51

标签: android android-animation image-generation

我正在Android上创建一个应用程序,它会在点击一个按钮时生成两个可用图像中的一个,并且在1秒后,该图像应该逐渐消失,让用户可以选择再次单击该按钮。 / p>

问题是动画在按下第一个按钮时运行平稳(即生成图像然后消失),但是在第二个按钮按下时,图像就在那里,没有任何反应。我无法弄清楚原因。

require "import_data/smart_csv_parser"

class CsvFile < ActiveRecord::Base
  TwiceImportError = Class.new(StandardError)
  ReportBeforeImportError = Class.new(StandardError)

  belongs_to :user
  has_many :invalid_records, class_name: '::CsvFile::InvalidRecord', dependent: :destroy
  has_many :contacts, as: :import_source

  mount_uploader :file, CsvUploader

  attr_accessor :file_url

  def filename
    self[:file]
  end

  state_machine initial: :uploaded, use_transactions: false do
    state :processing
    state :imported

    event :start_import! do
      transition uploaded: :processing
    end
    after_transition :uploaded => :processing,  do: :parse_data!

    event :finish_import! do
      transition processing: :imported
    end
  end

  def import!(file_url=nil)
    if file_url.nil?
      file_url = Rails.env.development? ? file.path : file.url
    end

    self.file_url = file_url
    raise TwiceImportError, "cannot import same file twice" unless uploaded?

    start_import!
  end

  def import_failed?
    import_result[:error].present?
  end

  def send_report!
    raise ReportBeforeImportError, 'please #import! before reporting' unless imported?
    Mailer.delay.csv_import_report(self)
  end

  def save_import_error(exception)
    import_result[:error_class] = exception.class.to_s
    import_result[:error] = exception.message
    import_result[:backtrace] = exception.backtrace
    import_result_will_change!
    save(validate: false)
  end

  private

  def parse_data!
    binding.pry
    CsvParsingService.new(self).perform
  end

  def initialize(*args, &block)
    super(*args, &block) # NOTE: This *must* be called, otherwise states won't get initialized
  end

end

日志看起来像这样:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ImageView firstimage = (ImageView)findViewById(R.id.imageView2);
        final ImageView secondimage = (ImageView)findViewById(R.id.imageView1);
        final Button clickMe = (Button)findViewById(R.id.button);
        final TextView image_description = (TextView)findViewById(R.id.textView);
        image_description.setText("");

        final Animation fadeout = new AlphaAnimation(1,0);
        fadeout.setStartOffset(1000);
        fadeout.setDuration(1000);
        secondimage.setAnimation(fadeout);
        firstimage.setAnimation(fadeout);
        image_description.setAnimation(fadeout);
        secondimage.setVisibility(View.GONE);
        firstimage.setVisibility(View.GONE);
        image_description.setVisibility(View.GONE);
        clickMe.setVisibility(View.VISIBLE);    

        fadeout.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) { }

            @Override
            public void onAnimationEnd(Animation animation) {
                System.out.println("Animation block");
                secondimage.setVisibility(View.GONE);
                firstimage.setVisibility(View.GONE);
                image_description.setVisibility(View.GONE);
                clickMe.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) { }
        }); 

        clickMe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.out.println("Click block");
                Random r = new Random();
                int i = r.nextInt(2);
                clickMe.setVisibility(View.GONE);
                if(i == 0) {
                    secondimage.setVisibility(View.VISIBLE);
                    image_description.setText("LOOK it's a CAT");
                    image_description.setVisibility(View.VISIBLE);
                    secondimage.setAnimation(fadeout);
                    image_description.setAnimation(fadeout);
                } else {
                    firstimage.setVisibility(View.VISIBLE);
                    image_description.setText("LOOK it's a DOG");
                    image_description.setVisibility(View.VISIBLE);
                    firstimage.setAnimation(fadeout);
                    image_description.setAnimation(fadeout);
                }    
            }
        });    
    }

知道为什么代码在第二次点击后没有到达动画块?

2 个答案:

答案 0 :(得分:2)

好的。我能够解决自己的问题。

我替换了

secondimage.setAnimation(fadeout);

secondimage.startAnimation(fadeout);

这样,代码就能够到达onAnimationEnd函数。

答案 1 :(得分:0)

最简单的方法是为每个需要动画的视图创建/膨胀动画类型对象的新实例。正如你现在所做的那样,它试图重用同一个对象。