在我的应用中,我有一张随机挑选的图像。图像出现在屏幕上,用户可以将其滑动到侧面。每当它是图像a
时,计数器应该计数,但事实并非如此。我的int getMyRandomResId的返回值是否在条件中错误,或者哪里出错?
public class MainActivity extends Activity
{
private ImageView imageView;
public int sco = 0;
float x1, x2;
float y1, y2;
final Random rand = new Random();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
score = (TextView)findViewById(R.id.textView1);
score.setText(String.valueOf(sco));
}
int getMyRandomResId()
{
int imag = rand.nextInt(4);
switch (imag)
{
case 0:
return R.drawable.a;
case 1:
return R.drawable.b;
case 2:
return R.drawable.c;
default:
return R.drawable.d;
}
}
public boolean onTouchEvent(MotionEvent touchevent)
{
final ViewPropertyAnimator animator = imageView.animate();
switch (touchevent.getAction())
{
case MotionEvent.ACTION_DOWN:
{
x1 = touchevent.getX();
y1 = touchevent.getY();
break;
}
case MotionEvent.ACTION_UP:
{
x2 = touchevent.getX();
y2 = touchevent.getY();
//if left to right sweep event on screen
if (x1 < x2 && (x2 - x1) >= (y1 - y2) && (x2 - x1) >= (y2 - y1))
{
if(getMyRandomResId() == R.drawable.a)
{
sco++;
score.setText(String.valueOf(sco));
}
animationRunning = true;
animator.translationX((imageView.getWidth()))
.setDuration(250)
.setListener(new AnimatorListenerAdapter()
{
@Override
public void onAnimationEnd(Animator animation)
{
imageView.setTranslationX(0);
imageView.setImageResource(getMyRandomResId());
animationRunning = false;
}
})
.start();
}
}
}
}
答案 0 :(得分:3)
每次创建Random rand = new Random();
时,它都从同一点开始。以下是documentation
如果使用相同的种子创建了两个Random实例,并且为每个实例创建了相同的方法调用序列,则它们将生成并返回相同的数字序列。
您应该只创建一次Random
类的实例。例如,像这样:
public class MainActivity extends Activity {
final Random random = new Random();
...
答案 1 :(得分:1)
我猜你错过了什么。
我已经测试了你发布的代码,它就像一个魅力。
MainActivity.java
public class MainActivity extends AppCompatActivity {
public int sco = 0;
float x1, x2;
float y1, y2;
private ImageView imageView;
@Override
public boolean onTouchEvent(MotionEvent touchevent) {
final ViewPropertyAnimator animator = imageView.animate();
switch (touchevent.getAction()) {
case MotionEvent.ACTION_DOWN: {
x1 = touchevent.getX();
y1 = touchevent.getY();
break;
}
case MotionEvent.ACTION_UP: {
x2 = touchevent.getX();
y2 = touchevent.getY();
//if left to right sweep event on screen
if (x1 < x2 && (x2 - x1) >= (y1 - y2) && (x2 - x1) >= (y2 - y1)) {
final int randomResId = getMyRandomResId();
Log.e("ResId: ", String.valueOf(randomResId));
if (randomResId == R.drawable.ic_assignment_late_black_24dp) {
sco++;
Log.e("NewScoId", String.valueOf(sco));
}
animator.translationX((imageView.getWidth()))
.setDuration(250)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
imageView.setTranslationX(0);
imageView.setImageResource(randomResId);
}
})
.start();
}
}
}
return true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.x);
}
int getMyRandomResId() {
Random rand = new Random();
int imag = rand.nextInt(4);
switch (imag) {
case 0:
return R.drawable.ic_assignment_late_black_24dp;
case 1:
return R.drawable.ic_android_black_24dp;
case 2:
return R.drawable.ic_battery_charging_30_black_24dp;
default:
return R.drawable.ic_exposure_neg_2_black_24dp;
}
}
}
activity_main.xml中
<RelativeLayout android:id="@+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<ImageView
android:id="@+id/x"
android:layout_width="200dp"
android:layout_height="200dp"
android:text="Hello World!"
app:srcCompat="@drawable/ic_android_black_24dp" />
</RelativeLayout>
使用使用create Vector资产wizzard创建的随机图像。 我的LogCat(在滑动时)看起来像这样:
E/ResId:: 2130837590
E/ResId:: 2130837590
E/ResId:: 2130837589
E/ResId:: 2130837588
E/NewScoId: 1
E/ResId:: 2130837587
E/ResId:: 2130837590
E/ResId:: 2130837588
E/NewScoId: 2
E/ResId:: 2130837587
E/ResId:: 2130837589
E/ResId:: 2130837588
E/NewScoId: 3
[...]
答案 2 :(得分:0)
getMyRandomResId()这个方法工作正常,虽然foxanna的建议也比较好,但是我做了一些改变,我已经测试了这个并且为我工作了
public class MainActivity extends Activity
{
private ImageView imageView;
public int sco = 0;
float x1, x2;
float y1, y2;
Random rand;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
score = (TextView)findViewById(R.id.textView1);
score.setText(String.valueOf(sco));
rand = new Random();
}
int getMyRandomResId()
{
int imag = rand.nextInt(4);
switch (imag)
{
case 0:
sco++;
return R.drawable.a;
case 1:
return R.drawable.b;
case 2:
return R.drawable.c;
default:
return R.drawable.d;
}
}
public boolean onTouchEvent(MotionEvent touchevent)
{
final ViewPropertyAnimator animator = imageView.animate();
switch (touchevent.getAction())
{
case MotionEvent.ACTION_DOWN:
{
x1 = touchevent.getX();
y1 = touchevent.getY();
break;
}
case MotionEvent.ACTION_UP:
{
x2 = touchevent.getX();
y2 = touchevent.getY();
//if left to right sweep event on screen
if (x1 < x2 && (x2 - x1) >= (y1 - y2) && (x2 - x1) >= (y2 - y1))
{
if(getMyRandomResId() == R.drawable.a)
{
score.setText(String.valueOf(sco));
}
animationRunning = true;
animator.translationX((imageView.getWidth()))
.setDuration(250)
.setListener(new AnimatorListenerAdapter()
{
@Override
public void onAnimationEnd(Animator animation)
{
imageView.setTranslationX(0);
imageView.setImageResource(getMyRandomResId());
animationRunning = false;
}
})
.start();
}
}
}
}
希望这有帮助。