Paint、Canvas_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > Paint、Canvas

Paint、Canvas

 2017/9/7 21:08:54  咖喱不见不散啊  程序员俱乐部  我要评论(0)
  • 摘要:1.Canvas类publicclassCanvas{publicstaticfinalintALL_SAVE_FLAG=31;/**@deprecated*/@DeprecatedpublicstaticfinalintCLIP_SAVE_FLAG=2;/**@deprecated*/@DeprecatedpublicstaticfinalintCLIP_TO_LAYER_SAVE_FLAG=16
  • 标签:can

1.Canvas类

class="code_img_closed" src="/Upload/Images/2017090721/0015B68B3C38AA5B.gif" alt="">
 
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() {
        }
    }
}
    
    
logs_code_collapse">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 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) ;

7.drawPath 绘制图形

 public void drawPath(Path path, Paint paint);

 

        Paint pathPaint = new Paint();
        pathPaint.setARGB(70, 238, 18, 137);
        pathPaint.setStyle(Paint.Style.FILL);
        pathPaint.setAntiAlias(true);
        pathPaint.setStrokeWidth(5);
        pathPaint.setColor(Color.CYAN);
        pathPaint.setStyle(Paint.Style.FILL);
        Path path = new Path();
        /*** 这个是上图的第三个 */
        path.moveTo(100,100);
        path.lineTo(50,300);
        path.lineTo(300,100);
        path.lineTo(350,300);
        canvas.drawPath(path,pathPaint);

按上图drawpath绘制图形,有moveTo()作为起始点,lineTo连接到各个顶点。第四个图的梯形是moveTo(A),LineTo(B),LineTo(c) 然后再次moveTo(D),LineTo(B),LineTo(C)的

 8.绘制drawText 

    /*** 绘制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);
/*** 绘制text文字,从text的index,绘制count个字数,绘制起点在(x,y),*/
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);

 

 drawTextOnPath方法:

 

    /*** 绘制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) ;

 

 

 

 Path path=new Path();
 path.addRoundRect(100,100,300,300,10,10, Path.Direction.CW);
/*** path.addRoundRect(100,100,300,300,10,10, Path.Direction.CWW); */ canvas.drawTextOnPath(
"这里是drawTextOnPath方法", path, 20, 20, rectPaint);
       Paint rectPaint = new Paint();
        rectPaint.setAntiAlias(true);
        rectPaint.setColor(Color.RED);
        rectPaint.setStrokeWidth(5);
        rectPaint.setAntiAlias(true);
        rectPaint.setStyle(Paint.Style.STROKE);
        rectPaint.setColor(0x88FF0000);
        rectPaint.setTextSize(40);

 

CW和CWW的区别:顺时针和逆时针

 

 

path.addArc()中没有Direction属性,绘制图形大致为下:

 drawTextRun方法

 

 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);

 

上面的CAN SHU 呢index开始下标,count绘制的数目,contextIndex和index一样(否则就异常退出了) contextCount>=count 。该方法要求api 23呢,isRtl改变了绘制的顺序,

false为正序,true为倒叙了。

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            canvas.drawTextRun("眼前风景不足停,不远之处是梦想".toCharArray(), 0, 7, 0, 7, 200, 500, false, rectPaint);
            canvas.drawTextRun("眼前风景不足停,不远之处是梦想".toCharArray(), 0, 7, 0, 9, 200, 600, true, rectPaint);
            canvas.drawTextRun("眼前风景不足停,不远之处是梦想".toCharArray(), 0, 4, 0, 9, 200, 700, false, rectPaint);
            canvas.drawTextRun("眼前风景不足停,不远之处是梦想".toCharArray(), 0, 4, 0, 9, 200, 800, true, rectPaint);

        }

如图:

 

发表评论
用户名: 匿名