我刚接触Android,正在尝试重新创建旧的iPhone游戏(骆驼或鸭),您可以在其中看到图片,并选择图片所描绘的动物。该代码适用于几张图片,然后针对屏幕上显示的任何图片,在onCreate方法中都会出现OutOfMemory错误。我看过其他问题,发现有一种位图方法(循环)可以解决问题,但我不知道如何实现它或它是否与我的问题有关。任何和所有帮助表示赞赏。
package com.example.ryan.llamaorduck;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.util.Random;
public class DuckScreen extends AppCompatActivity {
boolean isLlamaScreen = false; //false because this is the Duck screen
Intent intent;
int screen; // variable used just for finding the next random screen
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_duck_screen);
}
// user clicked on the llama button.
//method implementation decides if they are correct and either ends the
//game or moves them onto the next random page.
public void llamaClick(View view){
if (isLlamaScreen){
Random r = new Random();
screen = r.nextInt(2);
if (screen == 1){ //DUCK
intent = new Intent(DuckScreen.this,DuckScreen.class);
startActivity(intent);
} else{
intent = new Intent(DuckScreen.this,LlamaScreen.class);
startActivity(intent);
}
}else{
intent = new Intent(DuckScreen.this,GameOver.class);
startActivity(intent);
}
}
//user clicked on the button indicating that they think the animal is a
//duck. Same idea as method before, testing other button.
public void duckClick(View view){
if (!isLlamaScreen){
Random r = new Random();
screen = r.nextInt(2);
if (screen == 1){ //DUCK
intent = new Intent(DuckScreen.this,DuckScreen.class);
startActivity(intent);
} else{
intent = new Intent(DuckScreen.this,LlamaScreen.class);
startActivity(intent);
}
}else{
intent = new Intent(DuckScreen.this,GameOver.class);
startActivity(intent);
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ryan.llamaorduck.DuckScreen">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:src="@drawable/duck"
android:scaleType="centerCrop"
android:id="@+id/duck"
/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/duck"
android:orientation="vertical"
>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Duck"
android:textSize="30sp"
android:onClick="duckClick"
/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Llama"
android:textSize="30sp"
android:onClick="llamaClick"
/>
</RadioGroup>
</RelativeLayout>
最佳答案
如果不需要保留活动的历史记录,一种解决方案是在开始新活动时使用FLAG_ACTIVITY_CLEAR_TOP
标志。因此,您的代码如下所示:
package com.example.ryan.llamaorduck;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.util.Random;
public class DuckScreen extends AppCompatActivity {
boolean isLlamaScreen = false; //false because this is the Duck screen
Intent intent;
int screen; // variable used just for finding the next random screen
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_duck_screen);
}
// user clicked on the llama button.
//method implementation decides if they are correct and either ends the
//game or moves them onto the next random page.
public void llamaClick(View view){
if (isLlamaScreen){
Random r = new Random();
screen = r.nextInt(2);
if (screen == 1){ //DUCK
intent = new Intent(DuckScreen.this,DuckScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else{
intent = new Intent(DuckScreen.this,LlamaScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}else{
intent = new Intent(DuckScreen.this,GameOver.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
//user clicked on the button indicating that they think the animal is a
//duck. Same idea as method before, testing other button.
public void duckClick(View view){
if (!isLlamaScreen){
Random r = new Random();
screen = r.nextInt(2);
if (screen == 1){ //DUCK
intent = new Intent(DuckScreen.this,DuckScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else{
intent = new Intent(DuckScreen.this,LlamaScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}else{
intent = new Intent(DuckScreen.this,GameOver.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
}