Here you can find the source of resizeAndCropCenter(final Bitmap bitmap, final int size)
Parameter | Description |
---|---|
bitmap | The artist, album, genre, or playlist image that's going to be cropped. |
size | The new size. |
public static final Bitmap resizeAndCropCenter(final Bitmap bitmap, final int size)
//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; } }