Android: steering trajectory path? - android

I want to draw trajectory path based on car steering move. here I have few dummy data InnerRadius, OuterRadius and SteeringWheelangle.
public void drawCurvedArrow(float x1, float y1, float x2, float y2, float curveRadius, int lineWidth,Canvas canvas) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(lineWidth);
paint.setColor(Color.YELLOW);
final Path path = new Path();
float midX = x1 + ((x2 - x1) / 2);
float midY = y1 + ((y2 - y1) / 2);
float xDiff = midX - x1;
float yDiff = midY - y1;
double angle = (Math.atan2(yDiff, xDiff) * (180 / Math.PI)) - 90;
double angleRadians = Math.toRadians(angle);
float pointX = (float) (midX + curveRadius * Math.cos(angleRadians));
float pointY = (float) (midY + curveRadius * Math.sin(angleRadians));
path.moveTo(x1, y1);
path.cubicTo(x1,y1,pointX, pointY, x2, y2);
canvas.drawPath(path, paint);
}
like this.

Related

android canvas draw line with pointer ended arrows both side. can anyone help me out with it.?

#Override
public void onDraw(Canvas canvas) {
/*canvas.drawLine(0, 0, 20, 20, paint);
canvas.drawLine(200, 0, 0, 200, paint);*/
/*canvas.drawLine(downxpos, downypos, upxpos, upypos, paint);*/
for (Line l : lines) {
canvas.drawLine(l.startX, l.startY, l.stopX, l.stopY, paint);
}
}
example image
I Would like to draw the line like the image I show in canvas. I can draw the line but don't know how to add arrows to it.
Use these Methods to draw Arrow on both sides.
private void drawArrow1(Canvas canvas, Paint paint) {
double degree = calculateDegree(x, x1, y, y1);
float endX1 = (float) (x1 + ((10) * Math.cos(Math.toRadians((degree-30)+90))));
float endY1 = (float) (y1 + ((10) * Math.sin(Math.toRadians(((degree-30)+90)))));
float endX2 = (float) (x1 + ((10) * Math.cos(Math.toRadians((degree-60)+180))));
float endY2 = (float) (y1 + ((10) * Math.sin(Math.toRadians(((degree-60)+180)))));
canvas.drawLine(x1,y1,endX1,endY1,paint);
canvas.drawLine(x1, y1, endX2,endY2,paint);
}
private void drawArrow(Canvas canvas, Paint paint) {
double degree1 = calculateDegree(x1, x, y1, y);
float endX11 = (float) (x + ((10) * Math.cos(Math.toRadians((degree1-30)+90))));
float endY11 = (float) (y + ((10) * Math.sin(Math.toRadians(((degree1-30)+90)))));
float endX22 = (float) (x + ((10) * Math.cos(Math.toRadians((degree1-60)+180))));
float endY22 = (float) (y + ((10) * Math.sin(Math.toRadians(((degree1-60)+180)))));
canvas.drawLine(x,y,endX11,endY11,paint);
canvas.drawLine(x,y,endX22,endY22,paint);
}
public double calculateDegree(float x1, float x2, float y1, float y2) {
float startRadians = (float) Math.atan((y2 - y1) / (x2 - x1));
System.out.println("radian=====" + Math.toDegrees(startRadians));
startRadians += ((x2 >= x1) ? 90 : -90) * Math.PI / 180;
return Math.toDegrees(startRadians);
}
Where x,y is starting point and x1,y1 is ending point.

Wrap multiline text in canvas

I have used the following code to draw text,
public void drawText(Canvas canvas, int _x, int _y, float scale, float rotate) {
if (TextUtils.isEmpty(mText)) {
return;
}
int x = _x;
int y = _y;
mPaint.getTextBounds(mText, 0, mText.length(), mTextRect);
mTextRect.offset(x - (mTextRect.width() >> 1), y);
mHelpBoxRect.set(mTextRect.left - PADDING, mTextRect.top - PADDING
, mTextRect.right + PADDING, mTextRect.bottom + PADDING);
RectUtil.scaleRect(mHelpBoxRect, scale);
canvas.save();
canvas.scale(scale, scale, mHelpBoxRect.centerX(), mHelpBoxRect.centerY());
canvas.rotate(rotate, mHelpBoxRect.centerX(), mHelpBoxRect.centerY());
canvas.drawText(mText, x, y, mPaint);
canvas.restore();
}
Following is scaleRect method,
public static void scaleRect(RectF rect, float scale) {
float w = rect.width();
float h = rect.height();
float newW = scale * w;
float newH = scale * h;
float dx = (newW - w) / 2;
float dy = (newH - h) / 2;
rect.left -= dx;
rect.top -= dy;
rect.right += dx;
rect.bottom += dy;
}
The issue is that, the text is not wrapping in case if it is of multiline. How will I be able to let the text wrap in case of multi line text?

