<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 自定義View > 參考文章:[http://www.gcssloop.com/customview/CustomViewIndex/ ?](http://www.gcssloop.com/customview/CustomViewIndex/ "http://www.gcssloop.com/customview/CustomViewIndex/ ?") ### 1、坐標系 * **Android View中的y軸正方向是向下的** * **View的坐標系是相對于父控件而言的** ``` getTop(); //獲取子View左上角距父View頂部的距離 getLeft(); //獲取子View左上角距父View左側的距離 getBottom(); //獲取子View右下角距父View頂部的距離 getRight(); //獲取子View右下角距父View左側的距離 ``` * MotionEvent中getX( )和getRawX( )的區別 getX( )獲取的是相對于父控件的 getRawX( ) 獲取的是相對于屏幕默認的坐標軸 ![image.png](https://upload-images.jianshu.io/upload_images/13971762-235a13f4720ede7c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ### 2、自定義View的流程 ![自定義View的流程.png](https://upload-images.jianshu.io/upload_images/13971762-092f1f8bec8128d4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) | 操作類型 | 相關API | 備注 | |--|--|--| | 操作類型 | 相關API | 備注 | | 繪制顏色 | drawColor, drawRGB, drawARGB | 使用單一顏色填充整個畫布 | | 繪制基本形狀 | drawPoint, drawPoints, drawLine, drawLines, drawRect, drawRoundRect, drawOval, drawCircle, drawArc | 依次為 點、線、矩形、圓角矩形、橢圓、圓、圓弧| | 繪制圖片 | drawBitmap, drawPicture | 繪制位圖和圖片 | | 繪制文本 | drawText, drawPosText, drawTextOnPath | 依次為 繪制文字、繪制文字時指定每個文字位置、根據路徑繪制文字 | | 繪制路徑 | drawPath | 繪制路徑,繪制貝塞爾曲線時也需要用到該函數| | 頂點操作 | drawVertices, drawBitmapMesh | 通過對頂點操作可以使圖像形變,drawVertices直接對畫布作用、drawBitmapMesh只對繪制的Bitmap作用| | 畫布剪裁 | clipPath, clipRect | 設置畫布的顯示區域 | | 畫布快照 | save, restore, saveLayerXxx, restoreToCount, getSaveCount | 依次為 保存當前狀態、 回滾到上一次保存的狀態、 保存圖層狀態、 回滾到指定狀態、 獲取保存次數 | | 畫布變換 | translate, scale, rotate, skew | 依次為 位移、縮放、 旋轉、錯切| | Matrix(矩陣) | getMatrix, setMatrix, concat | 實際上畫布的位移,縮放等操作的都是圖像矩陣Matrix, 只不過Matrix比較難以理解和使用,故封裝了一些常用的方法。 | ### 3、View 的save和restore | **相關API** | 簡介 | |--|--| | save | 把當前的畫布的狀態進行保存,然后放入特定的棧中 | | saveLayerXxx | 新建一個圖層,并放入特定的棧中 | | restore | 把棧中最頂層的畫布狀態取出來,并按照這個狀態恢復當前的畫布 | | restoreToCount | 彈出指定位置及其以上所有的狀態,并按照指定位置的狀態進行恢復| | getSaveCount | 獲取棧中內容的數量(即保存次數)| ![layer.png](https://upload-images.jianshu.io/upload_images/13971762-cb9518064cd7548c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ``` // save 使用 // 保存全部狀態 public int save () // 根據saveFlags參數保存一部分狀態 public int save (int saveFlags) saveFlags: // ALL_SAVE_FLAG 默認,保存全部狀態 // CLIP_SAVE_FLAG 保存剪輯區 // CLIP_TO_LAYER_SAVE_FLAG 剪裁區作為圖層保存 // FULL_COLOR_LAYER_SAVE_FLAG 保存圖層的全部色彩通道 // HAS_ALPHA_LAYER_SAVE_FLAG 保存圖層的alpha(不透明度)通道 // MATRIX_SAVE_FLAG 保存Matrix信息( translate, rotate, scale, skew) ``` ``` // saveLayer使用 // 無圖層alpha(不透明度)通道 public int saveLayer (RectF bounds, Paint paint) public int saveLayer (RectF bounds, Paint paint, int saveFlags) public int saveLayer (float left, float top, float right, float bottom, Paint paint) public int saveLayer (float left, float top, float right, float bottom, Paint paint, int saveFlags) // 有圖層alpha(不透明度)通道 public int saveLayerAlpha (RectF bounds, int alpha) public int saveLayerAlpha (RectF bounds, int alpha, int saveFlags) public int saveLayerAlpha (float left, float top, float right, float bottom, int alpha) public int saveLayerAlpha (float left, float top, float right, float bottom, int alpha, int saveFlags) ``` > **saveLayerXxx方法會讓你花費更多的時間去渲染圖像(圖層多了相互之間疊加會導致計算量成倍增長),使用前請謹慎,如果可能,盡量避免使用。使用saveLayerXxx方法,也會將圖層狀態也放入狀態棧中,同樣使用restore方法進行恢復** ##### restore 狀態回滾,就是從棧頂取出一個狀態然后根據內容進行恢復。 同樣以上面狀態棧圖片為例,調用一次restore方法則將狀態棧中第5次取出,根據里面保存的狀態進行狀態恢復。 ##### restoreToCount 彈出指定位置以及以上所有狀態,并根據指定位置狀態進行恢復。 以上面狀態棧圖片為例,如果調用restoreToCount(2) 則會彈出 2 3 4 5 的狀態,并根據第2次保存的狀態進行恢復。 ##### getSaveCount 獲取保存的次數,即狀態棧中保存狀態的數量,以上面狀態棧圖片為例,使用該函數的返回值為5。不過,**該函數的最小返回值為1**,即使彈出了所有的狀態,返回值依舊為1,代表默認狀態。 ### 4、常用操作、方法 ``` canvas.translate(mWidth / 2, mHeight / 2); // 移動坐標系到屏幕中心 canvas.scale(1,-1); // 翻轉y坐標軸 ``` > ***@param forceMoveTo** If true, always begin a new contour with the arc* > > *// 默認值為false,默認使用lineTo連接到**畫圓弧前的最后那個點**;如果為true,則強制移動到**畫圓弧前的最后那個點。*** > > **public void** arcTo(RectF oval, **float** startAngle, **float** sweepAngle,**boolean** forceMoveTo) ``` // 判斷path是否是一個矩形,如果是一個矩形的話,會將矩形的信息存放進參數rect中。 public boolean isRect (RectF rect) //eg path.lineTo(0,400); path.lineTo(400,400); path.lineTo(400,0); path.lineTo(0,0); RectF rect = new RectF(); boolean b = path.isRect(rect); Log.e("Rect","isRect:"+b+"| left:"+rect.left+"| top:"+rect.top+"| right:"+rect.right+"| bottom:"+rect.bottom); ``` > com.sloop.canvas E/Rect: isRect:true| left:0.0| top:0.0| right:400.0| bottom:400.0 # 動畫 > 參考:[https://github.com/OCNYang/Android-Animation-Set](https://github.com/OCNYang/Android-Animation-Set "https://github.com/OCNYang/Android-Animation-Set") > > [https://blog.csdn.net/yanbober/article/details/46481171](https://blog.csdn.net/yanbober/article/details/46481171 "https://blog.csdn.net/yanbober/article/details/46481171")[](https://github.com/OCNYang/Android-Animation-Set)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看