Paint、Canvas

来源:http://www.cnblogs.com/galibujianbusana/archive/2017/09/07/7489128.html
-Advertisement-
Play Games

1:Canvas類 2:Paint類 3:簡單幾何圖形繪製 4.1:drawRect矩形繪製 4.2:drawLine 4.3:橢圓繪製 4.4:弧形繪製 4.5:drawPath方法 4.6:drawText,drawTextOnpath,drawTextRun ...


1.Canvas類

 
public class Canvas {
    public static final int ALL_SAVE_FLAG = 31;
    /** @deprecated */
    @Deprecated
    public static final int CLIP_SAVE_FLAG = 2;
    /** @deprecated */
    @Deprecated
    public static final int CLIP_TO_LAYER_SAVE_FLAG = 16;
    /** @deprecated */
    @Deprecated
    public static final int FULL_COLOR_LAYER_SAVE_FLAG = 8;
    /** @deprecated */
    @Deprecated
    public static final int HAS_ALPHA_LAYER_SAVE_FLAG = 4;
    /** @deprecated */
    @Deprecated
    public static final int MATRIX_SAVE_FLAG = 1;

    public Canvas() 
    public Canvas(Bitmap bitmap) ;
    public boolean isHardwareAccelerated() ;
    public void setBitmap(Bitmap bitmap) ;
    public boolean isOpaque() ;
    public int getWidth() ;
    public int getHeight();
    public int getDensity();
    public void setDensity(int density) ;
    public int getMaximumBitmapWidth() ;
    public int getMaximumBitmapHeight() ;
    public int save() ;

