2011-09-15 23 views
2

問題說明了一切,我有52個圖像視圖(紙牌),我想隨機顯示位置。將圖像視圖添加到隨機位置佈局的最佳方式

我目前有此:

for (Card card : deck.getAll()) { 
     ImageView iv = new ImageView(this); 
     RelativeLayout.LayoutParams lParams = new RelativeLayout.LayoutParams(57, 105); 
     if (random) { 
      lParams.leftMargin = (int)Math.ceil(Math.random()*(metrics.widthPixels - 57)); 
      lParams.topMargin = (int)Math.ceil(Math.random()*(metrics.heightPixels - 105)); 
     } else { 
      //if (metrics.widthPixels < ((i + 1) * 57)) {i = 0;++j;} 
      if (i > 12) {i = 0;++j;} 
      lParams.leftMargin = i*60; 
      lParams.topMargin = j*107; 
      ++i; 
     } 
     iv.setRotation((float)Math.ceil(Math.random()*(360))); 
     iv.setId(card.getInt()); 

     layout.addView(iv, lParams); 

這適用於現在,但後來我將在特定位置顯示的所有卡,它只是似乎沒有我的權利與邊緣定位的一切。

在此先感謝您的任何建議!

+0

經過一些工作,我找到了解決方案。 http://stackoverflow.com/a/35406696/3741769 – Ahsanwarsi

回答

1

請閱讀使用畫布並繪製位圖直接指向。你把一個視圖渲染到,然後實現它的onDraw來將你的卡片渲染到那個視圖的畫布上。

每次需要移動卡片時,都會重畫Canvas,否則不需要。這基本上是2D遊戲的工作原理。有很多教程,它不需要很長時間。

+0

感謝您指出我在正確的方向。下面的鏈接給了我一個堅實的起點:http://developer.android.com/guide/topics/graphics/index.html#draw-with-canvas(我應該在發佈這個問題之前找到這個..) – user458753

+0

你我很歡迎,並不認爲我需要尋找你。 – Zulaxia

相關問題