Java tutorial
//package com.java2s; /** * Copyright (c) 2015 Dennis Lang (LanDen Labs) landenlabs@gmail.com * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and * associated documentation files (the "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the * following conditions: * * The above copyright notice and this permission notice shall be included in all copies or substantial * portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * * @author Dennis Lang (3/21/2015) * @see http://landenlabs.com * */ import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; public class Main { /** * Create darkened version of input drawable offset. * * @param res * @param inImage * @param offsetX * @param offsetY * @param blurRadius Not currently used. * @param shadowColor * @return */ public static BitmapDrawable shadowImage(Resources res, Drawable inImage, int offsetX, int offsetY, float blurRadius, int shadowColor) { Bitmap inBitmap; if (inImage instanceof BitmapDrawable) { inBitmap = ((BitmapDrawable) inImage).getBitmap(); } else { // Bitmap from drawable int imgWidth = inImage.getIntrinsicWidth(); int imgHeight = inImage.getIntrinsicHeight(); inBitmap = Bitmap.createBitmap(imgWidth, imgHeight, Bitmap.Config.ARGB_8888); Canvas bottomCanvas = new Canvas(inBitmap); inImage.setBounds(0, 0, bottomCanvas.getWidth(), bottomCanvas.getHeight()); inImage.draw(bottomCanvas); } Bitmap blurBitmap = shadowBitmap(inBitmap, offsetX, offsetY, blurRadius, shadowColor); return new BitmapDrawable(res, blurBitmap); } /** * Draw input bitmap in shadow color and offset. Assumes offset is positive. * * @param inBitmap * @param offsetX * @param offsetY * @param blurRadius Not used * @param shadowColor * @return */ public static Bitmap shadowBitmap(Bitmap inBitmap, int offsetX, int offsetY, float blurRadius, int shadowColor) { int imgWidth = inBitmap.getWidth(); int imgHeight = inBitmap.getHeight(); Bitmap bottomBm = Bitmap.createBitmap(imgWidth, imgHeight, Bitmap.Config.ARGB_8888); Canvas bottomCanvas = new Canvas(bottomBm); // bottomCanvas.drawColor(shadowColor); Paint bottomPaint = new Paint(Paint.ANTI_ALIAS_FLAG); bottomPaint.setColor(shadowColor); bottomCanvas.drawRect(offsetX, offsetY, imgWidth, imgHeight, bottomPaint); // MaskFilter filter = new BlurMaskFilter(Math.max(0.5f, blurRadius), BlurMaskFilter.Blur.NORMAL); bottomPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP)); // bottomPaint.setShadowLayer(blurRadius, offsetX, offsetX, shadowColor); // bottomPaint.setMaskFilter(filter); bottomCanvas.drawBitmap(inBitmap, offsetX, offsetY, bottomPaint); return bottomBm; } }