    /** @deprecated */
    @Deprecated
    public int save(int saveFlags) ;
    /** @deprecated */
    @Deprecated
    public int saveLayer(RectF bounds, Paint paint, int saveFlags) ;
    public int saveLayer(RectF bounds, Paint paint) ;
    /** @deprecated */
    @Deprecated
    public int saveLayer(float left, float top, float right, float bottom, Paint paint, int saveFlags);
    public int saveLayer(float left, float top, float right, float bottom, Paint paint) ;
    /** @deprecated */
    @Deprecated
    public int saveLayerAlpha(RectF bounds, int alpha, int saveFlags) ;
    public int saveLayerAlpha(RectF bounds, int alpha) ;
    /** @deprecated */
    @Deprecated
    public int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, int saveFlags);
    public int saveLayerAlpha(float left, float top, float right, float bottom, int alpha);
    public void restore() ;
    public int getSaveCount() ;
    public void restoreToCount(int saveCount);
    public void translate(float dx, float dy);
    public void scale(float sx, float sy);
    public final void scale(float sx, float sy, float px, float py);
    public void rotate(float degrees) ;
    public final void rotate(float degrees, float px, float py);
    public void skew(float sx, float sy) ;
    public void concat(Matrix matrix) ;

    
    public void setMatrix(Matrix matrix) ;
    /** @deprecated */
    @Deprecated
    public void getMatrix(Matrix ctm) ;
    /** @deprecated */
    @Deprecated
    public final Matrix getMatrix() ;
    /** @deprecated */
    @Deprecated
    public boolean clipRect(RectF rect, Op op);
    /** @deprecated */
    @Deprecated
    public boolean clipRect(Rect rect, Op op) ;
    public boolean clipRect(RectF rect) ;
    public boolean clipOutRect(RectF rect);
    public boolean clipRect(Rect rect);
    public boolean clipOutRect(Rect rect) ;

    /** @deprecated */
    @Deprecated
    public boolean clipRect(float left, float top, float right, float bottom, Op op) ;

    public boolean clipRect(float left, float top, float right, float bottom);

    public boolean clipOutRect(float left, float top, float right, float bottom);

    public boolean clipRect(int left, int top, int right, int bottom);

    public boolean clipOutRect(int left, int top, int right, int bottom);

    /** @deprecated */
    @Deprecated
    public boolean clipPath(Path path, Op op);

    public boolean clipPath(Path path);

    public boolean clipOutPath(Path path) ;

    public DrawFilter getDrawFilter();

    public void setDrawFilter(DrawFilter filter) ;

    public boolean quickReject(RectF rect, Canvas.EdgeType type);

    public boolean quickReject(Path path, Canvas.EdgeType type) ;

    public boolean quickReject(float left, float top, float right, float bottom, Canvas.EdgeType type);

    public boolean getClipBounds(Rect bounds) ;

    public final Rect getClipBounds() ;

    public void drawPicture(Picture picture) ;

    public void drawPicture(Picture picture, RectF dst);

    public void drawPicture(Picture picture, Rect dst) ;
    

    /***
    oval :      圓弧的外輪廓矩形區域。
    startAngle: 圓弧起始角度,單位為度。
    sweepAngle: 圓弧掃過的角度,順時針方向,單位為度
    useCenter: 如果為True時,在繪製圓弧時將圓心包括在內,通常用來繪製扇形。如果為false會繪製切弦 看圖吧
    paint: 繪製圓弧的畫板屬性,如顏色,是否填充等。**/
    public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint);
    public void drawArc(float left, float top, float right, float bottom, float startAngle, float sweepAngle, boolean useCenter, Paint paint) ;
    
    
    /**  **/
    public void drawARGB(int a, int r, int g, int b) ;

    public void drawBitmap(Bitmap bitmap, float left, float top, Paint paint) ;

    public void drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint);

    public void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint);

  
    @Deprecated
    public void drawBitmap(int[] colors, int offset, int stride, float x, float y, int width, int height, boolean hasAlpha, Paint paint) ;


    @Deprecated
    public void drawBitmap(int[] colors, int offset, int stride, int x, int y, int width, int height, boolean hasAlpha, Paint paint);

    public void drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint);

    public void drawBitmapMesh(Bitmap bitmap, int meshWidth, int meshHeight, float[] verts, int vertOffset, int[] colors, int colorOffset, Paint paint);

    
    /*** 繪製圓形,圓心坐標cx,cy,半徑 radius,paint */
    public void drawCircle(float cx, float cy, float radius, Paint paint) ;

    /*** 繪製橢圓 */
    public void drawOval(RectF oval, Paint paint);
    public void drawOval(float left, float top, float right, float bottom, Paint paint);
    
    public void drawColor(int color) ;

    public void drawColor(int color, Mode mode) ;

    /*****  繪製線段:pos 的數量需要是4個倍數 ******/
    public void drawLine(float startX, float startY, float stopX, float stopY, Paint paint) ;
    public void drawLines(float[] pts, int offset, int count, Paint paint) ;
    public void drawLines(float[] pts, Paint paint) ;

    

    public void drawPaint(Paint paint);

    public void drawPath(Path path, Paint paint);

    public void drawPoint(float x, float y, Paint paint);

    public void drawPoints(float[] pts, int offset, int count, Paint paint) ;
    public void drawPoints(float[] pts, Paint paint) ;

    @Deprecated
    public void drawPosText(char[] text, int index, int count, float[] pos, Paint paint);

    @Deprecated
    public void drawPosText(String text, float[] pos, Paint paint);

    /*** 繪製矩形 */
    public void drawRect(RectF rect, Paint paint) ;
    public void drawRect(Rect r, Paint paint);
    public void drawRect(float left, float top, float right, float bottom, Paint paint);
    
    
    

    public void drawRGB(int r, int g, int b);

    /******** 繪製圓角矩形,REctF,x軸上的圓角半徑,y軸上圓角半徑,panint *******/
    public void drawRoundRect(RectF rect, float rx, float ry, Paint paint);
    public void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, Paint paint);

    

    /*** 繪製text文字,從text的start到end,繪製中心點在(x,y),*/
    public void drawText(String text, int start, int end, float x, float y, Paint paint) ;
    public void drawText(CharSequence text, int start, int end, float x, float y, Paint paint);
    public void drawText(char[] text, int index, int count, float x, float y, Paint paint);
    public void drawText(String text, float x, float y, Paint paint);
    
    
    /*** 繪製text文字,str ,path,(path是繪製的text的路徑,如:path.addArc,文字會隨著arc弧形繪製 )**/

    public void drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint) ;
    public void drawTextOnPath(char[] text, int index, int count, Path path, float hOffset, float vOffset, Paint paint) ;
    
    
    public void drawTextRun(char[] text, int index, int count, int contextIndex, int contextCount, float x, float y, boolean isRtl, Paint paint);
    public void drawTextRun(CharSequence text, int start, int end, int contextStart, int contextEnd, float x, float y, boolean isRtl, Paint paint);

    
    
    public void drawVertices(Canvas.VertexMode mode, int vertexCount, float[] verts, int vertOffset, float[] texs, int texOffset, int[] colors, int colorOffset, short[] indices, int indexOffset, int indexCount, Paint paint) ;

    public static enum VertexMode {
        TRIANGLES,
        TRIANGLE_FAN,
        TRIANGLE_STRIP;

        private VertexMode() {
        }
    }

    public static enum EdgeType {
        AA,
        BW;

        private EdgeType() {
        }
    }
}
    
    
View Code

