gpt4 book ai didi

android - Intent 使用图像android eclipse

转载 作者:行者123 更新时间:2023-11-29 16:00:17 27 4
gpt4 key购买 nike

我正在构建一个游戏,游戏的目标是防止图像掉落,我想要发生的是当图像到达屏幕底部时,它将开始一个新的 Activity 。请帮忙。我设法让图像落下,我想知道的是当图像到达屏幕底部时如何触发 Intent 。

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;
}





}

最佳答案

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

应该在点击监听器之外。

关于android - Intent 使用图像android eclipse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25114083/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com