Simple flip effects of images in Android

Source: Internet
Author: User

Refer to Region:

Public class pagewidget extends view {private bitmap foreimage; private bitmap bgimage; private pointf touchpt; private int screenwidth; private int screenheight; private gradient; private gradientdrawable gradient; private colormatrixcolorfilter mcolormatrixfilter; private scroller mscroroller; private int lasttouchx; Public pagewidget (context) {super (Conte XT); // todo auto-generated constructor stub touchpt = new pointf (-1,-1); // argb A (0-transparent, 255-opaque) int [] color = {0xb03333, 0x00333333}; shadowdrawablerl = new gradientdrawable (gradientdrawable. orientation. right_left, color); shadowdrawablerl. setgradienttype (gradientdrawable. linear_gradient); shadowdrawablelr = new gradientdrawable (gradientdrawable. orientation. left_right, color); shadowdrawablelr. Setgradienttype (gradientdrawable. linear_gradient); float array [] = {0.55f, 0, 0, 0, 80 then f, 0, 0.55f, 0, 0, 80 then f, 0, 0, 0.55f, 0, 80 then F, 0, 0, 0, 0.2f, 0}; colormatrix CM = new colormatrix (); cm. set (array ); /** | A * 0.55 + 80 | * | r * 0.55 + 80 | * | G * 0.55 + 80 | * | B * 0.2 | * // cm. setsaturation (0); mcolormatrixfilter = new colormatrixcolorfilter (CM); // use the scroll bar to achieve the animated Effect of New scroller (con Text) ;}@ override public void computescroll () {// todo auto-generated method stub if (mscroller. computescroloffset () {touchpt. X = mscroller. getcurrx (); touchpt. y = mscroller. getcurry (); postinvalidate ();} else {// touchpt. X =-1; // touchpt. y =-1;} super. computescroll ();} public void setscreen (INT screenwidth, int screenheight) {This. screenwidth = screenwidth; this. screenheight = screenheigh T;} public bitmap getforeimage () {return foreimage;} public void setforeimage (Bitmap foreimage) {This. foreimage = foreimage;} public bitmap getbgimage () {return bgimage;} public void setbgimage (Bitmap bgimage) {This. bgimage = bgimage;} @ override protected void ondraw (canvas) {// todo auto-generated method stub drawpageeffect (canvas); super. ondraw (canvas);}/** draw foreground image * @ Param ca Nvas */private void drawforceimage (canvas) {// todo auto-generated method stub paint mpaint = new paint (); If (foreimage! = NULL) {canvas. drawbitmap (foreimage, 0, 0, mpaint);}/*** draw background image * @ Param canvas */private void drawbgimage (canvas, Path) {// todo auto-generated method stub paint mpaint = new paint (); If (bgimage! = NULL) {canvas. save (); // draw a canvas only at the intersection of the path. clippath (path, op. intersect); canvas. drawbitmap (bgimage, 0, 0, mpaint); canvas. restore () ;}/ *** paging effect * @ Param canvas */private void drawpageeffect (canvas) {// todo auto-generated method stub drawforceimage (canvas ); paint mpaint = new paint (); If (touchpt. x! =-1 & touchpt. y! =-1) {// flip the left-side canvas. drawline (touchpt. x, 0, touchpt. x, screenheight, mpaint); // shadowdrawablerl is painted on the left side of the book. setbounds (INT) touchpt. x-20, 0, (INT) touchpt. x, screenheight); shadowdrawablerl. draw (canvas); // float halfcut = touchpt. X + (screenwidth-touchpt. x)/2; canvas. drawline (halfcut, 0, halfcut, screenheight, mpaint); // flip the page on the left and rect backarea = new rect (INT) touchpt. x, 0, (INT) halfcut, screenheight); paint backpaint = new paint (); backpaint. setcolor (0xffdacab0); canvas. drawrect (backarea, backpaint); // flip the front of the flip image horizontally and pan it to touchpt. X point paint fbpaint = new paint (); fbpaint. setcolorfilter (mcolormatrixfilter); matrix = new matrix (); matrix. prescale (-1, 1); matrix. posttranslate (foreimage. getwidth () + touchpt. x, 0); canvas. save (); canvas. cliprect (backarea); canvas. drawbitmap (foreimage, matrix, fbpaint); canvas. restore (); // place the shadowdrawablerl on the left. setbounds (INT) halfcut-50, 0, (INT) halfcut, screenheight); shadowdrawablerl. draw (canvas); Path bgpath = New Path (); // The bgpath of the background image is displayed. addrect (New rectf (halfcut, 0, screenwidth, screenheight), direction. CW); // fold the background drawbgimage (canvas, bgpath) On the right side; // draw the shadowdrawablelr on the right side. setbounds (INT) halfcut, 0, (INT) halfcut + 50, screenheight); shadowdrawablelr. draw (canvas) ;}@ override public Boolean ontouchevent (motionevent event) {// todo auto-generated method stub if (event. getaction () = motionevent. action_down) {touchpt. X = event. getx (); touchpt. y = event. gety ();} else if (event. getaction () = motionevent. action_move) {lasttouchx = (INT) touchpt. x; touchpt. X = event. getx (); touchpt. y = event. gety (); postinvalidate ();} else if (event. getaction () = motionevent. action_up) {int dx, Dy; DY = 0; // slide right IF (lasttouchx <touchpt. x) {dx = foreimage. getwidth ()-(INT) touchpt. X + 30;} else {// slide dx =-(INT) touchpt to the left. x-foreimage. getwidth ();} mscroller. startscroll (INT) touchpt. x, (INT) touchpt. y, dx, Dy, 1000); postinvalidate () ;}// it must be true; otherwise, the action_move and action_up events return true;} cannot be obtained ;}} public class pageactivity extends activity {protected void oncreate (Android. OS. bundle savedinstancestate) {requestwindowfeature (window. feature_no_title); pagewidget = new pagewidget (this); display = getwindowmanager (). getdefadisplay display (); int width = display. getwidth (); int Height = display. getheight (); pagewidget. setscreen (width, height); bitmap bm1 = bitmapfactory. decoderesource (getresources (), R. drawable. pre7); bitmap bm2 = bitmapfactory. decoderesource (getresources (), R. drawable. after7); bitmap foreimage = bitmap. createscaledbitmap (bm1, width, height, false); bitmap bgimage = bitmap. createscaledbitmap (bm2, width, height, false); pagewidget. setbgimage (bgimage); pagewidget. setforeimage (foreimage); setcontentview (pagewidget); super. oncreate (savedinstancestate );};}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.