我在Android開發初學者,我創造像4Pics1Word遊戲爲我的項目。 我想知道上哪能由4隨機圖像的每個活動都必須有4個圖像及這些圖像必須彼此相關,因此,4張圖片1個字。所以我想我可以把描述每個圖像,然後隨機然後才能使用控制語句到g 等這些4張圖片相匹配的描述。我只需要隨機它以隨機的水平(所以每個應用程序不會有相同的連續的水平)。有任何想法嗎?謝謝youuuu!如何隨機一組圖像中在Android Studio中的ImageView?
回答
我會做這樣的事情:
public void start() {
MyImage[] images = /* a list of your images */
ImageView[] imageViews = /* a list of your imageViews (in this case, 4)*/
fillImageViews(/* the desc */, imageViews, images);
}
private class MyImage() {
Drawable image;
String desc;
public MyImage(Drawable img, String description) {
image = img;
desc = description;
}
}
public void fillImageViews(String desc, ImageView[] views, MyImage[] images) {
ArrayList<Integer> usedIndexs = new ArrayList();
Random rand = new Random();
for (ImageView v : views) {
while (usedIndexs.size() < images.length) {
int index = rand.nextInt(images.length);
if (images[index].desc.equals(desc) && !usedIndexs.contains(index)) {
//description matches
v.setImageDrawable(images[index].image);
usedIndexs.add(index);
break;
}
usedIndexs.add(index);
}
}
}
它所做的就是找到一個列表,並檢查隨機圖片,看它是否已還,如果描述匹配使用。它將繼續拉動隨機圖片,直到它嘗試了所有或所有ImageView的填充。還沒有嘗試過,但代碼應該工作。
另一個想法:它可能會更好地具有相同描述中的所有圖像自己arraylist。這樣你就可以從這個數組列表中隨機抽取一張圖片。例如:
ArrayList<Drawable> catPics = new ArrayList();
//add cat pics here
ArrayList<Drawable> dogPics = new ArrayList();
//add dogs pics here
//And now you could make function to pull the pictures
public Drawable getRandomPicture(ArrayList<Drawable> imgs) {
return imgs.get(new Random.nextInt(imgs.size()));
}
Heyy!非常感謝你!我會嘗試的! :d –
// In drwable folder your imagename like img_0,img_1,img_2.....img_N
String imagename = "img_" + rnd.nextInt(N);// N is Maximum random number
imageView.setImageDrawable(
getResources().getDrawable(getResourceID(imagename, "drawable",
getApplicationContext()))
);
謝謝。但我需要有一組4個相關圖片隨機化。所以我們只說我的遊戲有5項活動。每個活動由4張彼此相關的圖片組成。所以我會用20張照片。那20張圖片將被除以5.每組有4張彼此相關的圖片。那麼,我該如何隨機化這些圖片呢? –
同樣的概念你應用所有的活動,但改變活動的圖像名稱..例如,如果我想顯示蘋果圖像只在活動A意味着你的圖像名稱應該像apple_0..apple_5..the隨機數5 ...然後imagename =「apple_ 「+ rnd.nextInt(N) – sasikumar
感謝!我也會試試! :D –
- 1. 如何在ImageView中加載隨機圖像android
- 2. 片段中ImageView中的隨機圖像
- 3. Android中的隨機圖像?
- 4. 如何從一組圖像中獲取隨機圖像?
- 5. 在Android Studio中獲取隨機圖片
- 6. 如何在Android ImageView中設置圖像
- 7. 如何引用隨機圖像數組中返回的圖像?
- 8. 如何在Android中的GridView中顯示隨機圖像
- 9. 在Android中循環隨機圖像
- 10. android如何從10個圖像數組中隨機獲得4個圖像
- 11. 如何隨機化android studio中的圖像?我寫的代碼是不工作
- 12. 如何在Android中的ListView中的imageview中設置圖像?
- 13. 隨機圖像 - 的Android
- 14. 如何在c#中初始化一組隨機圖像文件並在XAML中隨機地綁定它們
- 15. 如何隨機旋轉Android中的圖像?
- 16. android - ViewHolder ImageView隨機加載
- 17. 在Android中,如何使圖像隨機出現?
- 18. 隨機圖像中的GridView
- 19. 將隨機大小的圖像合成到ImageView中
- 20. android studio在imageview下方製作一個textView,如果imageview沒有圖像可見
- 21. 從數組中隨機選擇圖像
- 22. Xcode中的隨機圖像
- 23. UpdateProgress中的隨機圖像
- 24. Java中的隨機圖像
- 25. 如何在android中使用timer來在gridview的不同圖像視圖中隨機顯示圖像?
- 26. 在android studio中碰撞ImageView
- 27. 如何在android中設置imageview中的圖像?
- 28. 如何在android中的imageview中添加gif圖像?
- 29. 如何在android中的ImageView中顯示gif圖像?
- 30. 如何將隨機圖像設置爲ImageView?
您嘗試過什麼嗎?如果你有,然後在你的問題中提及它,並告訴你哪一步失敗。我們很多人想幫助你,但請閱讀[我如何問一個好問題](http://stackoverflow.com/help/how-to-ask)。無論如何,歡迎來到StackOverflow :) – koceeng