欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android中图片翻页效果简单的实现方法

程序员文章站 2023-12-06 10:29:22
复制代码 代码如下:public class pagewidget extends view {    private bitmap fore...

复制代码 代码如下:

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(context context) {
        super(context);
        // todo auto-generated constructor stub
        touchpt = new pointf(-1,-1);

        //argb a(0-透明,255-不透明)
        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);

        //利用滚动条来实现接触点放开后的动画效果
        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);
    }


    /**
     * 画前景图片
     * @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);
        }       
    }


    /**
     * 画背景图片
     * @param canvas
     */
    private void drawbgimage(canvas canvas,path path) {
        // todo auto-generated method stub
        paint mpaint = new paint();

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

            //只在与路径相交处画图
            canvas.clippath(path,op.intersect);
            canvas.drawbitmap(bgimage, 0, 0, mpaint);
            canvas.restore();
        }
    }


    /**
     * 画翻页效果
     * @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) {
            //翻页左侧书边
            canvas.drawline(touchpt.x, 0, touchpt.x,screenheight, mpaint);

            //左侧书边画阴影
            shadowdrawablerl.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);

            //对折处左侧画翻页页图片背面
            rect backarea = new rect((int)touchpt.x,0,(int)halfcut,screenheight);
            paint backpaint = new paint();
            backpaint.setcolor(0xffdacab0);
            canvas.drawrect(backarea, backpaint);

            //将翻页图片正面进行处理水平翻转并平移到touchpt.x点
            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();

            //对折处画左侧阴影
            shadowdrawablerl.setbounds((int)halfcut - 50, 0 ,(int)halfcut, screenheight);
            shadowdrawablerl.draw(canvas);

            path bgpath = new path();

            //可以显示背景图的区域
            bgpath.addrect(new rectf(halfcut,0,screenwidth,screenheight), direction.cw);

            //对折出右侧画背景
            drawbgimage(canvas,bgpath);

            //对折处画右侧阴影
            shadowdrawablelr.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;

            //向右滑动
            if (lasttouchx<touchpt.x) {
                dx = foreimage.getwidth() - (int)touchpt.x + 30;
            }
            else{
            //向左滑动
                dx = -(int)touchpt.x - foreimage.getwidth();
            }

            mscroller.startscroll((int)touchpt.x,(int)touchpt.y,dx,dy,1000);
            postinvalidate();
        }

        //必须为true,否则无法获取action_move及action_up事件
        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);
    };
}

android中图片翻页效果简单的实现方法