Android Bitmap Scale crossStretchImageX(Bitmap image, int xsize)

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

Description

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

Parameter

Parameter Description
image a parameter
xsize a parameter

Return

stretched image

Declaration

public static Bitmap crossStretchImageX(Bitmap image, int xsize) 

Method Source Code

//package com.java2s;

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

import android.graphics.Rect;

public class Main {
    /**/*from w  w  w  . j  a va 2 s  . co  m*/
     * 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 {@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. getScaleBitmap(Context context, int resId, int dstWidth, int dstHeight)
  2. getScaleImage(final Bitmap bitmap, final float boundBoxInDp)
  3. generateScaledBitmap(Bitmap bitmap, int width, int height)
  4. generateScaledBitmap(Drawable drawable, int width, int height)
  5. generateScaledBitmap(byte[] bitMapData, int width, int height)
  6. scaleBitmap(Bitmap bitmap, int width, int height)
  7. scaleBitmap(String path, int newWidth, int newHeight)
  8. scaleClipBitmapByCircle(Bitmap src, int nRadius, float fStrokeWidth)
  9. getScaledImageFromUri(Activity activity, Uri uri, int size)