I'm drawing a custom shape using the path method, but it performs differently than is displayed.
As displayed in the Layout:
On the device:
This only happens when the angle at the left vertex becomes too small. If I widened the shape, such that it became a regular diamond, the entire shape appears (regardless of size).
Here's the code:
final Path p = new Path();
p.moveTo(x, y);
p.lineTo(x - height / 16, y - 20);
p.lineTo(x - width / 3, y);
p.lineTo(x - height / 16, y + 20);
p.close();
canvas.drawPath(p, mThumbColorPaint);
Can anyone enlighten me?
Related
I have a code to scale drawing canvas.
So i tried this code. i scale it but it moves down,
here is my code:
canvas.save();
scaleA = 1.1;
scaleB = 1.1;
canvas.scale(scaleA, scaleB);
canvas.restore();
the issue is even when i scale the Y coordinates move down, so i try to translate or reduce y coordinates but i still issue. any one knows how to properly scale a drawing canvas?
i tried like this:
canvas.save();
scaleA = 1.1;
scaleB = 1.1;
canvas.translate(scaleA, -scaleB);
canvas.scale(scaleA, scaleB);
canvas.restore();
You have to set the center of your image, if your image center is equal to the canvas(view) center
canvas.scale(sx, sy, canvas.getWidth() / 2, canvas.getHeight() / 2);
Or else, change the last two params to whatever center you want it to be, you don't need to translate
I have looked all over, but nothing has worked. Every time I try finding the center of the screen for my watch face, it always end up slightly higher than the center, and to the far right of the screen (This is different than the problem that the original template from Android Studio has, where the Watch face is slightly up and all the way to the far left of the screen). What could I be doing wrong?
(First screenshot on Left: My problem. Second screenshot: Original form of the Android Wear template from Android Studio)
#Override
public void onDraw(Canvas canvas, Rect bounds) {
if (isInAmbientMode()) {canvas.drawColor(Color.BLACK);
}
else {
canvas.drawRect(0, 0, bounds.width(), bounds.height(), mBackgroundPaint);
}
// Draw H:MM in ambient mode or H:MM:SS in interactive mode.
long now = System.currentTimeMillis();
mCalendar.setTimeInMillis(now);
int width = bounds.width();
int height = bounds.height();
float centerX = width / 2f;
float centerY = height / 2f;
String text = mAmbient
? String.format("%d:%02d", mCalendar.get(Calendar.HOUR),
mCalendar.get(Calendar.MINUTE))
: String.format("%d:%02d", mCalendar.get(Calendar.HOUR),
mCalendar.get(Calendar.MINUTE));
canvas.drawText(text, centerX, centerY, mTextPaint);
}
Full code can be found here
As far as i can tell from a quick look on your sources you already calculate mTextXOffset and mTextYOffset but don't use it for your draw call, hence the texts origin is centered in your problematic screenshot instead of the texts center.
adjusting your centerX and centerY by mTextXOffset and mTextYOffset should do the trick.
If you're drawing text using a center point you can use
mTextPaint.setTextAlign(Paint.Align.CENTER);
which will draw from the center. Otherwise the x coordinte represents the left hand side of the text you are drawing.
I have taken the current frame under mRGBA in this fashion:
mRgba = inputFrame.rgba();
then created a rectangle object which takes the height and width of the current camera frame
rect = new Rect();
rect.width = mRgba.width();
rect.height = mRgba.height();
it takes the whole space of the frame but when i try to shrink this rectangle it got shrink in a one side(not as a whole which i need)
So i tried to find the rectangles center and then tried to create another rectangle according to that center and a predefined size
int x = (int) (rect.tl().x + rect.br().x)/2;
int y = (int) (rect.tl().y + rect.br().y)/2;
Rect rect1 = new Rect(x,y,280,280);
Imgproc.rectangle(mRgba, rect1.tl(), rect1.br(), new Scalar(255, 0, 0), 2, 8, 0);
But yet its not in the center!! i am not sure about the parameters the rectangle object takes i didn't find the document of opencv that much helpful. So how to overcome this situation i want the rectangle to be exactly at the center of the camera frame.
Base on my observation, the line below is actually creating a rectangle at a corner (x,y) with width 280 and height 280.
Rect rect1 = new Rect(x,y,280,280);
width(280)
<------(x,y)
|
| height(280)
|
V
so, your calculation for the point of center should be correct.
I hope the codes below may help you.
int width = 280;
int height = 280;
Rect rect1 = new Rect(x - width / 2, y - height / 2, width, height);
EDIT:
Thanks for Micka's reminder. My concept above is incorrect, although it works.
The Android camera is landscape in default. Usually, we rotate the image 90.
(Ref: Android - Camera preview is sideways)
Your calculation is based on a landscape image and openCV's coordinate system which is different from what we usually use. (Ref: Reference coordinate system changes between OpenCV, OpenGL and Android Sensor)
It's difficult to explain in words, so I draw a picture for you :)
So I have an actor object I extended that will either draw a square or a circle, every frame it increases in size by 0.1.
When I draw circle it increases in size correctly and grows on screen, however when I draw a rect it jumps in size every 2.0 increments. I was wondering if anyone else had seen this behaviour.
Here is the simple code I use to draw the shapes.
float size = 10.0f;
#Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
ShapeRenderer r = new ShapeRenderer();
r.begin(ShapeRenderer.ShapeType.Filled);
r.setColor(Color.RED);
//r.circle(this.getX() - (size / 2), this.getY() - (size / 2), size); //this increases correctly
r.rect(this.getX() - (size / 2), this.getY() - (size / 2), size, size); //this doesnt
r.end();
size += 0.1f;
}
So you can see what I mean I took some vids:
Circle : https://youtu.be/TtLcMxUCKb0
Square : https://youtu.be/UUGNfSpotKo
Seems to me like the square is expanding the least amount it can, 1 pixel at a time, it just looks a bit different from the circle since the circle does more than just expand 1 pixel to each side It has to the circle always look as round as possible, so updates a bit more often, shifting around a few pixel but not expanding.
I am trying to create a circle shape that will be resized on onTouchEvent.
I have followed resizable rectangle post, but I think due to lack of mathematics knowledge, I am not getting how to create resizeable circle.
I have tried changing
canvas.drawRect(
left + colorballs.get(0).getWidthOfBall() / 2,
top + colorballs.get(0).getWidthOfBall() / 2,
right + colorballs.get(2).getWidthOfBall() / 2,
bottom + colorballs.get(2).getWidthOfBall() / 2, paint);
to canvas.drawCircle(); it creates the circle but not quite what I wanted.
Can you please tell me is there any thing like this already implemented or what points should I follow to convert this rectangle example to resizable circle.
So, the center of circle will be:
float cx = (left + right) / 2f;
float cy = (top + bottom) / 2f;
-- quite obvious. Radius can be calculated using Math.hypot():
float radius = Math.hypot(top - bottom, right - left) / 2f;
Thus, we have center and radius to draw a circle:
drawCircle(cx, cy, radius, paint);