Android: Actual position of an ImageView - android

Is there a way to tell precisely where an image is placed on the parent?
ImageView layout returns some frame around the actual image.
One can see the difference when working with images that are very different from the screen proportions. For example a square image on a long screen.
Worse: devices like Edge have their bottom bar that consumes parts of the screen and affect the layouting calculations. In that case it's hard to do reverse calculations.

I can't test this now, but try something like the following, I believe this should do what you need:
ImageView iv = (ImageView)findViewById(R.id.image_view);
Rect rect = iv.getDrawable().getRect();
int xOffset = rect.left;
int yOffset = rect.top;
That should give you the exact pixel location of the top left corner. I'm pretty sure that the top-left is considered the origin...test this and see if it gives you the expected results, though.

Related

AppCompatImageView displaying big image using ScaleType.CENTER and the use of padding

I have an image 3264x1836 and I display it in a custom AppCompatImageView having dimensions 1280x720. I want to preserve the image size so I use ScaleType.CENTER, but I also want that its top-left corner is positioned at the coordinates 0,0 of my custom view, so I set the padding left and top accordingly.
Now, to achieve that I had to use 3264-1280 (the difference between the widths) as left padding and 1836-720 (the difference between the heights) as top padding while, in my opinion, these values should be both divided by 2.
Can somebody explain why?
It is likely that you are breaking the AppCompatImageView code by asking it to do something that wasn't anticipated or tested.
While the above could still be true, the padding that you are applying is correct. Here are the calculations:
For simplicity, let's take a look at the left padding needed to shift the graphic. The same sort of calculations will work for the top padding.
The width of the graphic is greater than the width of the ImageView.
d = wgraphic - wimageview
If the graphic is centered in the ImageView, then the overhang on the left and right sides will be 1/2 the difference in widths.
s = d / 2
The graphic is centered within the padding of the ImageView. The amount of padding that has to be specified must be just enough to shift the center of the graphic by the amount s.
The initial center of the ImageView without padding is
ci = wimageview / 2
The shifted center with left padding ( p ) is
cs = p + (wimageview - p) / 2
So,
s = cs - ci = p + (wimageview - p) / 2 - (wimageview / 2)
Solving for the padding needed for a shift of s we get p = 2s = d. In other words, the padding we need is twice the shift required which is what you are seeing.
A fix that doesn't involve padding would be to specify
android:scaleType="matrix"
You should remove the padding. The new scale type will apply the identity matrix to the image and place it in the top/left corner without resizing.

ImageView setScale applied, wrong alignment in RelativeLayout

I have a large image to be placed on parent's left Center. This is straight forward, and looks like that:
Large picture with layout_alignParentRight & layout_centerVertical true
It is too big, so I applied this, to make it smaller:
rightButton = (ImageView) findViewById(R.id.buttonnext);
rightButton.setScaleX(0.5f);
rightButton.setScaleY(0.5f);
But now it looks like that:
Scaled image, but not properly aligned to parents right
Questions:
Is setSaceX & Y a proper way to resize image?
I understnad that original image is still there, filling rest of space. Any way to move scaled image to the right? Tried setPivotX & setPivotY but without luck

how to scale image between 2 views with given coordinates in android

In my application I have a requirement to animate an image (this image view has an arrow set as source).
I am unable to figure this out how I can achieve this.To solve this i got x and y coordinates of second view that is rectangle after getting coordintes i am setting
scaleX() of image view that is purple line but I am not geeting desire out put because it stretches to the whole screen along x-axis
here is the code what i tried is
int x = (int) imageView.getX();
imageView2.setScaleX(x);
here imageview is rectangular box and imageview 2 is the purple line
Why you dont try with scenes? It's exactly what you need.
http://developer.android.com/training/transitions/scenes.html
I think you are getting the correct output when using the above code. Scale is defined against initial dimensions. And you would need to use something like this
dist = box.getX() - circle.getX()
and your scale would be
imageView2.setScale(dist/distInitial)
where distInitial you compute it at creation time using the dist formula.
You might need to change the position because it is scaled around the center of it (i.e. a smaller scale shrink margins to center of image)
You might want to perform this operations using a Canvas. It might be more efficient.

Android finding the center of screen in terms of px

Currently, My goal is to place some images in an arrangement relative to the center of the screen. There is a varying number of images and locations, so I'd like to do this programmatically.
I am having trouble finding the center of the correct center of screen in terms of px. To test, I have tried placing an image at the center.
ImageView image = (ImageView)findViewById(R.id.imageView1);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
image.setPadding(width/2,height/2,0,0);
I understand that there is an offset since setting the padding should place the image's top left corner in the center. Even so, the image comes out as too low and too far right. What kind of issue could be causing this?
Padding set just the inner space between element's border and content. If element have also margins, they summarized. You can use only margins (paddings equals to 0) or only paddings (margins equals to 0). Also if you not interested in supporting android 2.3 and less, you can use setX() and setY() methods of View class.
I hope this help you.

Using setLayerInset on LayerDrawable with Bitmap gives unexpected results

I'm dynamically placing small dot bitmaps ('hotspots') over a large bitmap image using LayerDrawable. The layer at index 0 contains the large image. The dots' positions on the image are specified to me in terms of percentage of large image. For example, I might have a hotspot that is supposed to be 50% from the left and 75% from the top of the large base image. In order to position the dots over the correct part of the image, I'm using
setLayerInset(layer, leftOffset, topOffset, rightOffset, bottomOffset)
and calculating the offsets based on the width and height of the ImageView which contains the LayerDrawable. I'm happy that the calculations are correct and that the right height and width are being retrieved, and expect the dots to be correctly displayed, but they are not. The dots are being skewed along the Y axis and their positions are incorrect (too high and too far to the left). Has anyone encountered a similar problem and found a solution?
I spend a day and a half working on this, trying to make sense of the numbers, and here's what I found:
In the situation I am dealing with, where the first layer contains a BitmapDrawable and I'm setting insets for the subseqent layers, the offset in the call:
setLayerInset(layer, leftOffset, topOffset, rightOffset, bottomOffset)
are relative to the dimensions of the bitmap in the first layer, even if those dimensions are greater than the view in which the DrawableLayer is being placed (e.g. when the Bitmap is being redimensioned automatically by the view).
Bizarre and unexpected, but true. Hope this saves somebody some time.

Categories

Resources