Android中自訂常用的三個對象解析(Paint,Color,Canvas)

來源:互聯網
上載者:User

標籤:

Paint,Color,Canvas

Paint:畫筆對象,畫圖用的“筆”
Color:顏色,相當於調料
Canvas:畫布,現實中的紙板

Paint 畫筆

常用的方法就是設定和擷取到畫筆的樣式:

paint.setStyle();   設定畫筆的風格,空心的或者是實心的paint.setColor();   設定畫筆的顏色paint.setStrokeWidth(); 設定邊框線的寬度paint.setAlpha();   設定畫筆的Alpha值paint.setAntiAlias();       設定畫筆的鋸齒效果paint.setTextSize();        設定字型的大小paint.getAlpha();       擷取到畫筆的Alpha值paint.getColor();       擷取到畫筆的顏色paint.getTextBounds();  擷取到最小的能包容字型的邊框。。。。。。。
Color 顏色

通過Color.顏色名 ,來擷取到想要的顏色。
Color裡面設定了許多常用的顏色枚舉值,比如:

    @ColorInt public static final int BLACK       = 0xFF000000;    @ColorInt public static final int DKGRAY      = 0xFF444444;    @ColorInt public static final int GRAY        = 0xFF888888;    @ColorInt public static final int LTGRAY      = 0xFFCCCCCC;    @ColorInt public static final int WHITE       = 0xFFFFFFFF;    @ColorInt public static final int RED         = 0xFFFF0000;    @ColorInt public static final int GREEN       = 0xFF00FF00;    @ColorInt public static final int BLUE        = 0xFF0000FF;    @ColorInt public static final int YELLOW      = 0xFFFFFF00;    @ColorInt public static final int CYAN        = 0xFF00FFFF;    @ColorInt public static final int MAGENTA     = 0xFFFF00FF;    @ColorInt public static final int TRANSPARENT = 0;

同樣,也可以自訂想要的顏色:調用Color.argb()

  /**     * Return a color-int from alpha, red, green, blue components.     * These component values should be [0..255], but there is no     * range check performed, so if they are out of range, the     * returned color is undefined.     * @param alpha Alpha component [0..255] of the color     * @param red   Red component [0..255] of the color     * @param green Green component [0..255] of the color     * @param blue  Blue component [0..255] of the color     */    @ColorInt    public static int argb(int alpha, int red, int green, int blue) {        return (alpha << 24) | (red << 16) | (green << 8) | blue;    }
Canvas 畫布,可以繪製常見的圖形,比如圓,直線,矩形繪製直線:canvas.drawLine();
    /**     * Draw a line segment with the specified start and stop x,y coordinates,     * using the specified paint.     *     * <p>Note that since a line is always "framed", the Style is ignored in the paint.</p>     *     * <p>Degenerate lines (length is 0) will not be drawn.</p>     *     * @param startX The x-coordinate of the start point of the line     * @param startY The y-coordinate of the start point of the line     * @param paint  The paint used to draw the line     */    public void **drawLine**(float startX, float startY, float stopX, float stopY,            @NonNull Paint paint)
繪製圓:canvas.drawCircle();
    /**     * Draw the specified circle using the specified paint. If radius is <= 0,     * then nothing will be drawn. The circle will be filled or framed based     * on the Style in the paint.     *     * @param cx     The x-coordinate of the center of the cirle to be drawn     * @param cy     The y-coordinate of the center of the cirle to be drawn     * @param radius The radius of the cirle to be drawn     * @param paint  The paint used to draw the circle     */    public void **drawCircle**(float cx, float cy, float radius, @NonNull Paint paint)
繪製矩形:;canvas.drawRect();
    /**     * Draw the specified Rect using the specified paint. The rectangle will     * be filled or framed based on the Style in the paint.     *     * @param left   The left side of the rectangle to be drawn     * @param top    The top side of the rectangle to be drawn     * @param right  The right side of the rectangle to be drawn     * @param bottom The bottom side of the rectangle to be drawn     * @param paint  The paint used to draw the rect     */    public void **drawRect**(float left, float top, float right, float bottom, @NonNull Paint paint)
繪製圖片:canvas.drawBitmap();
     /**     * Draw the specified bitmap, with its top/left corner at (x,y), using     * the specified paint, transformed by the current matrix.     *     * <p>Note: if the paint contains a maskfilter that generates a mask which     * extends beyond the bitmap‘s original width/height (e.g. BlurMaskFilter),     * then the bitmap will be drawn as if it were in a Shader with CLAMP mode.     * Thus the color outside of the original width/height will be the edge     * color replicated.     *     * <p>If the bitmap and canvas have different densities, this function     * will take care of automatically scaling the bitmap to draw at the     * same density as the canvas.     *     * @param bitmap The bitmap to be drawn     * @param left   The position of the left side of the bitmap being drawn     * @param top    The position of the top side of the bitmap being drawn     * @param paint  The paint used to draw the bitmap (may be null)     */    public void **drawBitmap**(@NonNull Bitmap bitmap, float left, float top, @Nullable Paint paint) {        throwIfCannotDraw(bitmap)
繪製文本:canvas.drawText();
    /**     * Draw the text, with origin at (x,y), using the specified paint. The     * origin is interpreted based on the Align setting in the paint.     *     * @param text  The text to be drawn     * @param x     The x-coordinate of the origin of the text being drawn     * @param y     The y-coordinate of the baseline of the text being drawn     * @param paint The paint used for the text (e.g. color, size, style)     */    public void **drawText**(@NonNull String text, float x, float y, @NonNull Paint paint)
繪製圓弧:canvas.drawArc();
    /**     * <p>Draw the specified arc, which will be scaled to fit inside the     * specified oval.</p>     *     * <p>If the start angle is negative or >= 360, the start angle is treated     * as start angle modulo 360.</p>     *     * <p>If the sweep angle is >= 360, then the oval is drawn     * completely. Note that this differs slightly from SkPath::arcTo, which     * treats the sweep angle modulo 360. If the sweep angle is negative,     * the sweep angle is treated as sweep angle modulo 360</p>     *     * <p>The arc is drawn clockwise. An angle of 0 degrees correspond to the     * geometric angle of 0 degrees (3 o‘clock on a watch.)</p>     *     * @param oval       The bounds of oval used to define the shape and size     *                   of the arc     * @param startAngle Starting angle (in degrees) where the arc begins     * @param sweepAngle Sweep angle (in degrees) measured clockwise     * @param useCenter If true, include the center of the oval in the arc, and                        close it if it is being stroked. This will draw a wedge     * @param paint      The paint used to draw the arc     */    public void **drawArc**(@NonNull RectF oval, float startAngle, float sweepAngle, boolean useCenter,            @NonNull Paint paint)

。。。。。。。

小結

自訂view的時候一般會重寫onDraw()方法,畫圖時必要的三要素:Color,paint,canvas

設定畫筆的樣式設定顏色在畫布上作圖

Android中自訂常用的三個對象解析(Paint,Color,Canvas)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.