Canvas drawLine - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Canvas drawLine method.

This code example shows how to use the following methods:drawArc, drawLine
    private void updateBitmap() { 
        if (mRect == null || mRect.width() == 0) {
            return; 
        } 
        mBitmap = Bitmap.createBitmap((int) mRect.width(), (int) mRect.height(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(mBitmap);
        canvas.drawArc(mRect, -90, 360, true, mPaintBackground);
        if (mValue < 0.01f) {
            canvas.drawLine(mRect.width() / 2, mRect.height() / 2, mRect.width() / 2, 0, mPaintForeground);
        } 
        float angle = mValue * 360;
        canvas.drawArc(mRect, -90, angle, true, mPaintForeground);
        if (!mPieStyle) {
            canvas.drawArc(mRectInner, -90, 360, true, mPaintErase);
        } 
        postInvalidate();
    } 
} 
8
CodeRank
This code example shows how to use the following methods:drawLine, drawRect, drawText, getClipBounds
 
        effect.setEffect(paint);
        paint.setColor(Color.DKGRAY);
 
        paint.setStrokeWidth(5);
        canvas.drawLine(20, 10, 140, 20, paint);
 
        paint.setTextSize(26);
        canvas.drawText("Android", 40, 50, paint);
 
        // create a new Paint to draw the widget structure 
        paint = new Paint();
        paint.setColor(Color.BLACK);
        canvas.drawText(String.valueOf(id), 2.0F, 12.0F, paint);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);
        canvas.drawRect(canvas.getClipBounds(), paint);
    } 
} 

8
CodeRank
This code example shows how to use the following methods:drawCircle, drawColor, drawLine, restore, save
        /* 
         * Draw a wireframe cube by drawing 12 3 dimensional lines between 
         * adjacent corners of the cube 
         */ 
        void drawCube(Canvas c) {
            c.save();
            c.translate(mCenterX, mCenterY);
            c.drawColor(0xff000000);
            drawLine(c, -400, -400, -400,  400, -400, -400);
            drawLine(c,  400, -400, -400,  400,  400, -400);
            drawLine(c,  400,  400, -400, -400,  400, -400);
            drawLine(c, -400,  400, -400, -400, -400, -400);
 
            drawLine(c, -400, -400,  400,  400, -400,  400);
            drawLine(c,  400, -400,  400,  400,  400,  400);
            drawLine(c,  400,  400,  400, -400,  400,  400);
            drawLine(c, -400,  400,  400, -400, -400,  400);
 
            drawLine(c, -400, -400,  400, -400, -400, -400);
            drawLine(c,  400, -400,  400,  400, -400, -400);

8
CodeRank
This code example shows how to use the following methods:drawCircle, drawLine, drawRect, drawText
	protected void onDraw(Canvas canvas) {
		int xPoint = getMeasuredWidth() / 2;
		int yPoint = getMeasuredHeight() / 2;
 
		float radius = (float) (Math.max(xPoint, yPoint) * 0.6);
		canvas.drawCircle(xPoint, yPoint, radius, paint);
		canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), paint);
 
		// 3.143 is a good approximation for the circle 
		canvas.drawLine(
				xPoint,
				yPoint,
				(float) (xPoint + radius
						* Math.sin((double) (-position) / 180 * 3.143)),
				(float) (yPoint - radius
						* Math.cos((double) (-position) / 180 * 3.143)), paint);
 
		canvas.drawText(String.valueOf(position), xPoint, yPoint, paint);
 
	} 

7
CodeRank
This code example shows how to use the following methods:drawARGB, drawBitmap, drawLine, drawPath, drawRect
 
        @Override 
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
 
            canvas.drawARGB(255, 255, 255, 255);
 
            canvas.save();
            canvas.translate(200.0f, 60.0f);
            canvas.drawPath(mPath, mSmallPaint);
 
            canvas.translate(350.0f, 0.0f);
            canvas.drawPath(mPath, mMediumPaint);
 
            mLargePaint.setShader(mShader);
            canvas.translate(350.0f, 0.0f);
            canvas.drawPath(mPath, mLargePaint);
            mLargePaint.setShader(null);
            canvas.restore();
 

7
CodeRank
This code example shows how to use the following methods:drawLine, drawLines
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(10);
 
        // default 
        canvas.drawLine(30, 30, 100, 30, paint);
 
        // 直線の始端と終端で何もしません。 
        paint.setStrokeCap(Paint.Cap.BUTT);
        canvas.drawLine(30, 50, 100, 50, paint);
 
        // 直線の始端と終端を丸くします。 
        paint.setStrokeCap(Paint.Cap.ROUND);
        canvas.drawLine(30, 70, 100, 70, paint);
 
        // 直線の始端と終端を四角形にします。 
        paint.setStrokeCap(Paint.Cap.SQUARE);
        canvas.drawLine(30, 90, 100, 90, paint);
 
        // 座標の配列を指定 

7
CodeRank
This code example shows how to use the following methods:drawLine, drawRect
This code example shows how to use the following methods:drawLine, getHeight, getWidth
    @Override 
    protected void onDraw(Canvas canvas) {
        Paint paint = new Paint();
        paint.setColor(Color.RED);
 
        float endX = canvas.getWidth();
        float centerX = endX / 2.0f;
        float endY = canvas.getHeight();
        float centerY = endY / 2.0f;
 
        // 横線 
        canvas.drawLine(0, centerY, endX, centerY, paint);
        // 縦線 
        canvas.drawLine(centerX, 0, centerX, endY, paint);
    } 
} 
7
CodeRank
This code example shows how to use the following methods:drawCircle, drawLine
		final int width = mappointcenter.x * 2;
 
		final int height_2 = mappointcenter.y;
		final int width_2 = mappointcenter.x;
 
		canvas.drawCircle(width_2, height_2, this.mCenterCircleRadius, this.mPaint);
 
		/* Draw line from left to the centercircle. */ 
		canvas.drawLine(0, height_2, width_2 - this.mCenterCircleRadius, height_2, this.mPaint);
 
		/* Draw line from right to the centercircle. */ 
		canvas.drawLine(width_2 + this.mCenterCircleRadius, height_2, width, height_2, this.mPaint);
 
		/* Draw line from top to the centercircle. */ 
		canvas.drawLine(width_2, 0, width_2, height_2 - this.mCenterCircleRadius, this.mPaint);
 
		/* Draw line from bottom to the centercircle. */ 
		canvas.drawLine(width_2, height_2 + this.mCenterCircleRadius, width_2, height, this.mPaint);
	} 
 

7
CodeRank
This code example shows how to use the following methods:drawLine
canvas.drawLines(copyPoints, 0, 12, p);
        } 
 
        private void drawVerticalLine(Canvas canvas, Paint p, float length, float x, float y) {
            canvas.drawLine(x, y, x, y + length, p);
        } 
 
        private void drawDiagonalLine(Canvas canvas, Paint p, float length, float x, float y) {
            canvas.drawLine(x, y, x + length, y + length, p);
        } 
 
        @Override 
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            Paint p = new Paint();
            p.setColor(mColor);
            float yOffset = 10;
 
            canvas.save();
7
CodeRank

Related Canvas drawLine Questions & Answers:

drawLine Method Overview
Draw a line segment with the specified start and stop x,y coordinates, using the specified paint.
See Code Examples for other Android Canvas Methods: