This article introduces an example of Android to achieve image shadow effect, set canvas color, image tilt effect, picture shadow effect method, using Canvas.save (Canvas.matrix_save_flag), to achieve. Because the actual size of the picture is larger than the displayed image, you need to change the size appropriately to achieve better results, in the original rectangle on the basis of a rounded rectangle, with a shadow layer. The reader can modify the code according to its own needs in order to meet the requirements of its own project.
The specific implementation code is as follows:
Package canvas.test;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.content.Context;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.graphics.PorterDuff;
Import Android.graphics.PorterDuff.Mode;
Import Android.graphics.PorterDuffXfermode;
Import Android.graphics.Rect;
Import Android.graphics.RectF;
Import android.graphics.drawable.Drawable;
Import Android.view.View; public class Shadereffect extends activity {@Override public void onCreate (Bundle savedinstancestate) {Super.onc
Reate (savedinstancestate);
Setcontentview (This) (new Imageeffect);
Class Imageeffect extends view{Paint Paint;
Public Imageeffect {Super (context);
Paint = new Paint ()//initializes the brush, which is used for shadow effects behind.
Paint.setantialias (TRUE);//antialiasing.
Paint.setshadowlayer (5f, 5.0f, 5.0f, color.black);//Set Shadow layer, this is the key. PaiNt.setxfermode (New Porterduffxfermode (mode.src_in));
@Override public void OnDraw (Canvas Canvas) {Super.ondraw (Canvas);
int posx = 20;
int posy = 50;
int picwidth,pichegiht;
drawable drawable = Getresources (). getdrawable (R.drawable.button); drawable DBE = Getresources (). getdrawable (R.drawable.button). mutate ();//If you do not invoke the Mutate method, the original image is also changed because the resource being invoked is the same one,
All objects are shared state.
drawable drawtest = Getresources (). getdrawable (R.drawable.button);
Bitmap bmp = Bitmapfactory.decoderesource (Getresources (), R.drawable.button);
Picwidth = Drawable.getintrinsicwidth ();
Pichegiht = Drawable.getintrinsicheight ();
Drawtest.setbounds (POSX, (2 * posy) + pichegiht, Posx + Picwidth, (2 * posy) + 2 * pichegiht);
Drawable.setbounds (POSX,POSY,POSX+PICWIDTH,POSY+PICHEGIHT);
Dbe.setbounds (0, 0, picwidth, pichegiht);
Canvas.drawcolor (Color.White);//Set Canvas color Canvas.save (canvas.matrix_save_flag); Dbe.setcolorfilter (0x7f000000,PORTERDUFF.MODE.SRC_IN);
Canvas.translate (posx + (int) (0.9 * picwidth/2), posy + PICHEGIHT/2);//image translation in order to create shadow effects just after the original artwork.
Canvas.skew ( -0.9f, 0.0F);//image tilt effect.
Canvas.scale (1.0f, 0.5f);//The image (in fact, the canvas) is scaled and the y direction is reduced to 1/2.
Dbe.draw (Canvas)//Here for the original image shadow effect map, than the first painting, it will be in the lower.
Drawable.clearcolorfilter ();
Canvas.restore ();
Canvas.save (Canvas.matrix_save_flag);
Drawable.draw (Canvas)//Here is the original image, because the canvas has a hierarchical effect, it will cover the shadow above.
Canvas.restore ();
Default no effect original Canvas.save (Canvas.matrix_save_flag);
Drawtest.draw (canvas);
Canvas.restore ();
Picture Shadow effect Canvas.save (Canvas.matrix_save_flag);
Rect Rect = new Rect (2*posx + picwidth, 2*posy + pichegiht, 2*posx + 2*picwidth, 2*posy + 2*pichegiht);//This is the theoretical shadow map coordinate
Rect Rect = new Rect (2*posx + picwidth + 3, 2*posy + pichegiht + 3, 2*posx + 2*picwidth-2, 2*posy + 2*pichegiht-2);
Since the actual size of the picture is larger than the displayed image, it is necessary to change the size appropriately to achieve better results rectf RECTF = new RECTF (rect); Canvas.drawroundreCT (RECTF, 10f, 10f, paint);//On the base of the original rectangle, draw a rounded rectangle with a shaded layer.
Canvas.drawbitmap (BMP, 2*posx + picwidth, 2*posy + pichegiht, NULL);//Draw the original artwork.
Canvas.restore (); }
}
}