Android Bitmap Resize resizeAndCropCenter(final Bitmap bitmap, final int size)

Here you can find the source of resizeAndCropCenter(final Bitmap bitmap, final int size)

Description

This is only used when the launcher shortcut is created.

License

Apache License

Parameter

Parameter Description
bitmap The artist, album, genre, or playlist image that's going to be cropped.
size The new size.

Return

A that has been resized and cropped for a launcher shortcut.

Declaration

public static final Bitmap resizeAndCropCenter(final Bitmap bitmap,
        final int size) 

Method Source Code

//package com.java2s;
/*//from   w w  w .j a v  a  2  s . c o m
 * Copyright (C) 2012 Andrew Neal Licensed under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

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

import android.graphics.Paint;

public class Main {
    /**
     * This is only used when the launcher shortcut is created.
     * 
     * @param bitmap The artist, album, genre, or playlist image that's going to
     *            be cropped.
     * @param size The new size.
     * @return A {@link Bitmap} that has been resized and cropped for a launcher
     *         shortcut.
     */
    public static final Bitmap resizeAndCropCenter(final Bitmap bitmap,
            final int size) {
        final int w = bitmap.getWidth();
        final int h = bitmap.getHeight();
        if (w == size && h == size) {
            return bitmap;
        }

        final float mScale = (float) size / Math.min(w, h);

        final Bitmap mTarget = Bitmap.createBitmap(size, size,
                Bitmap.Config.ARGB_8888);
        final int mWidth = Math.round(mScale * bitmap.getWidth());
        final int mHeight = Math.round(mScale * bitmap.getHeight());
        final Canvas mCanvas = new Canvas(mTarget);
        mCanvas.translate((size - mWidth) / 2f, (size - mHeight) / 2f);
        mCanvas.scale(mScale, mScale);
        final Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG
                | Paint.DITHER_FLAG);
        mCanvas.drawBitmap(bitmap, 0, 0, paint);
        return mTarget;
    }
}

Related

  1. getSampledBitmap(String filePath, int reqWidth, int reqHeight)
  2. manageBitmapRotatio(int photoW, int photoH, Bitmap bitMap, int rotation)
  3. readBitmapAutoSize(String filePath, int outWidth, int outHeight)
  4. resize(Bitmap bitmap, int newWidth, int newHeight)
  5. resizeAndCropCenter(Bitmap bitmap, int size, boolean recycle)
  6. resizeBitmap(Context context, int resId, int w, int h)
  7. resizeBitmapByScale(Bitmap bitmap, float scale, boolean recycle)
  8. resizeDownBySideLength(Bitmap bitmap, int maxLength, boolean recycle)
  9. resizeDrawable(Context context, int resId, int w, int h)