Bitmap createScaledBitmap - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Bitmap createScaledBitmap method.

This code example shows how to use the following methods:createScaledBitmap, getHeight, getWidth
	 * @return Bitmap A new Bitmap object of the same bitmap after scaling. 
	 */ 
	public Bitmap scaleBitmap(Bitmap bitmap) {
		int newWidth = this.targetWidth;
		int newHeight = this.targetHeight;
		int origWidth = bitmap.getWidth();
		int origHeight = bitmap.getHeight();
 
		// If no new width or height were specified return the original bitmap 
		if (newWidth <= 0 && newHeight <= 0) {
			return bitmap;
		} 
		// Only the width was specified 
		else if (newWidth > 0 && newHeight <= 0) {
			newHeight = (newWidth * origHeight) / origWidth;
		} 
		// only the height was specified 
		else if (newWidth <= 0 && newHeight > 0) {
			newWidth = (newHeight * origWidth) / origHeight;
		} 

8
CodeRank
This code example shows how to use the following methods:createScaledBitmap, getHeight, getWidth, recycle
     * @param height   preferred height of the new image 
     * @param upsample if true smaller images than the preferred size are increased, if false the origin bitmap is returned 
     * @return new bitmap if size has changed, otherwise original bitmap. 
     */ 
    public Bitmap scaleBitmap(Bitmap b, int width, int height, boolean upsampling) {
        int imageHeight = b.getHeight();
        int imageWidth = b.getWidth();
        if (!upsampling && imageHeight <= height && imageWidth <= width) {
            return b;
        } 
        int finalWidth = width;
        int finalHeight = height;
        if (imageHeight > imageWidth) {
            float factor = ((float) height) / ((float) imageHeight);
            finalHeight = new Float(imageHeight * factor).intValue();
            finalWidth = new Float(imageWidth * factor).intValue();
        } else { 
            float factor = ((float) width) / ((float) imageWidth);
            finalHeight = new Float(imageHeight * factor).intValue();
            finalWidth = new Float(imageWidth * factor).intValue();

8
CodeRank
This code example shows how to use the following methods:createScaledBitmap, getHeight, getWidth, recycle
    } 
 
    public static Bitmap scale(Bitmap bmp, int w, int h) {
        Bitmap b = null;
        if (bmp != null) {
            Log.v(TAG, "Scaling bitmap from " + bmp.getWidth() + "x" + bmp.getHeight() + " to " + w + "x" + h);
 
            // We're going to need some memory 
            System.gc();
            Runtime.getRuntime().gc();
 
            // Scale it! 
            b=Bitmap.createScaledBitmap(bmp, w, h, true);
 
            // Release original bitmap 
            bmp.recycle();
        } 
        return b;
    } 
 
    private void loadTextureImpl(Bitmap bitmap) {
8
CodeRank
This code example shows how to use the following methods:createBitmap, createScaledBitmap, getHeight, getWidth
	 */ 
	public static Bitmap resizeBitmap( final Bitmap input, int destWidth, int destHeight, int rotation ) throws OutOfMemoryError {
 
		int dstWidth = destWidth;
		int dstHeight = destHeight;
		final int srcWidth = input.getWidth();
		final int srcHeight = input.getHeight();
 
		if ( rotation == 90 || rotation == 270 ) {
			dstWidth = destHeight;
			dstHeight = destWidth;
		} 
 
		boolean needsResize = false;
		float p;
		if ( ( srcWidth > dstWidth ) || ( srcHeight > dstHeight ) ) {
			needsResize = true;
			if ( ( srcWidth > srcHeight ) && ( srcWidth > dstWidth ) ) {
				p = (float) dstWidth / (float) srcWidth;
				dstHeight = (int) ( srcHeight * p );

8
CodeRank
This code example shows how to use the following methods:createScaledBitmap, getHeight, getWidth
This code example shows how to use the following methods:createScaledBitmap, getHeight, getWidth
			// may be larger than the thumbnail size if it is not a 1:1 aspect ratio. This is necessary to provide 
			// parity with iOS since the Android scale-to-fit options will scale the image so that both width and 
			// height fit within the specified rectangle, whereas the iOS scale-to-fit options will scale so that 
			// the smaller size fits the specified rectangle 
			 
			float scale = (image.getWidth() < image.getHeight()) ? (float)image.getWidth() / (float)size : (float)image.getHeight() / (float)size;
			int width = (int)(image.getWidth() / scale);
			int height = (int)(image.getHeight() / scale);
			Bitmap tempBitmap = Bitmap.createScaledBitmap(image, width, height, true);
			 
			// Create a new bitmap that is the thumbnail size 
			Bitmap resultBitmap = Bitmap.createBitmap(size + borderSize * 2, size + borderSize * 2, Bitmap.Config.ARGB_8888);
			Canvas canvas = new Canvas(resultBitmap);
			 
			// Create a clipping path with rounded corners 
			if (cornerRadius > 0) {
		        final Rect rect = new Rect(borderSize, borderSize, size + borderSize, size + borderSize);
		        final RectF rectF = new RectF(rect);
				Path path = new Path();
				path.addRoundRect(rectF, (float)cornerRadius, (float)cornerRadius, Path.Direction.CCW);
8
CodeRank
This code example shows how to use the following methods:createScaledBitmap, getHeight, getWidth
        Point displaySize = Compatibility.getDisplaySize();
        final int maxWidth = displaySize.x - 25;
        final int maxHeight = displaySize.y - 25;
 
        Bitmap result = image;
        int width = image.getWidth();
        int height = image.getHeight();
 
        if (width > maxWidth || height > maxHeight) {
            final double ratio = Math.min((double) maxHeight / (double) height, (double) maxWidth / (double) width);
            width = (int) Math.ceil(width * ratio);
            height = (int) Math.ceil(height * ratio);
            result = Bitmap.createScaledBitmap(image, width, height, true);
        } 
 
        final BitmapDrawable resultDrawable = new BitmapDrawable(app.getResources(), result);
        resultDrawable.setBounds(new Rect(0, 0, width, height));
        return resultDrawable;
    } 
 
8
CodeRank
This code example shows how to use the following methods:copy, createScaledBitmap, getHeight, getWidth
This code example shows how to use the following methods:createScaledBitmap, getHeight, getWidth
	public static int getSize(Bitmap bm) {
		return bm.getRowBytes() * bm.getHeight();
	} 
 
	public static Bitmap getScaled(Bitmap bm, int sidePx, boolean max) {
		float w = bm.getWidth();
		float h = bm.getHeight();
		float wRatio = sidePx / w;
		float hRatio = sidePx / h;
		float ratio = max ? Math.min(wRatio, hRatio) : Math.max(wRatio, hRatio);
		w = ratio * w;
		h = ratio * h;
		return Bitmap.createScaledBitmap(bm, (int) w, (int) h, true);
	} 
 
	public static Bitmap getSquare(Bitmap bm) {
		int w = bm.getWidth();
		int h = bm.getHeight();
		int side = (w > h) ? h : w;
		return Bitmap.createBitmap(bm, 0, 0, side, side);
7
CodeRank
This code example shows how to use the following methods:createScaledBitmap, getWidth
    } 
 
    private void scaleBitmap() { 
        // If the Favicon can be resized to fill the view exactly without an enlargment of more than 
        // a factor of two, do so. 
        int doubledSize = mIconBitmap.getWidth()*2;
        if (mActualWidth > doubledSize) {
            // If the view is more than twice the size of the image, just double the image size 
            // and do the rest with padding. 
            mIconBitmap = Bitmap.createScaledBitmap(mIconBitmap, doubledSize, doubledSize, true);
        } else { 
            // Otherwise, scale the image to fill the view. 
            mIconBitmap = Bitmap.createScaledBitmap(mIconBitmap, mActualWidth, mActualWidth, true);
        } 
    } 
 
    /** 
     * Helper method to display background of the dominant colour of the favicon to pad the remaining 
     * space. 
     */ 
7
CodeRank

Related Bitmap createScaledBitmap Questions & Answers:

createScaledBitmap Method Overview
Creates a new bitmap, scaled from an existing bitmap, when possible.
See Code Examples for other Android Bitmap Methods: