How to make something appear on a specific location in an image? - android

I want to make something(for example an arrow) appear on a picture. The location of this arrow depends on the value in a variable. How do I make this in Android? If possible, I need it to be screensize-independent and also support zooming in. Thanks!

To draw an image at a specific place you should change its margins values:
MarginLayoutParams params= (MarginLayoutParams)getLayoutParams();
params.setMargins((int) Math.round(location.x - (_width / 2.0)),
(int)Math.round(location.y - (_height / 2.0)), 0, 0);
requestLayout();
width and height are you image dimensions.

Related

How to make the area of camera preview be half of SurfaceView in ContinuousCaptureActivity

I want to make the area of camera preview be half of SurfaceView, so I modify the code of ContinuousCaptureActivity.
The detail is as follows:
Use GLES20.glViewport(0, 0, viewWidth / 2, viewHeight / 2); to replace https://github.com/google/grafika/blob/master/src/com/android/grafika/ContinuousCaptureActivity.java#L436
But the result is strange, see the picture below.
I really can not understand it and what is the right way to do it?
Who can give me some advices?
My first thought would be that you need a glClear() in there (with the viewport set to cover the entire surface) since you're no longer filling the entire surface with the blit. Otherwise you get uninitialized data, and on a tile-based architecture things can get strange.
Fiddling with the viewport isn't really the right way to go. Just draw a smaller rectangle. Use Sprite2d and change the X/Y scale factors. See TextureFromCameraActivity for an example -- you can scale the rect, zoom in, rotate, etc.

How to make the simplest <div>-like transparent container/layer in AndEngine?

I would simply like to have an Entity, Rectangle or something that would serve as a plain container for other Sprites.
Positioning purposes is what I need it for... I need to be able to set this container's width and position.
I have tried with Rectangle, but it seems to hide absolutely everything behind it (with a lower Z-index).
you can change the alpha of the Rectangle to "see" what's underneath it. Something like
myRect = new Rectangle(x, (CAMERA_HEIGHT - 480 + 100) / 2, size, size, vboManager);
myRect.setAlpha(0);
scene.attachChild(myRect);

Scaling from ImageView to image.

ok so this is a hard one (in my head).
mframe, sframe1, sphoto1, sframe2, sphoto2 has its own scales and dimensions:
a width and a height is the dimensions that these objects have.
The plan:
Sframe2 gets dragged on to sframe1. When I let go of the mouse sphoto2's dimensions (which are scaled within the boundaries of sframe2) need to be dropped in the scaled location of sphoto1 (which resides scaled within sframe1).
to be able to drop sframe2 within sframe1 on the location I let go I need to be able to correlate the location it was dropped on to the scaled image as I want to merge sphoto2 with sphoto1.
sframe1 and sframe2 have coordinates on mframe. sphoto1 and sphoto2 only have private coordinates (such as I can merge the images to and x and y position on them.).
The problem is that because the photos inside are scaled differently to these frames I have figure out the scaling factors to be able to correctly merge sphoto2 with sphoto1 with photo2 at the correct size and position on sphoto1.
so the question is... How can I do that?
Below is a diagram to assist in visually representing the problem.
Here is also a video to show you what it should not do. The image inside the frame needs to scale and merge on the other image correctly.
http://www.youtube.com/watch?v=N17Rrs1dSz0&feature=youtu.be
My mind is fried. Can you figure out what needs to scale what?
You can set layout params for that image and multiply with screen ratio, example:
imageview.setLayoutParams(new LinearLayout.LayoutParams(
(int) (250 * config.ratio), (int) (280 * config.ratio)));
But if you use this solution, you must calculate sceen ratio before scaling your image

Rotate a Scaled Image From the Center

I have some sprites (Well, custom classes that implement Sprite, but whatever) that I resize. AndEngine resizes the image from the center, which makes an image placed at 0,0 no longer appear at 0,0. To fix this I applied
sprite.setScaleCenterX(0);
sprite.setScaleCenterY(0);
This places the image where I want it. However, now when I rotate the image, the image moves around (If the image were a plain square, rotating it should make no visible change). To fix this I applied
sprite.setRotationCenterX((sprite.getWidth() * sprite.getScaleX()) / 2);
sprite.setRotationCenterY((sprite.getHeight() * sprite.getScaleY()) / 2);
(For some reason, resizing a Sprite doesn't change the dimensions of the sprite, just the visual image, hence multiplying it by the scale). This, however, did not correct the problem, but merely changed where the image moved to when flipped.
Is my math off here? Wouldn't this center the rotation on the image so that the image doesn't move position? Or is there something else I'm missing?
Below is full code:
Sprite sprite = new Sprite(0, 0, singleTrackTR, getVertexBufferObjectManager());
sprite.setScale(scaleX, scaleY);
sprite.setScaleCenterX(0);
sprite.setScaleCenterY(0);
sprite.setRotationCenterX((sprite.getWidth() * sprite.getScaleX()) / 2);
sprite.setRotationCenterY((sprite.getHeight() * sprite.getScaleY()) / 2);
All your code is correct. I tried it myself, both the setProperty(x, y) and the setPropertyX/Y(a) versions.
By any chance, do you have it connected to a Body? Note that the Body also doesn't scale with a Sprite's setScale. It has its own setTransform method, which takes x and y (that you both have to divide by PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT) and a rotation value.

Finding the edge of the screen android

This may sound really obvious but how can I find the points along the edge of the screen in order to know if a moving object has hit the edge of the screen.
Thankyou
Given you have orthographic projection, you can easily know where your edges are by knowing the values you've passed into the glOrtho() function.
For example, consider this line:
glOrtho(0.0f, width, 0.0f, height, 0.0f, 1.0f);
Here you can decide how big influence a single float will have in your program. You can find more information about this here.
However, in this scenario your left edge is 0.0 and your bottom edge is 0.0. Your top edge is defined by height and your right edge is defined by width.
In this way you don't have to rely on a specific screen size since your world will always be as big as you define. For example, you don't need to define the orthographical width and height by the width and height parameters, but instead use some data telling your application how big your world should be.
Just do this:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();

Categories

Resources