Android Bitmap Scale cleanStretchImageY(Bitmap image, int ysize)

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

Description

stretches the middle section of the image on the Y axis to avoid the borders looking stretched

Parameter

Parameter Description
image a parameter
ysize a parameter

Return

stretched image

Declaration

public static Bitmap cleanStretchImageY(Bitmap image, int ysize) 

Method Source Code

//package com.java2s;

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

import android.graphics.Rect;

public class Main {
    /**//from w ww. j a va 2 s  . c  o m
     * 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, int xsize, int ysize)
  2. zoomImg(Bitmap bm, int newWidth, int newHeight)
  3. getSmallBitmap(String filePath)
  4. getScaledBitmap(String picturePath, int width, int height)
  5. cleanStretchImage(Bitmap image, int xsize, int ysize)