Recently in doing an Android bitmap shadow processing effect, today to summarize the idea:
The analysis is as follows:
The Android SDK provides the Blurmaskfilter tool class to add a shadow effect to the image, with the following code:
—/**
* This takes a mask, and blurs it edge by the specified radius. Whether or
* or not to include the original mask, and whether the blur goes outside,
* Inside, or straddles, the original mask ' s border, is controlled by the
* Blur enum.
*/
' public class Blurmaskfilter extends Maskfilter {
public enum Blur { NORMAL(0), //!< blur inside and outside of the original border SOLID(1), //!< include the original mask, blur outside OUTER(2), //!< just blur outside the original border INNER(3); //!< just blur inside the original border Blur(int value) { native_int = value; } final int native_int;}/** * Create a blur maskfilter. * * @param radius The radius to extend the blur from the original mask. Must be > 0. * @param style The Blur to use * @return The new blur maskfilter */public BlurMaskFilter(float radius, Blur style) { native_instance = nativeConstructor(radius, style.native_int);}private static native int nativeConstructor(float radius, int style);
}
' ——-
Note:
- Blurmaskfilter can blur the edges of a bitmap within a specified radius. Whether to include the original mask, whether or not to blur inside, outside, or across the inside and outside of the bitmap can be determined by giving the enumeration blur initialization of different parameters in the code.
- The Blur corresponds to each parameter meaning: (translation reference here)
NORMAL (0),//!< displays masks both inside and outside the target, from the edge to the target and from the outside to the radius of the edge, and when the mask is displayed outward, the color obtained at the target edge is displayed.
SOLID (1),//!< shows the mask outside the target, from the edge to the target to the radius from the edge, and the part shows the color obtained from the target edge, showing the target
OUTER (2), the//!< shows a mask outside the target, from the edge to the target to a radius wide from the edge, and the part shows the color obtained from the target edge, without showing the target
INNER (3); The!< displays a mask within the target, from the edge to the target, to the radius wide from the edge, and radius is the value of the initialized Blurmaskfilter
- After constructing the good one Blurmaskfilter object, you can pass the Android.graphics.Paint class's Setmaskfilter method to the Brush object, which we can use to manipulate the edges of the bitmap using the shadow effect brush.
Now that we have a brush that can be obfuscated, how do you use this brush to manipulate the edges of a specified bitmap? We also need to look at another tool class provided by the SDK, this class is a method of Android.graphics.Bitmap Extractalpha, the code is as follows:
/** * Returns a new bitmap thatCaptures theAlpha values of theOriginal. * These values may affected by theOptional Paint parameter, which * cancontain itsOwn Alpha, andMay alsocontainA maskfilter which * could change theActual dimensions of theresulting bitmap (e.g. * A blur maskfilter might enlarge theResulting bitmap). If Offsetxy * is notNullitReturns theAmount to Offset theReturned bitmap so * that itwould logically align with theOriginal. For example,if the* PaintcontainsA Blur ofRadius2, ThenOffsetxy[] wouldcontains* -2, -2, so thatDrawing theAlpha BitmapOffset by(-2, -2) and Then* Drawing theOriginal wouldresult inch theBlur visually aligning with* theOriginal. * * <p>the initial density of theReturned bitmap is theSame as theOriginal ' s. * * @param paint Optional paint used toModify theAlpha valuesinch the* resulting bitmap. Pass NULL forDefault behavior. * @param offsetxy Optional Array thatReturns theX (Index0) andY * (Index1)OffsetNeeded toPosition theReturned bitmap * So that itVisually lines up with theOriginal. * @returnNew Bitmap containing the(Optionally modified byPaint) Alpha * Channel of theOriginal bitmap. This could be drawn with* CANVAS.DRAWBITMAP (),where theColor (s) would be taken from the* Paint that isPassed to theDraw call. */Public Bitmap extractalpha (paint paint, int[] offsetxy) {checkrecycled ("Can ' t extractalpha on a recycled bitmap"); int Nativepaint = paint! = null? Paint.mnativepaint:0; Bitmap BM = Nativeextractalpha (Mnativebitmap, Nativepaint, OFFSETXY);if(BM = = null) {throw new RuntimeException ("Failed to Extractalpha on Bitmap"); } bm.mdensity = mdensity;returnBm }
Note:
- The function of this method is to return a new bitmap, which simply acquires the transparent value of the original bitmap alpha, but does not have RGB, so the bitmap we see is a black bitmap. For information on bitmap ARGB, refer to here.
- In this method there are two parameters, one is the brush paint, and the other is the offset offsetxy. We can pass in the shadow effect above, so that the new bitmap edge will have shadow processing, and the offset offsetxy is used to specify the radius of the brush's shadow effect on the edge of the bitmap, which is constructed from the above Blurmaskfilter The radius parameter that is passed in is determined. One point to mention is that the newly obtained bitmap size is probably larger than the original bitmap. Assuming that the radius value passed in when the Blurmaskfilter is constructed is 6 and the original bitmap size is 144x144, then the newly obtained bitmap size is (144+6x2) x (144+6x2). Offset offsetxy[0]=offsetxy[1]=-6.
Original bitmap
Gets the new bitmap after the original bitmap transparent channel
Now that we have the bitmap with the shadow effect on the edge, we define it as Shadowalphabitmap, but this bitmap is not the final effect we expected, we need to stitch this bitmap with the original bitmap.
The concrete idea is this:
- First, define a new canvas
- Give this canvas a canvas to initialize an RGBA bitmap, the size of which is consistent with Shadowalphabitmap. A little more detail in this, to the canvas to initialize the bitmap must be the type of ismutable, meaning that the bitmap pixel is allowed to be modified, or it will be an error. For example, a bitmap loaded with a resource ID is not allowed to be changed and is not initialized for use by the canvas. The canvas source code is as follows:
/** * Construct a canvas with the specified bitmap to draw into. The bitmap * must be mutable. * * <p>the Initial target density of the canvas is the same as the given * bitmap ' s density. * * @param bitmap Specifies a mutable bitmap for the canvas to draw into. */ Public Canvas(Bitmap Bitmap) {if(!bitmap.ismutable ()) {Throw NewIllegalStateException ("immutable bitmap passed to Canvas constructor"); } throwifrecycled (bitmap); Mnativecanvas = Initraster (Bitmap.ni ()); Mfinalizer =NewCanvasfinalizer (Mnativecanvas); Mbitmap = bitmap; mdensity = bitmap.mdensity; }
- Drawing Shadowalphabitmap onto the canvas
- The original bitmap is made onto the canvas. It is important to note that the original bitmap is drawn, since the original bitmap is smaller than the Shadowalphabitmap, the-offsetxy[0], and-offsetxy[1], so we need to translate the original bitmap when we draw the original bitmap, This allows the original bitmap to be centered in the canvas. Between the original bitmap and the canvas is the shadow we expect to see.
The specific code is as follows:
Private Bitmap Getshadowbitmap (Bitmap srcbitmap) {Paint shadowpaint = new Paint ();Blurmaskfilter blurmaskfilter = new Blurmaskfilter (6, Blurmaskfilter. Blur. NORMAL);Shadowpaint. Setmaskfilter(Blurmaskfilter);int[] Offsetxy = new int[2];Bitmap Shadowbitmap = Srcbitmap. Extractalpha(SHADOWPAINT,OFFSETXY);Bitmap Canvasbgbitmap = Bitmap. CreateBitmap(Shadowbitmap. GetWidth(), Shadowbitmap. GetHeight(), Bitmap. Config. ARGB_8888);Canvas canvas = new Canvas ();Canvas. SetBitmap(Canvasbgbitmap);Canvas. Drawbitmap(Shadowbitmap,0,0, Shadowpaint);Canvas. Drawbitmap(Srcbitmap,-offsetxy[0],-offsetxy[1], NULL);Shadowbitmap. Recycle();Return Canvasbgbitmap;}
The last bitmap that gets the shadow effect on the edge
Summary:
To get a picture shadow effect, it is important to understand the following knowledge points:
-The use of blurmaskfilter.
-Bitmap The meaning and usage of the Extractalpha method.
-Bitmap ARGB Basic support
-Learn about modifying bitmaps with canvas canvases
Code GitHub Address
Android image Shadow processing analysis!