美文网首页
Bitmap位图渲染与操作(图片移动,放大,缩小,旋转,镜像)转

Bitmap位图渲染与操作(图片移动,放大,缩小,旋转,镜像)转

作者: 一粒渣 | 来源:发表于2017-03-06 15:53 被阅读0次

    位图操作主要有2中方式:

    1.使用canvas 画布操作:

                // ----------旋转位图(方式1)
                canvas.save();
                canvas.rotate(30, bmp.getWidth() / 2, bmp.getHeight() / 2);// 旋转弧度,旋转中心点x,旋转中心点y
                canvas.drawBitmap(bmp, bmp.getWidth() / 2 + 20,bmp.getHeight() + 10, paint);
                canvas.restore();
    
                // ----------平移位图(方式1)
                canvas.save();
                canvas.translate(100, 0);// 平移坐标X,Y
                canvas.drawBitmap(bmp, 0, 2 * bmp.getHeight() + 10, paint);
                canvas.restore();
    
                // ----------缩放位图(方式1)
                canvas.save();
                canvas.scale(2f, 2f, 50 + bmp.getWidth() / 2,50 + bmp.getHeight() / 2);// X方向缩放比例,Y方向缩放比例,缩放中心X,缩放中心Y
                canvas.drawBitmap(bmp, 150, 3 * bmp.getHeight() + 10, paint);
                canvas.restore();
    
                // ----------镜像反转位图(方式1)
                // X轴镜像
                canvas.save();
                canvas.scale(-1, 1, 100 + bmp.getWidth() / 2,100 + bmp.getHeight() / 2);// scale()当第一个参数为负数时表示x方向镜像反转,同理第二参数,镜像反转x,镜像反转Y
                canvas.drawBitmap(bmp, 0, 4 * bmp.getHeight() + 10, paint);
                canvas.restore();
                // Y轴镜像
                canvas.save();
                canvas.scale(1, -1, 100 + bmp.getWidth() / 2,100 + bmp.getHeight() / 2);
                canvas.drawBitmap(bmp, 4 * bmp.getHeight(),bmp.getWidth() + 10, paint);
                canvas.restore();
    

    2.通过矩阵操作位图:

               // ----------旋转位图(方式2)--通过矩阵旋转
                Matrix mx = new Matrix();
                mx.postRotate(60, bmp.getWidth() / 2, bmp.getHeight() / 2);// 旋转弧度,旋转中心点x,旋转中心点y
                canvas.drawBitmap(bmp, mx, paint);
    
                // ----------平移位图(方式2)--通过矩阵
                Matrix maT = new Matrix();
                maT.postTranslate(2 * bmp.getWidth(), 0);// 平移坐标X,Y
                canvas.drawBitmap(bmp, maT, paint);
    
                // ----------镜像反转位图(方式2)
                // X轴镜像
                Matrix maMiX = new Matrix();
                maMiX.postTranslate(0, 2 * bmp.getHeight());
                maMiX.postScale(-1, 1, 100 + bmp.getWidth() / 2,100 + bmp.getHeight() / 2);
                canvas.drawBitmap(bmp, maMiX, paint);
    
                // Y轴镜像
                Matrix maMiY = new Matrix();
                maMiY.postScale(1, -1, 100 + bmp.getWidth() / 2,100 + bmp.getHeight() / 2);
                canvas.drawBitmap(bmp, maMiY, paint);
    
                // ----------缩放位图(方式2)-放大
                Matrix maS = new Matrix();
                maS.postTranslate(200, 2 * bmp.getHeight());
                maS.postScale(2f, 2f, 50 + bmp.getWidth() / 2,50 + bmp.getHeight() / 2);// X方向缩放比例,Y方向缩放比例,缩放中心X,缩放中心Y
                canvas.drawBitmap(bmp, maS, paint);
                // ----------缩放位图(方式2)-缩小
                Matrix maS1 = new Matrix();
                maS1.postTranslate(0, 2 * bmp.getHeight());
                maS1.postScale(0.5f, 0.5f, 50 + bmp.getWidth() / 2,50 + bmp.getHeight() / 2);// X方向缩放比例,Y方向缩放比例,缩放中心X,缩放中心Y
                canvas.drawBitmap(bmp, maS1, paint);

    相关文章

      网友评论

          本文标题:Bitmap位图渲染与操作(图片移动,放大,缩小,旋转,镜像)转

          本文链接:https://www.haomeiwen.com/subject/blrugttx.html