2014-11-21 87 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