我正在开发的应用程序的部分代码必须在100张不同的卡片之间随机显示一张卡片。这是,当我触摸一个按钮时,我有100张图像,它必须选择一个并显示。
但是我有一些问题,我觉得这个问题很大。这是我的代码:
private final int rollAnimations = 50;
private final int delayTime = 15;
private final int[] cardFaces = new int[] {R.drawable.c1, R.drawable.c2, R.drawable.c3, R.drawable.c4, R.drawable.c5,
R.drawable.c6, R.drawable.c7, R.drawable.c8, R.drawable.c9, R.drawable.c10,
R.drawable.c10, R.drawable.c12, R.drawable.c13, R.drawable.c14, R.drawable.c5,
R.drawable.c16, R.drawable.c17, R.drawable.c18, R.drawable.c19, R.drawable.c20,
R.drawable.c21, R.drawable.c22, R.drawable.c23, R.drawable.c24, R.drawable.c25,
R.drawable.c26, R.drawable.c27, R.drawable.c28, R.drawable.c29, R.drawable.c30,
R.drawable.c31, R.drawable.c32, R.drawable.c33, R.drawable.c34, R.drawable.c35,
R.drawable.c36, R.drawable.c37, R.drawable.c38, R.drawable.c39, R.drawable.c40,
R.drawable.c41, R.drawable.c42, R.drawable.c43, R.drawable.c44, R.drawable.c45,
R.drawable.c46, R.drawable.c47, R.drawable.c48, R.drawable.c49, R.drawable.c50,
R.drawable.c51, R.drawable.c52, R.drawable.c53, R.drawable.c54, R.drawable.c55,
R.drawable.c56, R.drawable.c57, R.drawable.c58, R.drawable.c59, R.drawable.c60,
R.drawable.c61, R.drawable.c62, R.drawable.c63, R.drawable.c64, R.drawable.c65,
R.drawable.c66, R.drawable.c67, R.drawable.c68, R.drawable.c69, R.drawable.c70,
R.drawable.c71, R.drawable.c72, R.drawable.c73, R.drawable.c74, R.drawable.c75,
R.drawable.c76, R.drawable.c77, R.drawable.c78, R.drawable.c79, R.drawable.c80,
R.drawable.c81, R.drawable.c82, R.drawable.c83, R.drawable.c84, R.drawable.c85,
R.drawable.c86, R.drawable.c87, R.drawable.c88, R.drawable.c89, R.drawable.c90,
R.drawable.c91, R.drawable.c92, R.drawable.c93, R.drawable.c94, R.drawable.c95,
R.drawable.c96, R.drawable.c97, R.drawable.c98, R.drawable.c99, R.drawable.c100,};
private final Drawable mCard[] = new Drawable[100];
private final Random randomGen = new Random();
private final int roll[] = new int[] {100};
private ImageView card;
private LinearLayout cardContainer;
private Handler animationHandler;
private boolean paused = false;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.card_game);
/*Here I define each image resurce to a position of the array*/
for (int i = 0; i < 100; i++) {
mCard[i] = getResources().getDrawable(cardFaces[i]);
}
/*Instead of touching a button, I've modfied to touch the screen*/
cardContainer = (LinearLayout) findViewById(R.id.cardContainer);
cardContainer.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
rollCards();
} catch (Exception e) {};
}
});
animationHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
card.setImageDrawable(mCard[roll[0]]);
}
};
}
/******************
*
* METHODS
*
******************/
/**Thread that rolls the cards like if it would be a dice*/
private void rollCards() {
if (paused) return;
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < rollAnimations; i++) {
doRoll();
}
}
}).start();
MediaPlayer mp = MediaPlayer.create(this, R.raw.roll);
try {
mp.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
}
/***/
private void doRoll() {
roll[0] = randomGen.nextInt(100);
synchronized (getLayoutInflater()) {
animationHandler.sendEmptyMessage(0);
}
try { // delay to alloy for smooth animation
Thread.sleep(delayTime);
} catch (final InterruptedException e) {
e.printStackTrace();
}
}
这是显示logcat的内容:
05-23 15:50:31.458: E/AndroidRuntime(21778): FATAL EXCEPTION: main
05-23 15:50:31.458: E/AndroidRuntime(21778): java.lang.OutOfMemoryError: (Heap Size=49187KB, Allocated=48011KB)
05-23 15:50:31.458: E/AndroidRuntime(21778): at android.graphics.Bitmap.nativeCreateScaledBitmap(Native Method)
05-23 15:50:31.458: E/AndroidRuntime(21778): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:744)
05-23 15:50:31.458: E/AndroidRuntime(21778): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:766)
05-23 15:50:31.458: E/AndroidRuntime(21778): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:492)
05-23 15:50:31.458: E/AndroidRuntime(21778): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
05-23 15:50:31.458: E/AndroidRuntime(21778): at android.content.res.Resources.loadDrawable(Resources.java:2053)
05-23 15:50:31.458: E/AndroidRuntime(21778): at android.content.res.Resources.getDrawable(Resources.java:683)
05-23 15:50:31.458: E/AndroidRuntime(21778): at com.example.beding.CardActivity.onCreate(CardActivity.java:68)
05-23 15:50:31.458: E/AndroidRuntime(21778): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
cardActivity的第68行指的是:mCard[i] = getResources().getDrawable(cardFaces[i]);
答案 0 :(得分:1)
参考上述评论,这是我处理OutOfMemory问题的方法。
//Populate Reference Array
ImageEnum[] refArr = {ImageEnum.C1,ImageEnum.2......};
Constructor()
{
//For loop to loadRandom() 5 different values from refArr
//Load each Drawable and place in a Queue Object
queue.add(loadRandom());
}
Drawable loadRandom()
{
ImageEnum rnd = refArr[Random.nextInt() % ImageEnum.length];
return rnd.loadDrawable();
}
Drawable getRandomDrawable()
{
queue.add(loadRandom()); //Ensure there is always a new one to replace the retrieved
return queue.getFirst()//Not sure if that's the method
}
答案 1 :(得分:0)
我修改了一下我的代码,但最后我还有另一种方法来实现这一点。基本上我已经改变了卡的显示方式。在他们被显示为骰子之前,这是一个快速预览他们中的一些,然后它随机停止。现在我只显示卡片的背面,然后显示卡片,更可能是纸牌游戏。
对于任何看到这个有用的人,我就是这样做的:
card_list_array.xml - &gt;在这里,我们定义现有的卡
<resources>
<array name="cardFaces">
<item>@drawable/c1</item>
<item>@drawable/c2</item>
//...
<强> MainActivity:强>
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.card_game);
card = (ImageView) findViewById(R.id.card);
card.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
card.setImageResource(R.drawable.backside);
card.postDelayed(new Runnable() {
@Override
public void run() {
int rdmCard = randomCard();
card.setImageResource(rdmCard);
}
},100);
}
});
}
public int randomCard() {
TypedArray cardsArray = getResources().obtainTypedArray(R.array.cardFaces);
int randomNumber = (int)(Math.random() * ((100 - 0) + 1));
int card = cardsArray.getResourceId(randomNumber, randomNumber);
cardsArray.recycle();
return card;
}