Android画布Canvas裁剪效果演示--RevealView

ReavlView.gif
图片资源如下图,就是几张灰色和彩色图片
图片资源.jpg
思路是利用画布canvas的裁剪,先画出灰色的图片,再画出彩色图片并对彩色图片进行裁剪,先初始化一些参数
private Paint mPaint = new Paint();
    private Map<Integer, List<Bitmap>> bitmap = new HashMap<>();
    private float needClipWidth, needClipHeight;
    private float maxWidth, maxHeight;
    private float translateX;
    private float dx;
    private float currentX;

    private void init() {
        if (bitmap.size() != 0) return;

        mPaint.setDither(true);

        List<Bitmap> bitmaps1 = new ArrayList<>();
        bitmaps1.add(BitmapFactory.decodeResource(getResources(), R.drawable.avft));
        bitmaps1.add(BitmapFactory.decodeResource(getResources(), R.drawable.avft_active));
        bitmap.put(1, bitmaps1);

        List<Bitmap> bitmaps2 = new ArrayList<>();
        bitmaps2.add(BitmapFactory.decodeResource(getResources(), R.drawable.box_stack));
        bitmaps2.add(BitmapFactory.decodeResource(getResources(), R.drawable.box_stack_active));
        bitmap.put(2, bitmaps2);

        List<Bitmap> bitmaps3 = new ArrayList<>();
        bitmaps3.add(BitmapFactory.decodeResource(getResources(), R.drawable.bubble_frame));
        bitmaps3.add(BitmapFactory.decodeResource(getResources(), R.drawable.bubble_frame_active));
        bitmap.put(3, bitmaps3);

        List<Bitmap> bitmaps4 = new ArrayList<>();
        bitmaps4.add(BitmapFactory.decodeResource(getResources(), R.drawable.bubbles));
        bitmaps4.add(BitmapFactory.decodeResource(getResources(), R.drawable.bubbles_active));
        bitmap.put(4, bitmaps4);

        List<Bitmap> bitmaps5 = new ArrayList<>();
        bitmaps5.add(BitmapFactory.decodeResource(getResources(), R.drawable.bullseye));
        bitmaps5.add(BitmapFactory.decodeResource(getResources(), R.drawable.bullseye_active));
        bitmap.put(5, bitmaps5);

        List<Bitmap> bitmaps6 = new ArrayList<>();
        bitmaps6.add(BitmapFactory.decodeResource(getResources(), R.drawable.circle_filled));
        bitmaps6.add(BitmapFactory.decodeResource(getResources(), R.drawable.circle_filled_active));
        bitmap.put(6, bitmaps6);

        List<Bitmap> bitmaps7 = new ArrayList<>();
        bitmaps7.add(BitmapFactory.decodeResource(getResources(), R.drawable.circle_outline));
        bitmaps7.add(BitmapFactory.decodeResource(getResources(), R.drawable.circle_outline_active));
        bitmap.put(7, bitmaps7);

        //最大宽高
        maxWidth = bitmaps1.get(0).getWidth() * 7;
        maxHeight = bitmaps1.get(0).getHeight();
        if (maxWidth > getMeasuredWidth()) {//缩放
            float scaleX = getMeasuredWidth() / maxWidth;
            for (int i = 1; i <= 7; i++) {
                Bitmap newBitmap1 = Bitmap.createScaledBitmap(bitmap.get(i).get(0), (int) (bitmap.get(i).get(0).getWidth() * scaleX), (int) (bitmap.get(i).get(0).getHeight() * scaleX), true);
                bitmap.get(i).set(0, newBitmap1);
                Bitmap newBitmap2 = Bitmap.createScaledBitmap(bitmap.get(i).get(1), (int) (bitmap.get(i).get(1).getWidth() * scaleX), (int) (bitmap.get(i).get(1).getHeight() * scaleX), true);
                bitmap.get(i).set(1, newBitmap2);
            }

            maxWidth = getMeasuredWidth();
            maxHeight = maxHeight * scaleX;
        }
        //需要裁剪的宽高
        needClipWidth = maxWidth / 7;
        needClipHeight = maxHeight;
    }
在onDraw方法中进行处理,要注意的是,为了不影响灰色图片,需要新创建一层图层,对画布进行裁剪后,在新图层中画出彩色图片,由于canvas的绘图是根据当前绘图的matrix进行处理的,所以要先裁剪将matrix改变再调用canvas的draw方法,倒过来将没有效果
@Override
    protected void onDraw(Canvas canvas) {
        init();

        //先画有灰色的
        int nowLeft = 0;
        for (int i = 1; i <= 7; i++) {
            canvas.drawBitmap(bitmap.get(i).get(0), nowLeft, 0, mPaint);
            //偏移
            nowLeft += bitmap.get(i).get(0).getWidth();
        }

        int layerCount = canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), null, ALL_SAVE_FLAG);

        translateX += dx;
        //平移操作达到最右边或着最左边,停止
        if (translateX <= -(maxWidth / 2 - needClipWidth / 2)) {
            translateX = -(maxWidth / 2 - needClipWidth / 2);
        }
        if (translateX >= maxWidth / 2 - needClipWidth / 2) {
            translateX = maxWidth / 2 - needClipWidth / 2;
        }

        //将画布裁剪
        canvas.clipRect(new RectF(maxWidth / 2 - needClipWidth / 2 + translateX, 0, maxWidth / 2 + needClipHeight / 2 + translateX, needClipHeight));

        //再画有色彩的
        nowLeft = 0;
        for (int i = 1; i <= 7; i++) {
            canvas.drawBitmap(bitmap.get(i).get(1), nowLeft, 0, mPaint);
            //偏移
            nowLeft += bitmap.get(i).get(1).getWidth();
        }

        canvas.restoreToCount(layerCount);
    }
项目地址:https://gitee.com/aruba/CanvasApplication.git
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值