Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Image splitting in Android

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 3.48k
    Comment on it

    Image split can be required by different android puzzle application where there is a need to divide the one complete picture into different pieces.


    Here is the function that takes three argument one is the imageview that holds your complete image and the other two are the number of rows and columns.

    /* globally declare this arraylist, this will be used to display your image chunks into the gridview*/
    ArrayList<Bitmap> imageChunksStorageList ;
    private void splitImage(ImageView image, int rows, int columns)
    {
    //For height and width of the small image chunks
    int chunkHeight,chunkWidth;
    Bitmap image; // use to store image chunks
    int chunkNumbers=rows*column;
    //To store all the small image chunks in bitmap format in this list
    bitmapStorageList = new ArrayList<Bitmap>(chunkNumbers);
    //Getting the BitmapDrawable from the imageview where your image is displayed
    BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
    Bitmap bitmap = drawable.getBitmap();
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);
    //setting the height and width of each pieces according to the rows and columns
    chunkHeight = bitmap.getHeight()/rows;
    chunkWidth = bitmap.getWidth()/columns;
    //x and y are the pixel positions of the image chunks
    int yCoord = 0;
    for(int i=0; i<rows; i++)
    {
    int x = 0;
    for(int j=0; j<columns; j++)
    {
    image=Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight);
    imageChunksStorageList.add(image);
    x=x+chunkWidth;
    }
    y=y+chunkHeight;
    }
    /* Once, you are done with this split task then you can show your arraylist into the gridview */
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: