jekeyhui99 发表于 2018-9-13 10:51:05

(AS3)利用BitmapData.draw对位图进行翻转缩放操作

<p style="margin: 10px auto; color: rgb(102, 102, 102); font-family: Arial, 微软雅黑, 宋体, sans-serif; font-size: 12px; background-color: rgb(221, 221, 221);">Sprite对象有2个关于缩放的属性scaleX、scaleY,分别是缩放x轴和y轴。</p><p style="margin: 10px auto; color: rgb(102, 102, 102); font-family: Arial, 微软雅黑, 宋体, sans-serif; font-size: 12px; background-color: rgb(221, 221, 221);">看下面代码:</p><p style="margin: 10px auto; color: rgb(102, 102, 102); font-family: Arial, 微软雅黑, 宋体, sans-serif; font-size: 12px; background-color: rgb(221, 221, 221);">&nbsp;</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-size: 12px; font-family: &quot;Courier New&quot; !important;"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="color: rgb(6, 129, 186); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px; height: auto;"></a></span></div><div><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span>&nbsp;c:Sprite&nbsp;=&nbsp;new&nbsp;Sprite;<br>c.x&nbsp;=&nbsp;200;<br>addChild(c);<br><br><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span>&nbsp;s:Sprite&nbsp;=&nbsp;new&nbsp;Sprite;<br>s.graphics.beginFill(0xff0000);<br>s.graphics.drawRect(0,0,100,100);<br>s.graphics.endFill();<br>s.graphics.beginFill(0x00ff00);<br>s.graphics.drawRect(100,0,100,100);<br>s.graphics.endFill();<br>c.addChild(s);<br><br><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">s.scaleX&nbsp;=&nbsp;-1;</span></div><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="color: rgb(6, 129, 186); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px; height: auto;"></a></span></div></div><p style="margin: 10px auto; color: rgb(102, 102, 102); font-family: Arial, 微软雅黑, 宋体, sans-serif; font-size: 12px; background-color: rgb(221, 221, 221);">&nbsp;</p><p style="margin: 10px auto; color: rgb(102, 102, 102); font-family: Arial, 微软雅黑, 宋体, sans-serif; font-size: 12px; background-color: rgb(221, 221, 221);">下图分别是注释和没注释最后一句代码的运行结果:</p><p style="margin: 10px auto; color: rgb(102, 102, 102); font-family: Arial, 微软雅黑, 宋体, sans-serif; font-size: 12px; background-color: rgb(221, 221, 221);"><img src="https://images.cnblogs.com/cnblogs_com/yili16438/QQ%E6%88%AA%E5%9B%BE20120427163705.jpg" width="441" height="186" alt="" style="border: 0px; max-width: 900px; height: auto;"><img src="https://images.cnblogs.com/cnblogs_com/yili16438/QQ%E6%88%AA%E5%9B%BE20120427163722.jpg" width="459" height="177" alt="" style="border: 0px; max-width: 900px; height: auto;"><br></p><p style="margin: 10px auto; color: rgb(102, 102, 102); font-family: Arial, 微软雅黑, 宋体, sans-serif; font-size: 12px; background-color: rgb(221, 221, 221);">&nbsp;</p><p style="margin: 10px auto; color: rgb(102, 102, 102); font-family: Arial, 微软雅黑, 宋体, sans-serif; font-size: 12px; background-color: rgb(221, 221, 221);">把注释去掉可以发现,该对象已经水平翻转了一下,不过看过来却好像坐标往左移了200个像素,实际上只是像素变了,坐标并没有变。</p><p style="margin: 10px auto; color: rgb(102, 102, 102); font-family: Arial, 微软雅黑, 宋体, sans-serif; font-size: 12px; background-color: rgb(221, 221, 221);">不过这里可以发现,当把scaleX会水平翻转,那scaleY会怎么样呢?没错,是垂直翻转了!</p><p style="margin: 10px auto; color: rgb(102, 102, 102); font-family: Arial, 微软雅黑, 宋体, sans-serif; font-size: 12px; background-color: rgb(221, 221, 221);">&nbsp;</p><p style="margin: 10px auto; color: rgb(102, 102, 102); font-family: Arial, 微软雅黑, 宋体, sans-serif; font-size: 12px; background-color: rgb(221, 221, 221);">由此为基础,使用BitmapData.draw,配合Matrix,就可以实现一个位图或者对象的水平翻转和垂直翻转,下面是实现代码:</p><p style="margin: 10px auto; color: rgb(102, 102, 102); font-family: Arial, 微软雅黑, 宋体, sans-serif; font-size: 12px; background-color: rgb(221, 221, 221);">&nbsp;</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-size: 12px; font-family: &quot;Courier New&quot; !important;"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="color: rgb(6, 129, 186); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px; height: auto;"></a></span></div><div>package&nbsp;org.easily.utils<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">import</span>&nbsp;flash.display.BitmapData;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">import</span>&nbsp;flash.geom.Matrix;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">public</span>&nbsp;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">class</span>&nbsp;BitmapDataUtils<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">水平翻转一个位图</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"><br></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">public</span>&nbsp;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">static</span>&nbsp;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span>&nbsp;flipHorizontal(bmpData:BitmapData,&nbsp;transparent:<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">Boolean</span>&nbsp;=&nbsp;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">true</span>,&nbsp;fillColor:uint&nbsp;=&nbsp;0):BitmapData<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span>&nbsp;matrix:Matrix&nbsp;=&nbsp;new&nbsp;Matrix();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matrix.a&nbsp;=&nbsp;-1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matrix.tx&nbsp;=&nbsp;bmpData.width;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span>&nbsp;bmpData_:BitmapData&nbsp;=&nbsp;new&nbsp;BitmapData(bmpData.width,&nbsp;bmpData.height,&nbsp;transparent,&nbsp;fillColor);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bmpData_.draw(bmpData,&nbsp;matrix);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span>&nbsp;bmpData_;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">垂直翻转一个位图</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"><br></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">public</span>&nbsp;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">static</span>&nbsp;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span>&nbsp;flipVertical(bmpData:BitmapData,&nbsp;transparent:<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">Boolean</span>&nbsp;=&nbsp;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">true</span>,&nbsp;fillColor:uint&nbsp;=&nbsp;0):BitmapData<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span>&nbsp;matrix:Matrix&nbsp;=&nbsp;new&nbsp;Matrix();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matrix.d&nbsp;=&nbsp;-1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matrix.ty&nbsp;=&nbsp;bmpData.height;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span>&nbsp;bmpData_:BitmapData&nbsp;=&nbsp;new&nbsp;BitmapData(bmpData.width,&nbsp;bmpData.height,&nbsp;transparent,&nbsp;fillColor);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bmpData_.draw(bmpData,&nbsp;matrix);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span>&nbsp;bmpData_;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">缩放位图</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"><br></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">public</span>&nbsp;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">static</span>&nbsp;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span>&nbsp;scaleBitmapData(bmpData:BitmapData,&nbsp;scaleX:<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">Number</span>,&nbsp;scaleY:<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">Number</span>):BitmapData<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span>&nbsp;matrix:Matrix&nbsp;=&nbsp;new&nbsp;Matrix();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matrix.scale(scaleX,&nbsp;scaleY);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span>&nbsp;bmpData_:BitmapData&nbsp;=&nbsp;new&nbsp;BitmapData(scaleX&nbsp;*&nbsp;bmpData.width,&nbsp;scaleY&nbsp;*&nbsp;bmpData.height,&nbsp;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">true</span>,&nbsp;0);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bmpData_.draw(bmpData,&nbsp;matrix);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span>&nbsp;bmpData_;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}</div><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="color: rgb(6, 129, 186); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px; height: auto;"></a></span></div></div><p style="margin: 10px auto; color: rgb(102, 102, 102); font-family: Arial, 微软雅黑, 宋体, sans-serif; font-size: 12px; background-color: rgb(221, 221, 221);">&nbsp;</p><p style="margin: 10px auto; color: rgb(102, 102, 102); font-family: Arial, 微软雅黑, 宋体, sans-serif; font-size: 12px; background-color: rgb(221, 221, 221);">&nbsp;</p><p style="margin: 10px auto; color: rgb(102, 102, 102); font-family: Arial, 微软雅黑, 宋体, sans-serif; font-size: 12px; background-color: rgb(221, 221, 221);">最后一个缩放位图的方法适用于等比例缩放一个对象,位图引擎里面应该也有类似的实现。</p><p></p>
页: [1]
查看完整版本: (AS3)利用BitmapData.draw对位图进行翻转缩放操作