place images inside a layout android - java

I want to place some images (triggered on click action) inside a layout. I have to place them such that they don't get out of the parent layout.
Code I'm using to add a new image on clicking the layout:
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
ImageView image = new ImageView(this);
LinearLayout.LayoutParams coordinates = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
image.setLayoutParams(coordinates);
image.setImageResource(R.drawable.image);
layout.addView(image);
If I press on the layout , I must see my imageView put randomly.
Random random = new Random();
int x = random.nextInt(layout.getWidth());
int y = random.nextInt(layout.getHeight());
image.setX(x);
image.setY(y);
But this won't do it. And I see those images outside my layout too.

You are setting x & y which are upper left-top corner - starting point of image to display it. As x/y value can be right/bottom corner, therefore, your image goes out of layout in that case.
Please note - x, y are starting point from where your image will be drawn.
You need to make sure that layoutWidth - x >= imageWidth and layoutHeight - y >= imageHeight.

Related

How do I make an animation of an ImageView from a RecyclerView on screen moving to a floating action button programmatically?

I have a RecyclerView on the screen and products with images in it. I'm trying to make an animation where the image of the product from the RecyclerView moves to the floating action button on the screen (both have the same parent layout - a coordinator layout.)
I have tried achieving this with TranslateAnimation and when that didn't work well, I also tried doing it with ObjectAnimator, but with both the animation always either starts from the wrong position or ends in the wrong position on the screen.
This was my attempt to achieve this with TranslateAnimation:
// Get the X and Y position of the ImageView and the FAB
int[] imageViewPos = new int[2];
productImage.getLocationInWindow(imageViewPos);
int[] fabPos = new int[2];
fab.getLocationInWindow(fabPos);
// Calculate the x and the y distance between the FAB and the imageView
xDelta = fabPos[0] - imageViewPos[0];
yDelta = fabPos[1] - smallImageViewPos[1];
productMoveAnimation = new TranslateAnimation(0, xDelta, 0, yDelta);
productMoveAnimation.setDuration(500);
productMoveAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
productImage.startAnimation(productMoveAnimation);
I also tried to use getLocationOnScreen instead of getLocationInWindow, but the productImage still didn't move to the correct position of the FAB.
This was my attempt to achieve this using ObjectAnimator:
pvhX = PropertyValuesHolder.ofFloat("x", imageViewPos[0], fabPos[0]);
pvhY = PropertyValuesHolder.ofFloat("y", imageViewPos[1], fabPos[1]);
productMoveAnim = ObjectAnimator.ofPropertyValuesHolder(productImage, pvhX, pvhY);
productMoveAnim.start();
And with this code instead, the ImageView kept going off the screen in the animation - even though it did start from the correct position. I tried getLocationOnScreen here too, and getLeft and getTop but it didn't fix anything.
I would really appreciate if anyone could help me figure out what I'm missing here, thanks in advance for any help! :)

View.getWidth() shows the correct width but cannot produce the right margin when added to View's x position

So I overlayed a view of another view. And try to position my arrow so that it points to the image underneath it:
The arrow is an ImageView and I set its top and left margins to position it in the right place. I did it by adding the width of the TextView underneath it that says "No photo taken today" to the TextView's left border position. But funnily, the arrow is pointing to the middle and not the end of that TextView.
int topMargin=(int)pxToDp(getRelativeTop(dailyPhotos)+(int)dailyPhotos.getHeight(),this);
int leftMargin=(int)pxToDp(getRelativeLeft(dailyPhotos)+dailyPhotos.getWidth(),this);
getRelativeLeft() I copied from this answer.
I show the width of that TextView and the screen's width in a Toast. They are both correct since that TextView's width is set to match_parent. I don't what is wrong.
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
Toast.makeText(this,"width="+dailyPhotos.getWidth()+", screen's width="+width,Toast.LENGTH_SHORT).show();
showNote.putExtra("photoCountTopMargin", topMargin);
showNote.putExtra("photoCountLeftMargin", leftMargin);
Reading/Changing the margin of a view needs to be done in the layoutparams of the view
LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();
layoutParams.topMargin = ddd (in pixels)

