Cómo dibujar una línea curva en android?
Soy nuevo en android y estoy desarrollando un proyecto de muestra en líneas de dibujo. Quiero dibujar una línea curva o elevada que conecte dos puntos (x1, y1 y x2, y2). He intentado el método canvas.drawArc (), pero los valores RectF dentro del método drawArc son sólo los puntos centrales x, y del círculo. Me está dando un arco entre mis dos puntos. Pero quiero una línea curva que conecte exactamente mis dos puntos. ¿Puede alguien ayudarme? Gracias por adelantado.
- Dibujar una línea con un cierto ancho de píxel
- Dibuja un árbol (literalmente) con círculos en Android: no es una estructura de árbol
- Inclinar un mapa de bits sólo en la dirección vertical
- Cómo encontrar la lista de puntos en la línea sobre lienzo android
- Android - dibujo táctil
- Dibujo personalizado en la lona del mapa de Mapbox
- Cómo dibujar una porción de mapa de bits a través de lienzo DrawBitmap
- Android canvas drawText y-posición del texto
- Cómo dibujar el texto sobre lienzo?
- Dibujo sobre lienzo - PorterDuff.Mode.CLEAR dibuja negro! ¿Por qué?
- Android: ¿Cómo manejar el dibujo con métodos de lienzo en diferentes tamaños de pantalla?
- ¿Cómo configurar un filtro de color para XWalkView? (Resuelto)
- Cómo rotar un lienzo en un punto específico usando android.graphics.Camera.rotateX (angle)
Declare este método dentro del método onDraw:
private void drawOvalAndArrow(Canvas canvas){ Paint circlePaint = new Paint(); circlePaint.setStyle(Paint.Style.FILL_AND_STROKE); circlePaint.setAntiAlias(true); circlePaint.setStrokeWidth(2); circlePaint.setColor(Color.CYAN); float centerWidth = canvas.getWidth()/2; //get center x of display float centerHeight = canvas.getHeight()/2; //get center y of display float circleRadius = 20; //set radius float circleDistance = 200; //set distance between both circles //draw circles canvas.drawCircle(centerWidth, centerHeight, circleRadius, circlePaint); canvas.drawCircle(centerWidth+circleDistance, centerHeight, circleRadius, circlePaint); //to draw an arrow, just lines needed, so style is only STROKE circlePaint.setStyle(Paint.Style.STROKE); circlePaint.setColor(Color.RED); //create a path to draw on Path arrowPath = new Path(); //create an invisible oval. the oval is for "behind the scenes" ,to set the path´ //area. Imagine this is an egg behind your circles. the circles are in the middle of this egg final RectF arrowOval = new RectF(); arrowOval.set(centerWidth, centerHeight-80, centerWidth + circleDistance, centerHeight+80); //add the oval to path arrowPath.addArc(arrowOval,-180,180); //draw path on canvas canvas.drawPath(arrowPath, circlePaint); //draw arrowhead on path start arrowPath.moveTo(centerWidth,centerHeight ); //move to the center of first circle arrowPath.lineTo(centerWidth-circleRadius, centerHeight-circleRadius);//draw the first arrowhead line to the left arrowPath.moveTo(centerWidth,centerHeight );//move back to the center arrowPath.lineTo(centerWidth+circleRadius, centerHeight-circleRadius);//draw the next arrowhead line to the right //same as above on path end arrowPath.moveTo(centerWidth+circleDistance,centerHeight ); arrowPath.lineTo((centerWidth+circleDistance)-circleRadius, centerHeight-circleRadius); arrowPath.moveTo(centerWidth+circleDistance,centerHeight ); arrowPath.lineTo((centerWidth+circleDistance)+circleRadius, centerHeight-circleRadius); //draw the path canvas.drawPath(arrowPath,circlePaint); }
También encontrará los dos lados de la pantalla (modo de paisaje) y dibujará una curva perfecta a través de la pantalla
protected void onDraw(Canvas canvas) { super.onDraw(canvas); PointF mPoint1 = new PointF(w/1.2F, h/1.2F); PointF mPoint2 = new PointF(w/24, h/1.2F); Path myPath1 = new Path(); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setStyle(Style.STROKE); paint.setStrokeWidth(2); paint.setColor(Color.WHITE); myPath1 = drawCurve(canvas, paint, mPoint1, mPoint2); canvas.drawPath(myPath1, paint); } private Path drawCurve(Canvas canvas, Paint paint, PointF mPointa, PointF mPointb) { Path myPath = new Path(); myPath.moveTo(63*w/64, h/10); myPath.quadTo(mPointa.x, mPointa.y, mPointb.x, mPointb.y); return myPath; }
Referencias útiles sobre la pintura en android:
Cómo dibujar Arcos en Android con lienzo?
Pintura básica con vistas
Puede que no sea lo que quieres, pero echa un vistazo a http://developer.android.com/reference/android/graphics/Path.html más precisamente en moveTo, lineTo, quadTo y cubicTo. (Los 2 últimos métodos dibujarán curvas de bezier, ya sea cuadrático o cúbico.Si u no saben lo que son, eche un vistazo a http://en.wikipedia.org/wiki/B%C3%A9zier_curve Sólo es necesario entender los parámetros de la función, no la matemática detrás de ella). Para tu propósito puedes hacer así:
Path mPath; Paint paint; mPath = new Path(); mPath.moveTo(x1, y1); mPath.cubicTo(anchor1_x, anchor1_y, anchor2_x, anchor2_y, x2, y2); /*the anchors you want, the curve will tend to reach these anchor points; look at the wikipedia article to understand more */ paint = new Paint(); paint.setColor(0xFFFFFFFF); paint.setAntiAlias(true); paint.setStrokeCap(Paint.Cap.ROUND); paint.setStrokeJoin(Paint.Join.ROUND); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(width); //the width you want canvas.drawPath(mPath, paint);