2.Paint類

 

public class Paint {
    public static final int ANTI_ALIAS_FLAG = 1;
    public static final int DEV_KERN_TEXT_FLAG = 256;
    public static final int DITHER_FLAG = 4;
    public static final int EMBEDDED_BITMAP_TEXT_FLAG = 1024;
    public static final int FAKE_BOLD_TEXT_FLAG = 32;
    public static final int FILTER_BITMAP_FLAG = 2;
    public static final int HINTING_OFF = 0;
    public static final int HINTING_ON = 1;
    public static final int LINEAR_TEXT_FLAG = 64;
    public static final int STRIKE_THRU_TEXT_FLAG = 16;
    public static final int SUBPIXEL_TEXT_FLAG = 128;
    public static final int UNDERLINE_TEXT_FLAG = 8;

    public Paint();

    public Paint(int flags);

    public Paint(Paint paint);

    public void reset();

    public void set(Paint src);

    public int getFlags() ;

    public void setFlags(int flags) ;

    public int getHinting();
    public void setHinting(int mode);
    public final boolean isAntiAlias();

    public void setAntiAlias(boolean aa);

    public final boolean isDither() ;
    public void setDither(boolean dither);
    public final boolean isLinearText() ;
    public void setLinearText(boolean linearText);
    public final boolean isSubpixelText();
    public void setSubpixelText(boolean subpixelText);
    public final boolean isUnderlineText();

    public void setUnderlineText(boolean underlineText);
    public final boolean isStrikeThruText();
    public void setStrikeThruText(boolean strikeThruText);
    public final boolean isFakeBoldText();
    public void setFakeBoldText(boolean fakeBoldText);
    public final boolean isFilterBitmap() ;
    public void setFilterBitmap(boolean filter);
    public Paint.Style getStyle() ;

    public void setStyle(Paint.Style style);
    public int getColor();
    public void setColor(int color) ;
    public int getAlpha() ;
    public void setAlpha(int a);
    public void setARGB(int a, int r, int g, int b) ;
    public float getStrokeWidth();
    public void setStrokeWidth(float width);
    public float getStrokeMiter() ;
    public void setStrokeMiter(float miter);

