gpt4 book ai didi

java - 显示随机图像

转载 作者:行者123 更新时间:2023-11-30 02:52:51 25 4
gpt4 key购买 nike

我正在开发的应用程序的部分代码必须在 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]);

最佳答案

引用上面的评论,这就是我处理 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
}

关于java - 显示随机图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23831287/

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