Arc Shape Background Android

I wanna create a view with Arc Shape background.. my view has weight of .3.. which makes it fill one third of my layout..
I try to set its background to an arc shape (I want it to be half oval) like this:
ArcShape shape = new ArcShape(270, 180);
ShapeDrawable shapeDrawable = new ShapeDrawable(shape);
shapeDrawable.getPaint().setColor(getResources().getColor(R.color.primary_color));
leftPathView.setBackgroundDrawable(shapeDrawable);
this generates the shape I need but it doesn't fill the space of my view.. It only takes half of it.. But when I create a completed circle it takes all the space..
Any Idea ?
After a bit of research I found this to be the best answer. Especially if you want to do this in your onCreate function, bacause when the onCreate function hasn't ended the layout does not yet have a width and height (more info here).
final LinearLayout leftPathView= (LinearLayout) findViewById(R.id.left_path_view);
layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
leftPathView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
leftPathView.setX(-layout.getWidth());
leftPathView.getLayoutParams().width = layout.getWidth() * 2;
leftPathView.requestLayout();
}
});
ArcShape shape = new ArcShape(270, 180);
ShapeDrawable shapeDrawable = new ShapeDrawable(shape);
shapeDrawable.getPaint().setColor(getResources().getColor(R.color.primary_color));
layout.setBackground(shapeDrawable);
This code does make your View go out of the bounds of your screen, so when adding other items to this View you need to take that into account. A solution for that could be to put the View in a RelativeLayout and put another view on top of the leftPathView.

How to add an imageview at a fix point x-y of another imageview (not the container) in Android

I have a relative layout in which an imageview is placed. In onTouch event of this imageview I have made a code to add another imageview (a marker image) dynamically on that image at the x y point of the container.
I want to zoom my main imageview and also want to reside the dynamically added imageview at the fix location of the image (not the container).
That is, when I zoom my main image then the marker image should also move where the main image zoomed and will be fixed at point x,y of the image.
Since your container is a relative layout, you can make your inner imageView to align to the outer imageView, and put top margin and left margin. x and y are the coords of the point you want to put the image in, some_x and some_y is the size of innerView
ImageView outerView = (ImageView) findViewById(R.id.outer_image_view);
ImageView innerView = (ImageView) findViewById(R.id.inner_image_view);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(some_x, some_y)
rlp.addRule(RelativeLayout.ALIGN_LEFT, R.id.outer_image_view);
rlp.addRule(RelativeLayout.ALIGN_TOP, R.id.outer_image_view);
rlp.leftMargin = x;
rlp.topMargin = y;
innerView.setLayoutParams(rlp);

Relative coordinates of an ImageView in Android

I have a square image in an ImageView contained within a FrameLayout that is right aligned (landscape layout). The FrameLayout is set to FillParent width and height and the ImageView is set to Adjust View Bounds so that the resulting ImageView is a perfect square with each side being the length of the height of the landscape layout, right aligned. However, the original image is larger than this.
I need to get the coordinates clicked on the ImageView in relation to itself (ie. the top left hand corner of the ImageView is 0,0 and the bottom right corner would be the displayed width and height coordinates) so that I can work out if the clicked point was past the mid-point of the ImageView.
I initially get the size of the ImageView using:
int myImageViewWidth = imageView.getWidth();
int myImageViewHeight = imageView.getHeight();
I've created an OnTouchListener and within this I'm using event.getX() and event.getY(), but this seems to return the coordinates relative to the image's oiginal size, not the displayed size. I've also tried the following:
Matrix m = imageView.getImageMatrix();
float[] values = new float[9];
m.getValues(values);
float relativeX = (event.getX() - values[2]) / values[0];
float relativeY = (event.getY() - values[5]) / values[4];
But again this seems to return inaccurate coordinates. Can anyone please help?

Categories