Android Bitmap Scale cleanStretchImage(Bitmap image, int xsize, int ysize)

Here you can find the source of cleanStretchImage(Bitmap image, int xsize, int ysize)

Description

stretches the middle sections of the image in a cross to avoid the borders looking stretched

Parameter

Parameter Description
image a parameter
xsize a parameter
ysize a parameter

Declaration

public static Bitmap cleanStretchImage(Bitmap image, int xsize,
        int ysize) 

Method Source Code

//package com.java2s;

import android.graphics.Bitmap;
import android.graphics.Canvas;

import android.graphics.Rect;

public class Main {
    /**/*  w  w  w.  j a va2 s . c o m*/
     * stretches the middle sections of the image in a cross to avoid the borders looking stretched
     * @param image
     * @param xsize
     * @param ysize
     * @return
     */
    public static Bitmap cleanStretchImage(Bitmap image, int xsize,
            int ysize) {
        image = crossStretchImageX(image, xsize);
        image = cleanStretchImageY(image, ysize);
        return image;
    }

    /**
     * stretches the middle section of the image on the X axis to avoid the borders looking stretched
     * @param image
     * @param xsize
     * @return stretched image
     */
    public static Bitmap crossStretchImageX(Bitmap image, int xsize) {
        Bitmap cutout = Bitmap.createBitmap((image.getWidth() / 3),
                image.getHeight(), image.getConfig());
        Canvas cutoutc = new Canvas(cutout);
        Bitmap temp = Bitmap.createBitmap(xsize, image.getHeight(),
                image.getConfig());
        Canvas tempc = new Canvas(temp);

        cutoutc.drawBitmap(image,
                new Rect(image.getWidth() / 3, 0,
                        (image.getWidth() / 3) * 2, image.getHeight()),
                new Rect(0, 0, cutout.getWidth(), cutout.getHeight()), null);
        tempc.drawBitmap(image,
                new Rect(0, 0, image.getWidth() / 3, image.getHeight()),
                new Rect(0, 0, image.getWidth() / 3, temp.getHeight()),
                null);
        tempc.drawBitmap(
                image,
                new Rect((image.getWidth() / 3) * 2, 0, image.getWidth(),
                        image.getHeight()),
                new Rect(temp.getWidth() - (image.getWidth() / 3), 0, temp
                        .getWidth(), temp.getHeight()), null);

        cutout = stretchImage(cutout, xsize - ((temp.getWidth() / 3) * 2),
                cutout.getHeight());

        tempc.drawBitmap(
                cutout,
                new Rect(0, 0, cutout.getWidth(), cutout.getHeight()),
                new Rect(image.getWidth() / 3, 0, temp.getWidth()
                        - (image.getWidth() / 3), image.getHeight()), null);

        return temp;
    }

    /**
     * stretches the middle section of the image on the Y axis to avoid the borders looking stretched
     * @param image
     * @param ysize
     * @return stretched image
     */
    public static Bitmap cleanStretchImageY(Bitmap image, int ysize) {
        Bitmap cutout = Bitmap.createBitmap(image.getWidth(),
                image.getHeight() / 3, image.getConfig());
        Canvas cutoutc = new Canvas(cutout);
        Bitmap temp = Bitmap.createBitmap(image.getWidth(), ysize,
                image.getConfig());
        Canvas tempc = new Canvas(temp);

        cutoutc.drawBitmap(
                image,
                new Rect(0, image.getHeight() / 3, image.getWidth(), (image
                        .getHeight() / 3) * 2),
                new Rect(0, 0, cutout.getWidth(), cutout.getHeight()), null);
        tempc.drawBitmap(image,
                new Rect(0, 0, image.getWidth(), image.getHeight() / 3),
                new Rect(0, 0, temp.getWidth(), image.getHeight() / 3),
                null);
        tempc.drawBitmap(image, new Rect(0, (image.getHeight() / 3) * 2,
                image.getWidth(), image.getHeight()),
                new Rect(0, temp.getHeight() - (image.getHeight() / 3),
                        temp.getWidth(), temp.getHeight()), null);

        cutout = stretchImage(cutout, cutout.getWidth(),
                ysize - ((temp.getHeight() / 3) * 2));

        tempc.drawBitmap(
                cutout,
                new Rect(0, 0, cutout.getWidth(), cutout.getHeight()),
                new Rect(0, image.getHeight() / 3, image.getWidth(), temp
                        .getHeight() - (image.getHeight() / 3)), null);

        return temp;
    }

    /**
     * Stretches {@link Bitmap} to a scale in each direction.
     * 
     * @param image
     * @param xscale
     * @param yscale
     * @return
     */
    public static Bitmap stretchImage(Bitmap image, float xscale,
            float yscale) {
        Bitmap data = Bitmap.createBitmap(
                (int) (image.getWidth() * xscale),
                (int) (image.getHeight() * yscale), image.getConfig());
        Canvas canvas = new Canvas(data);
        canvas.drawBitmap(image,
                new Rect(0, 0, image.getWidth(), image.getHeight()),
                new Rect(0, 0, (int) (image.getWidth() * xscale),
                        (int) (image.getHeight() * yscale)), null);
        return data;
    }

    /**
     * Stretches {@link Bitmap} to a scale in each direction.
     * 
     * @param image
     * @param xsize
     * @param ysize
     * @return
     */
    public static Bitmap stretchImage(Bitmap image, int xsize, int ysize) {
        Bitmap data = Bitmap.createBitmap(xsize, ysize, image.getConfig());
        Canvas canvas = new Canvas(data);
        canvas.drawBitmap(image,
                new Rect(0, 0, image.getWidth(), image.getHeight()),
                new Rect(0, 0, xsize, ysize), null);
        return data;
    }
}

Related

  1. stretchImage(Bitmap image, float xscale, float yscale)
  2. stretchImage(Bitmap image, int xsize, int ysize)
  3. zoomImg(Bitmap bm, int newWidth, int newHeight)
  4. getSmallBitmap(String filePath)
  5. getScaledBitmap(String picturePath, int width, int height)
  6. cleanStretchImageY(Bitmap image, int ysize)