文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2022-06-06 10:45

关注

代码如下:
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);
       
//      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);
    }


   
    private void drawForceImage(Canvas canvas) {
        // TODO Auto-generated method stub
        Paint mPaint = new Paint();
        if (foreImage!=null) {
            canvas.drawBitmap(foreImage, 0, 0, mPaint);
        }       
    }


   
    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();
        }
    }


   
    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);
            //对折处画右侧阴影
     &n bsp;      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 中使用RecyclerView实现底部翻页解析Android中实现滑动翻页之ViewFlipper的使用详解Android实现阅读APP平移翻页效果android ViewPager实现滑动翻页效果实例代码Android自定义左右或上下滑动翻页效果基于Android实现3D翻页效果Android ViewPager实现左右滑动翻页效果Android利用悬浮按钮实现翻页效果Android 仿日历翻页、仿htc时钟翻页、数字翻页切换效果Android通过手势实现答题器翻页效果RecyclerView+PagerSnapHelper实现抖音首页翻页的Viewpager效果


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