Java tutorial
//package com.java2s; /* * Copyright (C) 2008 Romain Guy * * 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; import android.graphics.Matrix; import android.graphics.Rect; import android.graphics.RectF; public class Main { private static final float EDGE_START = 0.0f; private static final float EDGE_END = 4.0f; private static final Paint EDGE_PAINT = new Paint(); private static final Paint END_EDGE_PAINT = new Paint(); private static final float FOLD_START = 5.0f; private static final float FOLD_END = 13.0f; private static final Paint FOLD_PAINT = new Paint(); private static final float SHADOW_RADIUS = 12.0f; private static final Paint SHADOW_PAINT = new Paint(); private static final Paint SCALE_PAINT = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG); private static volatile Matrix sScaleMatrix; /** * Create a book cover with the specified bitmap. This method applies several * lighting effects to the original bitmap and returns a new decored bitmap. * * @param bitmap The bitmap to decor with lighting effects * @param width The target width of the decored bitmap * @param height The target height of the decored bitmap * * @return A new Bitmap based on the original bitmap */ public static Bitmap createBookCover(Bitmap bitmap, int width, int height) { final int bitmapWidth = bitmap.getWidth(); final int bitmapHeight = bitmap.getHeight(); final float scale = Math.min((float) width / (float) bitmapWidth, (float) height / (float) bitmapHeight); final int scaledWidth = (int) (bitmapWidth * scale); final int scaledHeight = (int) (bitmapHeight * scale); final Bitmap decored = createScaledBitmap(bitmap, scaledWidth, scaledHeight, SHADOW_RADIUS, true, SHADOW_PAINT); final Canvas canvas = new Canvas(decored); canvas.translate(SHADOW_RADIUS / 2.0f, SHADOW_RADIUS / 2.0f); canvas.drawRect(EDGE_START, 0.0f, EDGE_END, scaledHeight, EDGE_PAINT); canvas.drawRect(FOLD_START, 0.0f, FOLD_END, scaledHeight, FOLD_PAINT); //noinspection PointlessArithmeticExpression canvas.translate(scaledWidth - (EDGE_END - EDGE_START), 0.0f); canvas.drawRect(EDGE_START, 0.0f, EDGE_END, scaledHeight, END_EDGE_PAINT); return decored; } private static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, float offset, boolean clipShadow, Paint paint) { Matrix m; synchronized (Bitmap.class) { m = sScaleMatrix; sScaleMatrix = null; } if (m == null) { m = new Matrix(); } final int width = src.getWidth(); final int height = src.getHeight(); final float sx = dstWidth / (float) width; final float sy = dstHeight / (float) height; m.setScale(sx, sy); Bitmap b = createBitmap(src, 0, 0, width, height, m, offset, clipShadow, paint); synchronized (Bitmap.class) { sScaleMatrix = m; } return b; } private static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, float offset, boolean clipShadow, Paint paint) { int scaledWidth = width; int scaledHeight = height; final Canvas canvas = new Canvas(); canvas.translate(offset / 2.0f, offset / 2.0f); Bitmap bitmap; final Rect from = new Rect(x, y, x + width, y + height); final RectF to = new RectF(0, 0, width, height); if (m == null || m.isIdentity()) { bitmap = Bitmap.createBitmap(scaledWidth + (int) offset, scaledHeight + (int) (clipShadow ? (offset / 2.0f) : offset), Bitmap.Config.ARGB_8888); paint = null; } else { RectF mapped = new RectF(); m.mapRect(mapped, to); scaledWidth = Math.round(mapped.width()); scaledHeight = Math.round(mapped.height()); bitmap = Bitmap.createBitmap(scaledWidth + (int) offset, scaledHeight + (int) (clipShadow ? (offset / 2.0f) : offset), Bitmap.Config.ARGB_8888); canvas.translate(-mapped.left, -mapped.top); canvas.concat(m); } canvas.setBitmap(bitmap); canvas.drawRect(0.0f, 0.0f, width, height, paint); canvas.drawBitmap(source, from, to, SCALE_PAINT); return bitmap; } }