Random black and white grid on Android

I am starting with Android and I am working on image processing. I would like to create a grid with random black and white pixels. I tried with a bitmap, but I really don't understand how it works.

I tried with the following code, but it says: Expression type must be array type, but it is allowed for Matrix

Even without this error, I'm not sure if this is the right way. So if you have any ideas ...

Thank!

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);                                     
            }                                               
        }                                   
    }                   
}

      

+3


source to share


2 answers


You can use a bitmap to achieve this, first create a bitmap with your preferred size (100 * 100 in this example):

 int width = 100;
 int height = 100;

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

      

Color this bitmap with black or white pixels (go through each pixel and assign a "random color"):

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);
    }
}

      

Finally, put this bitmap in your image:



imv.setImageBitmap(randomGridBitmap);

      

Globally, your activity should look like this:

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);                        
    }                   
}

      


You are trying to use the setImageMatrix () method (which is oddly not documented), but it is used to transform the image of the image (such as rotation or custom image) and not set the image.

+1


source


I personally suggest you go to the GridView and customize it to suit your GridView needs



Hope it helps.

-1


source







All Articles