【问题标题】:intent using images android eclipse使用图像 android eclipse 的意图
【发布时间】:2014-08-04 08:00:01
【问题描述】:

我正在构建一个游戏,游戏的目标是防止图像掉落,我想要发生的是当图像到达屏幕底部时,它会意图进行新的活动。请帮助。我设法让图像下降,我想知道的是当图像到达屏幕底部时如何触发意图。

package com.example.crashthetrash;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class Game extends Activity {

int posy;
int posy2;
int posy3;
int posy4;
int posy5;
int posy6;
int dur;
int dur2;

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

      dur = 5000;
      dur2 = dur - 10;
      final ImageView chips = (ImageView) findViewById(R.id.chips);

      final TranslateAnimation animation = new TranslateAnimation(0.0f, 0.0f, 0.0f,     500.0f);
      animation.setDuration(dur);
      animation.setRepeatCount(0);
      animation.setRepeatMode(0);
      animation.setFillAfter(true);
      chips.startAnimation(animation);


      chips.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub

              posy = chips.getTop();
              posy2 = 0 - posy;
              dur2 = dur2 - 10;

              TranslateAnimation animation = new TranslateAnimation(0.0f, 0.0f, posy2, 500.0f);
              animation.setDuration(dur2);
              animation.setRepeatCount(0);
              animation.setRepeatMode(0);
              animation.setFillAfter(true);
              chips.startAnimation(animation);

            return true;
        }
    });


      final ImageView fish = (ImageView) findViewById(R.id.fish);

      final TranslateAnimation animation2 = new TranslateAnimation(0.0f, 0.0f, 0.0f, 500.0f);
      animation2.setDuration(dur);
      animation2.setRepeatCount(0);
      animation2.setRepeatMode(0);
      animation2.setFillAfter(true);
      fish.startAnimation(animation2);

      fish.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                  posy3 = fish.getTop();
                  posy4 = 0 - posy;
                  dur2 = dur2 - 10;

                  TranslateAnimation animation2 = new TranslateAnimation(0.0f, 0.0f, posy4, 500.0f);
                  animation2.setDuration(dur2);
                  animation2.setRepeatCount(0);
                  animation2.setRepeatMode(0);
                  animation2.setFillAfter(true);
                  fish.startAnimation(animation2);
                return true;
            }
        });

      final ImageView paper = (ImageView) findViewById(R.id.paper);

      final TranslateAnimation animation3 = new TranslateAnimation(0.0f, 0.0f, 0.0f, 500.0f);
      animation3.setDuration(dur);
      animation3.setRepeatCount(0);
      animation3.setRepeatMode(0);
      animation3.setFillAfter(true);
      paper.startAnimation(animation3);


      paper.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                  posy5 = paper.getTop();
                  posy6 = 0 - posy;
                  dur2 = dur2 - 10;

                  TranslateAnimation animation3 = new TranslateAnimation(0.0f, 0.0f, posy6, 500.0f);
                  animation3.setDuration(dur2);
                  animation3.setRepeatCount(0);
                  animation3.setRepeatMode(0);
                  animation3.setFillAfter(true);
                  paper.startAnimation(animation3);
                return true;
            }
        });

      if(animation.hasEnded() || animation2.hasEnded() || animation3.hasEnded())
      {
         Intent intent = new Intent(Game.this, Restart.class);
         startActivity(intent);
      }

}

@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    return super.onTouchEvent(event);
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.game, menu);
    return true;
}





}

【问题讨论】:

  • 我认为如果您使用 Canvas 来创建动画而不是依赖 Android 内置的布局动画框架会更容易。
  • 先生,我该怎么做?我是 Eclipse 的新手,所以我不太熟悉。
  • 另外一件事,我可以单击图像,动画会重新启动,但是当图像从屏幕下降到一半时,我不能再单击它了。为什么会这样?

标签: android eclipse android-intent


【解决方案1】:
     if(animation.hasEnded() || animation2.hasEnded() || animation3.hasEnded())
  {
     Intent intent = new Intent(Game.this, Restart.class);
     startActivity(intent);
  }

应该在点击监听器之外。

【讨论】:

  • 它在触摸监听器之外。
  • 会触发下一个活动吗?
  • 它没有,什么都没有发生,当图像到达底部时,它就停在那里。
  • 鱼或纸有 .hasEnded() 方法吗?
  • 将 if(animation.hasEnded() || animation2.hasEnded() || animation3.hasEnded()) { Intent intent = new Intent(Game.this, Restart.class); startActivity(intent); } 更改为 if(fish.hasEnded() || paper.hasEnded()) { Intent intent = new Intent(Game.this, Restart.class); startActivity(intent); }
【解决方案2】:
if(animation.hasEnded())
{
   Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
   CurrentActivity.this.startActivity(myIntent);
}

【讨论】:

  • 我试过了,先生,但它不起作用,它没有错误,但是当我运行它时,图像一直在下降。
  • if(animation.hasEnded()) { Intent myIntent = new Intent(Game.this, Restart.class); Game.this.startActivity(myIntent); }
  • 嗯,我认为这两行 animation.setRepeatCount(Animation.INFINITE); animation.setRepeatMode(Animation.INFINITE); 是它一直运行的原因......
  • 我把它改成 1,它仍然没有意图。
  • if(animation.hasEnded() || animation2.hasEnded() || animation3.hasEnded()) { Intent change = new Intent(Game.this, Restart.class); startActivity(change); }
【解决方案3】:

TranslateAnimation 继承自动画对象并具有布尔值“hasEnded()”,因此您可以测试此值并触发您的新意图。

【讨论】:

  • 先生,我该怎么做?我是 Eclipse 的新手,所以我不太熟悉。谢谢。
  • 另外一件事,我可以单击图像,动画会重新启动,但是当图像从屏幕下降到一半时,我不能再单击它了。为什么会这样?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-02
相关资源
最近更新 更多