Setting The Image According To The Grid

- 1 answer

Currently I am making custom view which is used for the cropping the image The view is basically a grid which defines the crop area like 3x3,3x2,3x1 and changes the grid basically from 3x3 to 3x2 or one respectively on the button click and one more view the touch image where the image is seated and image is adjusted according the grid(custom view)

Now the problem is that some of the image is not getting adjusted in the 3x3,mostly all the image is getting adjusted

Here see image of my app

Image of my app

What it actually should be like

what it actually should be

That is the problem and even in 3x2,3x1 image is getting cutted i think problem is with my observer where i am putted the code to adjust the image

private class CObserver implements ViewTreeObserver.OnGlobalLayoutListener {

    public void onGlobalLayout() {
        GridLineView.gridHeight = MainActivity.this.gridLineView.getGridHeight();
        Uri parse = Uri.parse(MainActivity.this.getIntent().getExtras().getString("imageUri"));
        Log.e("selectedImage", parse.toString());
        Point point = new Point();
        int i = point.x;
        int i2 = point.y;
        if (i == 0 || i2 == 0) {
            DisplayMetrics displayMetrics = new DisplayMetrics();
            i = displayMetrics.widthPixels;
            i2 = displayMetrics.heightPixels;
        try {
            MainActivity mainActivity = MainActivity.this;
            Context context = MainActivity.this;
            double d = (double) i;
            i = (int) (d * 1.5d);
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver() ,parse);
            Bitmap samplebitmap=scaleBitmap(bitmap,i,i2);
            //Bitmap samplebitmap = mainActivity.decodeSampledBitmapFromInputStream(context, parse, i, i2);
            //Bitmap samplebitmap = Bitmap.createScaledBitmap(bitmap,i,i2,false);
            MainActivity.this.origBitmap = samplebitmap;
            if (samplebitmap != null) {
        } catch (IOException e) {
            Log.e("Error", e.getMessage());

so if anyone can help me with my code to fix the image shown in 3x3,and getting half image in 3x2 grid

Here is my all project files Project Files



If you want to draw the lines dynamically as per your screen size then you should use the following code in your camera preview class.

    protected void onDraw(Canvas canvas)  
        //  Find Screen size first  
        DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();  
        int screenWidth = metrics.widthPixels;  
        int screenHeight = (int) (metrics.heightPixels*0.9);  

        //  Set paint options  
        paint.setColor(Color.argb(255, 255, 255, 255));  


what we are trying to do in the above code is to draw three line both horizontaly and vertically with even spacing in between in you code inside @OnDraw you are trying to draw an circle which is not needed by you as stated in the question You also need to add the following line in the constructor of your camera preview class:


below code source :Divide Image into parts

public ArrayList<Bitmap> splitBitmap(Bitmap bitmap, int xCount, int yCount) {
    ArrayList<Bitmap> pieces = new ArrayList<>(piecesNumber);
    int width, height;
    width = bitmap.getWidth() / xCount;
    height = bitmap.getHeight() / yCount;
    for(int x = 0; x < xCount; ++x) {
        for(int y = 0; y < yCount; ++y) {
            pieces.add(Bitmap.createBitmap(bitmap, x * width, y * height, width, height));
    return pieces;

the above code will cut the image in 9 equal parts