Rotate a view like a knob

I have to modify some one's project . getTheta is a method in that project which i didn't understand but it help to rotate view rotate like a knob (not fully 360 but less on b/s)
private float getTheta(float x, float y)
{
float sx = x - (float)getWidth() / 2.0F;
float sy = y - (float)getHeight() / 2.0F;
float length = (float)Math.sqrt(sx * sx + sy * sy);
float nx = sx / length;
float theta = 57.29578F * (float)Math.atan2(sy / length, nx);
if (theta < 0.0F)
{
theta += 360F;
}
return theta;
}
What i find method of getting theta is
private float getMyTheta(float x, float y) {
float sx = x - (getWidth() / 2.0f);
float sy = y - (getHeight() / 2.0f);
float length = (float) Math.sqrt(sx * sx + sy * sy);
float nx = sx / length;
float ny = sy / length;
float theta = (float) Math.atan2(ny, nx);
final float rad2deg = (float) (180.0 / Math.PI);
float theta2 = theta * rad2deg;
return (theta2 < 0) ? theta2 + 360.0f : theta2;
}
Can someone explain me what is doing in getTheta(float x,float y);
Thanks in Advance...

How to draw a straight line in arc which connects the center with circumference

I have a custom view where I draw an arc.
Now I also need to draw a line which joins the center and the circumference
and should be exactly in the middle of arc.
Now I know that the code for drawing line in android canvas is fairly simple
but it does not involve angle and the arc is always using angle to draw itself.
So I can I draw the line in the same direction as the arc?
Am I clear here or do I need to explain more ?
Please help.
Calculate the start point and the end point.
private class MView extends View {
private Paint mPaint;
private RectF mRect;
private int mCenterX = 150;
private int mCenterY = 150;
public MView(Context context) {
super(context);
mPaint = new Paint();
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Style.STROKE);
mRect = new RectF(0, 0, mCenterX * 2, mCenterY * 2);
}
#Override
protected void onDraw(Canvas canvas) {
float startAngle = 30;
float sweepAngle = 60;
canvas.drawArc(mRect, startAngle, sweepAngle, true, mPaint);
float startX = mCenterX;
float startY = mCenterY;
float radius = mCenterX;
float angle = (float) ((startAngle + sweepAngle / 2) * Math.PI / 180);
float stopX = (float) (startX + radius * Math.cos(angle));
float stopY = (float) (startY + radius * Math.sin(angle));
canvas.drawLine(startX, startY, stopX, stopY, mPaint);
}
}
Here's what I end up with based on answer above:
for (int i = 0; i < numOfDividing; i++) {
angle = i * (360 / numOfDividing);
Point aPoint = new Point(
(int) (x0 + (radius) * Math.cos(Math.toRadians(angle))),
(int) (y0 + (radius) * Math.sin(Math.toRadians(angle)))
);
//Timber.d("Point %d : %d, %d", i, aPoint.x, aPoint.y);
points.add(aPoint);
}

How to manage overlays in MapViewCompassDemo in android sdk samples

I am using MapViewCompassDemo sample from
\add-ons\addon-google_apis-google_inc_-7\samples\MapsDemo\
for rotating map view based on user direction.This is working
perfectly if the map is without overlays.But if i add overlays to map the ontap not working exactly
on marker while rotating.can any one help me regarding this.
in this sample it only rotate dispatchDraw method only.Along with dispatchDraw rotate
dispatchTouchEvent also.find below code to rotate toouch events also
#Override
public void dispatchDraw(Canvas canvas) {
canvas.save();
canvas.rotate(getRotation(), getWidth() / 2, getHeight() / 2);
super.dispatchDraw(canvas);
canvas.restore();
}
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
float[] coords = new float[] {
event.getX(), event.getY()
};
adjustCoords(coords, getRotation());
MotionEvent evt = MotionEvent.obtain(event.getDownTime(), event.getEventTime(), event
.getAction(), coords[0], coords[1], event.getPressure(), event.getSize(), event
.getMetaState(), event.getXPrecision(), event.getYPrecision(), event.getDeviceId(),
event.getEdgeFlags());
return super.dispatchTouchEvent(evt);
}
protected void adjustCoords(float[] coords, float deg) {
float x = coords[0];
float y = coords[1];
int centerX = getWidth() / 2;
int centerY = getHeight() / 2;
// convert to radians
float rad = (float) ((deg * Math.PI) / 180F);
float s = (float) Math.sin(rad);
float c = (float) Math.cos(rad);
// translate point back to origin:
x -= centerX;
y -= centerY;
// apply rotation
float tmpX = x * c - y * s;
float tmpY = x * s + y * c;
x = tmpX;
y = tmpY;
// translate point back:
x += centerX;
y += centerY;
coords[0] = x;
coords[1] = y;
}
See this below link
http://evancharlton.com/thoughts/android-rotating-touch-events/

Categories

Resources