I am reading the book called beginning android games.
In the book it has the following method in the SpriteBatcher class...
public void drawSprite(float x, float y, float width, float height, float angle, TextureRegion region) {
float halfWidth = width / 2;
float halfHeight = height / 2;
float rad = angle * Vector2.TO_RADIANS;
float cos = FloatMath.cos(rad);
float sin = FloatMath.sin(rad);
float x1 = -halfWidth * cos - (-halfHeight) * sin;
float y1 = -halfWidth * sin + (-halfHeight) * cos;
float x2 = halfWidth * cos - (-halfHeight) * sin;
float y2 = halfWidth * sin + (-halfHeight) * cos;
float x3 = halfWidth * cos - halfHeight * sin;
float y3 = halfWidth * sin + halfHeight * cos;
float x4 = -halfWidth * cos - halfHeight * sin;
float y4 = -halfWidth * sin + halfHeight * cos;
x1 += x;
y1 += y;
x2 += x;
y2 += y;
x3 += x;
y3 += y;
x4 += x;
y4 += y;
verticesBuffer[bufferIndex++] = x1;
verticesBuffer[bufferIndex++] = y1;
verticesBuffer[bufferIndex++] = region.u1;
verticesBuffer[bufferIndex++] = region.v2;
verticesBuffer[bufferIndex++] = x2;
verticesBuffer[bufferIndex++] = y2;
verticesBuffer[bufferIndex++] = region.u2;
verticesBuffer[bufferIndex++] = region.v2;
verticesBuffer[bufferIndex++] = x3;
verticesBuffer[bufferIndex++] = y3;
verticesBuffer[bufferIndex++] = region.u2;
verticesBuffer[bufferIndex++] = region.v1;
verticesBuffer[bufferIndex++] = x4;
verticesBuffer[bufferIndex++] = y4;
verticesBuffer[bufferIndex++] = region.u1;
verticesBuffer[bufferIndex++] = region.v1;
numSprites++;
}
My question is how do you rotate an image like the one below smoothly so that the lines in the image don't look pixelated?
I do have the image at the right size and DPI and looks great before its rotated, but when it is rotated the edges of the lines don't look so great.
You could try using LINEAR sampling instead of NEAREST, though it's hard for me to guess more without seeing a picture of the problem, or seeing your texture creation code.
Related
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.
#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.
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?
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...
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/