Java tutorial
//package com.java2s; //License from project: Apache License import android.graphics.Rect; public class Main { /** * Method for finding the flag given its index. * @param index The index of the flag of one of the 32 countries. * @param width The width of the source image. * @return Rect * * <p> * Usage for BitmapRegiondDecoder. * It decodes a region of a bitmap. * It can be very useful if you only need a region of a large image. * <p> */ public static Rect getRectForIndex(int index, int width, int countAllImages, int countPerRow, int space) { // check if index is valid if (index < 0 && index >= countAllImages) throw new IllegalArgumentException("Index must be between 0 and 31."); // calculate one side of a single flag int oneSide = (width - ((countPerRow + 1) * space)) / countPerRow; // calculate the row and col of the given index int row = (index / countPerRow); int col = (index % countPerRow); // left and right sides of the rectangle int left = (oneSide * col) + (space * (col + 1)); int right = left + oneSide; // top and bottom sides of the rectangle int top = (oneSide * row) + (space * (row + 1)); int bottom = top + oneSide; // the resulting rectangle return new Rect(left, top, right, bottom); } }