A simple way to realize the effect of image paging in Android _android

Source: Internet
Author: User

Copy Code code as follows:

public class Pagewidget extends View {
Private Bitmap foreimage;
Private Bitmap bgimage;
Private PointF Touchpt;
private int screenwidth;
private int screenheight;
Private Gradientdrawable Shadowdrawablerl;
Private Gradientdrawable SHADOWDRAWABLELR;
Private Colormatrixcolorfilter Mcolormatrixfilter;
Private Scroller Mscroller;
private int Lasttouchx;

Public Pagewidget {
Super (context);
TODO auto-generated Constructor stub
TOUCHPT = new PointF ( -1,-1);

ARGB A (0-transparent, 255-opaque)
int[] color = {0xb0333333, 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.0f,
, 0.55f, 0, 0, 80.0f,
, 0,0.55f, 0, 80.0f,
, 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);

Using the scroll bar to realize the animation effect of contact point release
Mscroller = new Scroller (context);
}

    @Override
    public void Computescroll () {
        //TODO auto-generated method stub
        if ( Mscroller.computescrolloffset ()) {
            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 = ScreenHeight;
}

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 Canvas) {
        //TODO auto-generated method stub
        Drawpageeffect (canvas);
        Super.ondraw (canvas);
   }


   /**
     * Picture foreground picture
     * @param canvas
     */
    private void Drawforceimage (Canvas Canvas) {
        //TODO auto-generated method stub
        Paint Mpaint = new Paint ();

        if (foreimage!=null) {
             Canvas.drawbitmap (foreimage, 0, 0, mpaint);
       }       
    }


   /**
     * picture background picture
     * @param canvas
     */
    private void Drawbgimage (Canvas canvas,path Path) {
        //TODO auto-generated method stub
        Paint mpaint = new Paint ();

        if (bgimage!=null) {
             Canvas.save ();

           //Paint only at the intersection of Paths
             Canvas.clippath (Path,op.intersect);
            canvas.drawbitmap (bgimage, 0, 0, mpaint);
            Canvas.restore ();
       }
   }


/**
* Picture Page effect
* @param canvas
*/
private void Drawpageeffect (Canvas Canvas) {
TODO auto-generated Method Stub
Drawforceimage (canvas);
Paint mpaint = new Paint ();
if (touchpt.x!=-1 && touchpt.y!=-1) {
Flip the left side of the book
Canvas.drawline (touchpt.x, 0, Touchpt.x,screenheight, mpaint);

Left side of the book Side painting Shadow
Shadowdrawablerl.setbounds ((int) touchpt.x-20, 0, (int) touchpt.x, screenheight);
Shadowdrawablerl.draw (canvas);

Flip the fold in the corner
Float Halfcut = touchpt.x + (screenwidth-touchpt.x)/2;
Canvas.drawline (halfcut, 0, Halfcut, ScreenHeight, Mpaint);

Fold the left side of the page to picture the back
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 page image horizontally and pan to the touchpt.x point
Paint fbpaint = new Paint ();
Fbpaint.setcolorfilter (Mcolormatrixfilter);
Matrix 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 ();

Draw left Shadow in fold
Shadowdrawablerl.setbounds ((int) halfCut-50, 0, (int) halfcut, screenheight);
Shadowdrawablerl.draw (canvas);

Path Bgpath = new Path ();

An area that can display a background image
Bgpath.addrect (New RECTF (halfcut,0,screenwidth,screenheight), DIRECTION.CW);

Fold the right drawing background
Drawbgimage (Canvas,bgpath);

Draw the right shadow in the fold
Shadowdrawablelr.setbounds ((int) halfcut, 0, (int) Halfcut +, 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 Left
DX =-(int) touchpt.x-foreimage.getwidth ();
}

Mscroller.startscroll ((int) touchpt.x, (int) touchpt.y,dx,dy,1000);
Postinvalidate ();
}

Must be true otherwise the action_move and ACTION_UP events cannot be obtained
return true;
}
}

public class Pageactivity extends activity {
protected void OnCreate (Android.os.Bundle savedinstancestate) {
Requestwindowfeature (Window.feature_no_title);

Pagewidget pagewidget = new Pagewidget (this);

Display display = Getwindowmanager (). Getdefaultdisplay ();
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);
};
}

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.