    public Paint.Cap getStrokeCap();
    public void setStrokeCap(Paint.Cap cap);
    public Paint.Join getStrokeJoin() ;
    public void setStrokeJoin(Paint.Join join);
    public boolean getFillPath(Path src, Path dst) ;
    public Shader getShader() ;
    public Shader setShader(Shader shader) ;
    public ColorFilter getColorFilter() ;
    public ColorFilter setColorFilter(ColorFilter filter);
    public Xfermode getXfermode() ;
    public Xfermode setXfermode(Xfermode xfermode);
    public PathEffect getPathEffect() ;
    public PathEffect setPathEffect(PathEffect effect);
    public MaskFilter getMaskFilter();
    public MaskFilter setMaskFilter(MaskFilter maskfilter);
    public Typeface getTypeface() ;
    public Typeface setTypeface(Typeface typeface) ;
    public void setShadowLayer(float radius, float dx, float dy, int shadowColor);
    public void clearShadowLayer();
    public Paint.Align getTextAlign();
    public void setTextAlign(Paint.Align align);
    public Locale getTextLocale() ;
    public LocaleList getTextLocales() ;
    public void setTextLocale(Locale locale);
    public void setTextLocales(LocaleList locales);
    public boolean isElegantTextHeight() ;
    public void setElegantTextHeight(boolean elegant) ;
    public float getTextSize();
    public void setTextSize(float textSize);
    public float getTextScaleX();
    public void setTextScaleX(float scaleX);
    public float getTextSkewX() ;
    public void setTextSkewX(float skewX) ;
    public float getLetterSpacing();
    public void setLetterSpacing(float letterSpacing) ;
    public String getFontFeatureSettings();
    public void setFontFeatureSettings(String settings);
    public String getFontVariationSettings() ;
    public boolean setFontVariationSettings(String fontVariationSettings) ;
    public float ascent() ;
    public float descent();
    public float getFontMetrics(Paint.FontMetrics metrics);
    public Paint.FontMetrics getFontMetrics();
    public int getFontMetricsInt(Paint.FontMetricsInt fmi);
    public Paint.FontMetricsInt getFontMetricsInt();
    public float getFontSpacing();
    public float measureText(char[] text, int index, int count);
    public float measureText(String text, int start, int end);
    public float measureText(String text) ;
    public float measureText(CharSequence text, int start, int end);
    public int breakText(char[] text, int index, int count, float maxWidth, float[] measuredWidth);
    public int breakText(CharSequence text, int start, int end, boolean measureForwards, float maxWidth, float[] measuredWidth);
    public int breakText(String text, boolean measureForwards, float maxWidth, float[] measuredWidth) ;
    public int getTextWidths(char[] text, int index, int count, float[] widths);
    public int getTextWidths(CharSequence text, int start, int end, float[] widths);
    public int getTextWidths(String text, int start, int end, float[] widths);
    public int getTextWidths(String text, float[] widths);
    public void getTextPath(char[] text, int index, int count, float x, float y, Path path);
    public void getTextPath(String text, int start, int end, float x, float y, Path path) ;
    public void getTextBounds(String text, int start, int end, Rect bounds) ;
    public void getTextBounds(char[] text, int index, int count, Rect bounds);
    public boolean hasGlyph(String string);
    public float getRunAdvance(char[] text, int start, int end, int contextStart, int contextEnd, boolean isRtl, int offset);
    public float getRunAdvance(CharSequence text, int start, int end, int contextStart, int contextEnd, boolean isRtl, int offset);
    public int getOffsetForAdvance(char[] text, int start, int end, int contextStart, int contextEnd, boolean isRtl, float advance);
    public int getOffsetForAdvance(CharSequence text, int start, int end, int contextStart, int contextEnd, boolean isRtl, float advance) ;
    public static class FontMetricsInt {
        public int ascent;
        public int bottom;
        public int descent;
        public int leading;
        public int top;

        public FontMetricsInt() {
            throw new RuntimeException("Stub!");
        }

        public String toString() {
            throw new RuntimeException("Stub!");
        }
    }

    public static class FontMetrics {
        public float ascent;
        public float bottom;
        public float descent;
        public float leading;
        public float top;

        public FontMetrics() {
            throw new RuntimeException("Stub!");
        }
    }

    public static enum Align {
        CENTER,
        LEFT,
        RIGHT;

        private Align() {
        }
    }

    public static enum Join {
        BEVEL,
        MITER,
        ROUND;

        private Join() {
        }
    }

    public static enum Cap {
        BUTT,
        ROUND,
        SQUARE;

        private Cap() {
        }
    }

    public static enum Style {
        FILL,
        FILL_AND_STROKE,
        STROKE;

        private Style() {
        }
    }
}
View Code

 

 簡單幾何圖形繪製

 

3.drawRect矩形繪製

 

    /*** 繪製矩形 */
    public void drawRect(RectF rect, Paint paint) ;
    public void drawRect(Rect r, Paint paint);
    public void drawRect(float left, float top, float right, float bottom, Paint paint);
   /*** 繪製圓角矩形,REctF,x軸上的圓角半徑,y軸上圓角半徑,panint ****/
    public void drawRoundRect(RectF rect, float rx, float ry, Paint paint);
    public void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, Paint paint);    

 

4.drawLine繪製線段

