2014-11-21 70 views
3

我開始使用Android並開始圖像處理。 我想創建一個具有隨機黑白像素的網格。我試圖用位圖,但我真的不知道它是如何工作的。Android上的隨機黑白網格

我試着用下面的代碼,但它說: 類型的表達式必須是一個數組類型,但它解決了在矩陣

即使沒有這個錯誤,我不知道這是正確的方式繼續。所以,如果你有任何想法...

謝謝!

import java.util.Random; 
import android.support.v7.app.ActionBarActivity; 
import android.graphics.Bitmap; 
import android.graphics.Matrix; 
import android.graphics.drawable.BitmapDrawable; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ImageView; 

public class MainActivity extends ActionBarActivity {  

    private ImageView img;   

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     img = (ImageView)findViewById(R.id.imageView1);      
    } 

    public void RandomImage (View view) 
    { 
     Random rand = new Random(); 

     Matrix mat = new Matrix(); 
     for (int i=0; i<100; i++) 
     { 
      for (int j=0; j<100 ; j++) 
      {      
       mat[i][j]=rand.nextInt(1); 
       img.setImageMatrix(mat);          
      }            
     }         
    }     
} 
+2

我不認爲'img.setImageMatrix(mat);'應該在你的for循環中。但這是一個單獨的問題 – Doomsknight 2014-11-21 14:16:38

+0

是的,你說得對。這不是故意的。我會改變它。 – user3855955 2014-11-21 14:20:28

+0

另一點是'nextInt(1)'將返回0我認爲(你應該檢查這個)。 'nextInt(2)'將返回0或1.考慮在自定義畫布上繪製正方形(或像素取決於您想要的比例)。實現像繪圖一樣的網格。或者使用畫布放到位圖上,以返回網格圖像。有關更多信息,請參閱http://stackoverflow.com/questions/5663671/creating-an-empty-bitmap-and-drawing-though-canvas-in-android – Doomsknight 2014-11-21 14:26:30

回答

1

您可以使用位圖來實現這一目標,首先用首選尺寸(100 *在這個例子中100)的位圖:

int width = 100; 
int height = 100; 

Bitmap randomGridBitmap = Bitmap.createBitmap(width,height, Bitmap.Config.RGB_565); 

顏色此位圖與黑色或白色像素(經過每個像素assigne 「隨機顏色」):

Random rand = new Random(); 

for (int i=0; i<width; i++){ 
    for (int j=0; j<height ; j++){ 
     // default color : Black 
     int colorToPut = Color.BLACK; 

     // If lucky get a white pixel ;) 
     if(rand.nextInt(2)==0){ 
      colorToPut = Color.WHITE; 
     } 

     // Set color to (i,j) pixel 
     randomGridBitmap.setPixel(i,j,colorToPut); 
    } 
} 

最後把這個位在您的ImageView:

imv.setImageBitmap(randomGridBitmap); 

全球範圍內的活動應該是這樣的:

public class MainActivity extends ActionBarActivity {  

    private ImageView img;   

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     img = (ImageView)findViewById(R.id.imageView1); 
     createAndSetRandomImage();      
    } 

    public void createAndSetRandomImage(){ 
     Random rand = new Random(); 

     for (int i=0; i<width; i++){ 
      for (int j=0; j<height ; j++){ 
      // default color : Black 
      int colorToPut = Color.BLACK; 

      // If lucky get a white pixel ;) 
      if(rand.nextInt(2)==0){ 
       colorToPut = Color.WHITE; 
      } 

      // Set color to (i,j) pixel 
      randomGridBitmap.setPixel(i,j,colorToPut); 
      } 
     } 
     imv.setImageBitmap(randomGridBitmap);       
    }     
} 

您嘗試使用setImageMatrix()方法(這是奇怪的沒有記錄),但它是利用改造的ImageView的圖像(例如旋轉或定製級圖像)並且不設置圖像。

+0

這正是我所建議的,但沒有例子:) +1。 我認爲還有繪製小方塊的範圍,而不僅僅是像素。取決於要求。 – Doomsknight 2014-11-21 14:38:28

+0

所以我嘗試了你的建議。我真的很喜歡這個主意!沒有錯誤,但是當我運行應用程序時什麼都沒有發生...... – user3855955 2014-11-21 16:04:21

+0

您的方法RandomImage如何被調用?在onCreate中沒有任何關係 – 2014-11-22 10:29:12

-1

我個人建議你去GridView和自定義此根據自己的需要GridView

希望這有助於。

+0

我想要的結果更像是QR碼。所以GridView並不適合。 – user3855955 2014-11-21 14:21:51