Is image cropping possible in an Android app? - android

Is it possible to crop an image on Android?
For example:
If the image must fit in a 400w x 400h space and it is 600w x 400h, can you horizontally center align it within the 400w x 400h placeholder space and crop out 100 pixels on the left and 100 pixels on the right.
OR
If the image must fit in a 400w x 400h space and it is 400w x 600h, can you vertically center align it to the image placeholder and crop out 100 pixels on the top and 100 pixels on the bottom.
This may also apply to images of many different resolutions coming in from a server - regardless of the resolution of the image, can you accomplish the above?
I found this but this is specifically for users to be able to crop their images manually in the app (https://medium.com/mindorks/android-top-image-cropper-libraries-3bc4a4f8f2df), but I need to have the image pull in to a set placeholder size in the app from the API.
Thanks!

Related

Android Studio: Scale ImageView Boundary to fit within Image

I want to scale the borders to fit the image. In my file explorer, the image is small and has fit, But hasn't fit in android Studio
If I scale the width and height to a specific value, the whole image shrinks:
Like this
Edit: I got it by changing the scaleX and scaleY
the empty space around image exist in your image , so edit your image and make sure that you crop the image so it just contain what you want.

Placing a view to exact same place for all android devices

My task is to allow user to upload an image and let user to put a custom view anywhere he clicks. (Like taging a pic on facebook) That is simple by getting the touch location and dynamically adding view to a relative layout and setting leftMargin and topMargin.
But I want this custom component to get drawn in the 'exact' same position in every android device.
For example; Think that you uploaded a picture into your profile and tag someone, other people who look at your pic with different devices should see the tag in the exact same place relative to the pic.
I know that I have to take account of screen density as well but don't know how to manipulate x, y according to that.
Look at the example screenshot:
(Blue box is put by user click)
This screenshot is from a test device with screen density 1, another device with density 1.5 shows the blue box in a different position.
(Images Updated:)
This is a test code:
icon = new ImageView(getActivity());
icon.setBackgroundColor(getResources().getColor(R.color.blue));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30, 40);
int density = this.getResources().getDisplayMetrics().density;
params.leftMargin = (int)(posX/density);
params.topMargin = (int)(posY/density);
Think that you uploaded a picture into your profile and tag someone, other people who look at your pic with different devices should see the tag in the exactly same place.
Then you do not want to be placing "a view to absolute X, Y for all android devices".
For example, suppose we have two devices, A and B. On Device A, your image is a small phone, and the image happens to be rendered at a size of 480 pixels high by 270 pixels wide. The user taps at a position that happens to be 80 pixels down from the top and 70 pixels in from the left.
Device B has a screen approximately the size of the country of Turkey. More specifically, your image is rendered at 1600km by 900km on an -mdpi screen density, in a portrait orientation, meaning the image is around 10,078,736,000 pixels high by 5,669,289,000 pixels wide.
You do not want to be putting your marker 80 pixels down from the top and 70 pixels in from the left. That will put the marker almost in the upper-left corner.
Assuming that you arrange to show your image at the same aspect ratio, you will want to save the percentages that the marker is from some corner. In this example, 80 pixels from the top is 80/480 or ~16.7% from the top, and 70 pixels in from the left is 70/270 or ~25.9% from the left. You would then apply the percentages to the size of the image on another device, and so show your marker ~16.7% from the top and ~25.9% from the left on the Turkey-sized screen. Or on a tablet. Or on a larger phone. Or on a TV. Or on whatever you are choosing to use.

Putting a button or text on an exact place of a scrollable background image

Say I have a background image as such.
http://i.imgur.com/rRCtzyl.png
I want to put a button or text right inside the white box. My text or button must scale and fit the box perfectly when the image is scaled according to device screen size. The image does not fit the screen vertically so it is scrollable. I know that I need to put them in a group if I want them both to scroll together.
But how do I place the button inside the box? Since its scrollable I dont know the exact x and y values of the box. Not to mention these values will change according to device screen size. And how do I make the buttons scale to fit the box with all screen sizes?
This image is only an example. Of course I wouldn't have a problem if I drew the box myself programmatically. I would know where to put the button. Assume I dont know the coordinates and the size of the box since I didn't d
If your box object is called box, set the button to the box.x and box.y like this:
object1.x = box.x
object1.y = box.y
The scroll aspect should not matter if you put the button and image in same group and scroll the group. Then all you need to do is position the button relative to the image once, the rest will be taken care of by corona.
What will matter is the screen resolution. One way of tackling this is to view the image at full resolution and compute at what x and y pixels the box is located. For instance,
image: 200 x 300 pixels
box top left: 100, 100 (pixels from top left of image)
box width height: 20, 60
Then you use ratios: top left of button box is at 0.5, 0.3333 of image size; the button box width is 0.1 and height 0.2 of image size. Then when you start app, you compute what will be the image scaling factor based on actual resolution and you apply that to box coordinates:
device 300 x 400:
image will be scaled 1.5x in width, 1.333x in height
so button box should be at (200*1.5*0.5, 300*1.333*0.333)
with width 20*1.5 and height 60*1.333

Locate the same area of an ImageView regardless of scaling

I want to identify a certain part of an ImageView -- based on the actual size of the image regardless of how the image is scaled for the particular device.
Example: I have an image that is 480x320. I want a rectangle that (at this size) is defined as
Rect(10,35,20,50). What I'm wanting to do is figure out how I can define this rectangle relative to the actual size of the image when it scaled and displayed on the screen.
Can you just scale each part of the rectangle? For instance, if your image is displayed at 2x -- 960x480 -- then would the correct rectangle be (20,70,40,100)?
Use percentages from a pivot point: the top-left or the bottom-right.

Android ImageView scaling

I am using google graph to get the desired graph for me and display it on a android device.
Now I want to display the received graph on a tablet screen but google graph api supports only 300,000 pixels as a max size (width x height) graph.
I am making a request for 500 x 600 px image and display it on a ImageView. I set the scaleType to fitXy and give the imageView width=fill_parrent height=850dpi because I want the image to almost fill the screen.
The problem is that the background of the image goes light gray or blurring after the ImageView scales the image and it looks bad on the white background of my application... If I make the ImageView wrap_content on width and height the graph has white background.
Is there any way to get the image displayed without changing it's background color?
Please note that I don't want to change the image's size.
I'd recommend to instead use width match_parent and then setAdjustViewBounds=true
If that doesn't fix it you probarbly have to change the image.
Unfortunately I had to change the image to a bigger one and the problem disappeared.

Categories

Resources