/*****  繪製線段:pts 的數量需要是4的倍數 ******/
public void drawLine(float startX, float startY, float stopX, float stopY, Paint    paint) ;
public void drawLines(float[] pts, int offset, int count, Paint paint) ;
public void drawLines(float[] pts, Paint paint) ;
      Paint rectPaint = new Paint();
        rectPaint.setAntiAlias(true);
        rectPaint.setColor(Color.GRAY);
        rectPaint.setStyle(Paint.Style.STROKE);
        rectPaint.setStrokeWidth(5);
        rectPaint.setAntiAlias(true);
        rectPaint.setStyle(Paint.Style.FILL);
        rectPaint.setColor(0x88FF0000);
        /***** pos 的數量需要是4個倍數*/
       float []pos=new float[]{10,10,10,100,100,100,100,240};
        /***** pos 每4個一組,4個float代表2個點坐標,然後連接成線*/
       // canvas.drawLines(pos,rectPaint);
        /**** pos,從點pos[2]到pos[5]*/
        canvas.drawLines(pos,rectPaint);

 5.繪製圓或橢圓

    /*** 繪製圓形,圓心坐標cx,cy,半徑 radius,paint */
    public void drawCircle(float cx, float cy, float radius, Paint paint) ;

    /*** 繪製橢圓 */
    public void drawOval(RectF oval, Paint paint);
    public void drawOval(float left, float top, float right, float bottom, Paint paint);

6.繪製弧形

    /***
    oval :      圓弧的外輪廓矩形區域。
    startAngle: 圓弧起始角度,單位為度。
    sweepAngle: 圓弧掃過的角度,順時針方向,單位為度
    useCenter:  如果為True時,在繪製圓弧時將圓心包括在內,通常用來繪製扇形。如果為false會繪製切弦 看圖吧
    paint:     圓弧的畫板屬性,如顏色,是否填充等。**/
    public void drawArc(RectF oval, float star

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • iOS 讓視圖UIView 單獨顯示某一側的邊框線 有時候需要讓view顯示某一側的邊框線,這時設置layer的border是達不到效果的。在網上查閱資料發現有一個投機取巧的辦法,原理是給view的layer再添加一個layer,讓這個layer充當邊框線的角色。根據這個方法我寫了一個函數,有需要的 ...
  • 監聽簡訊(監聽系統廣播) ...
  • 普通廣播接收者和有序廣播接收者 接收者的操作和普通情況下一樣 在有序情況下,優先順序不相同的情況下先執行優先順序高的,優先順序相同的情況下先執行先在Manifest中註冊的。 無序的話順序是任意的。 ...
  • 自定義廣播 自定義廣播就是我們自己來寫廣播發送者,也自己來寫廣播接收者。 效果圖: 分析: 1、自己寫好廣播發送者 設置廣播的id,廣播接收者監聽的時候需要監聽這個 intent.setAction("com.fry.receiver"); 這是給廣播接收者帶數據,沒這個也行 intent.putE ...
  • 動態註冊廣播接收者 這一段代碼,和靜態註冊廣播接收者下麵的代碼作用是一樣的: 都是用來監聽打電話應用的。 動態註冊的話,第二次打開應用的時候不能正常發揮作用。 適用場景: 靜態註冊:適合程式結束後還要監聽的廣播。比如ip撥號。 動態註冊:適合程式結束後不要監聽的廣播,比如界面上顯示的數據是廣播接收者 ...
  • ”只能在UI主線程中更新View“。 這句話很熟悉吧? 來來,哥們,看一下下麵的例子 代碼這麼寫,不是逗比嗎!肯定崩啊!但是,如果你試一下,你會發現,絕大多數是不會崩的。至於極少數會崩潰的原因,我一會再說。 你可能會很疑惑,不是”只能在UI主線程中更新View“嗎?你這個在子線程裡面更新View,為 ...
  • 廣播接收者實現IP撥號 效果圖: 實現的功能就是自動監聽我們要撥打的號碼,在我們撥打的號碼前加上179521 分析: 1、敲個類來繼承廣播接收者 並且將從打電話應用位置獲取的號碼加上179521,並將修改後的號碼返回給打電話APP 2、指定廣播接收者監聽的對象 這個就是指定我們的廣播接收者監聽電話應 ...
  • 參考: Android入門:廣播發送者與廣播接收者 - xiazdong - CSDN博客http://blog.csdn.net/xiazdong/article/details/7768807 一、廣播發送者&廣播接收者介紹 1.廣播接收者 廣播接收者簡單地說就是接收廣播意圖的Java類,此Ja ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...