Coordinates of content of ImageView - java

So I have an ImageView where I set png as a background. Let's assume the png picture is a circle, so it doesn't take all the space of the ImageView.
I need to catch the event of clicking only the content of the ImageView (the circle) and not the remaining empty area.
Is this actually possible at all or with any other Android control?
Edit:
I still can't get this to work.
So to be more precise I have this picture of an egg which I set as ImageView src
And I want to be able to click only on it. Foe example if I click slightly outside the egg I want to know that and prevent code inside the ImageView click event.
I tried this code but I don't have any idea how this helps me:
ImageView imageView = (ImageView)findViewById(R.id.imageview);
Drawable drawable = imageView.getDrawable();
Rect imageBounds = drawable.getBounds();
//original height and width of the bitmap
int intrinsicHeight = drawable.getIntrinsicHeight();
int intrinsicWidth = drawable.getIntrinsicWidth();
//height and width of the visible (scaled) image
int scaledHeight = imageBounds.height();
int scaledWidth = imageBounds.width();
//Find the ratio of the original image to the scaled image
//Should normally be equal unless a disproportionate scaling
//(e.g. fitXY) is used.
float heightRatio = intrinsicHeight / scaledHeight;
float widthRatio = intrinsicWidth / scaledWidth;
//do whatever magic to get your touch point
//MotionEvent event;
//get the distance from the left and top of the image bounds
int scaledImageOffsetX = event.getX() - imageBounds.left;
int scaledImageOffsetY = event.getY() - imageBounds.top;
//scale these distances according to the ratio of your scaling
//For example, if the original image is 1.5x the size of the scaled
//image, and your offset is (10, 20), your original image offset
//values should be (15, 30).
int originalImageOffsetX = scaledImageOffsetX * widthRatio;
int originalImageOffsetY = scaledImageOffsetY * heightRatio;

here is how you would get the click event:
int[] viewCoords = new int[2];
imageView.getLocationOnScreen(viewCoords);
//From this and the touch coordinates you can calculate the point inside the ImageView:
int touchX = (int) event.getX();
int touchY = (int) event.getY();
int imageX = touchX - viewCoords[0]; // viewCoords[0] is the X coordinate
int imageY = touchY - viewCoords[1]; // viewCoords[1] is the y coordinate

Related

How to crop resize image in Java?

When I set the image with actual size onto a panel and cropping using mouse it's working fine but when I resize image onto panel and cropping it's getting wrong cropping image. How to crop resize image using mouse?
int x = Math.min(p1.x, p2.x);
int y = Math.min(p1.y, p2.y);
int w = Math.abs(p1.x - p2.x);
int h = Math.abs(p1.y - p2.y);
BufferedImage dest = image.getSubimage(x,y,w,h)
If you want to crop from the original image then you will need to calculate the x/y scale factors of the two images. Then you will need to adjust the x/y/width/height values by these scale factors.
So if your original image is 400 x 100 and the resized image is 100 x 100 you would do something like:
double xScale = originalImageWidth / resizeImageWidth = 400 / 100 = 4.
So now if the cropping rectangle on the resized image is (10, 10, 20, 30);
Then you need to calculate your values something like:
int x = rectangle.x * xScale;
int width = rectangle.width * xScale;
image.getSubImage(x, y, width, height);
You would obviously need to calculate the y / height values using the y scaling factor.

Scaling down and making image square in android

I need a way to scale an image down to 78x78. I have found ways of doing this by cutting part of the image off, like this:
Bitmap image = Bitmap.createBitmap(image, 0, 0, 78, 78);
but I need to maintain as much of the image as possible. I had thought of scaling the image down and then making it square:
Bitmap image = Bitmap.createScaledBitmap(imageTest, 78, 78, true);
but of course this creates a square image that is squashed.
Can anyone suggest how I can create a 78x78 image that doesn't rescale and maintains as much of the original image as possible?
From what I understood, you should scale down and center crop the image. Try this code out.
public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) {
int sourceWidth = source.getWidth();
int sourceHeight = source.getHeight();
// Compute the scaling factors to fit the new height and width, respectively.
// To cover the final image, the final scaling will be the bigger
// of these two.
float xScale = (float) newWidth / sourceWidth;
float yScale = (float) newHeight / sourceHeight;
float scale = Math.max(xScale, yScale);
// Now get the size of the source bitmap when scaled
float scaledWidth = scale * sourceWidth;
float scaledHeight = scale * sourceHeight;
// Let's find out the upper left coordinates if the scaled bitmap
// should be centered in the new size give by the parameters
float left = (newWidth - scaledWidth) / 2;
float top = (newHeight - scaledHeight) / 2;
// The target rectangle for the new, scaled version of the source bitmap will now
// be
RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
// Finally, we create a new bitmap of the specified size and draw our new,
// scaled bitmap onto it.
Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
Canvas canvas = new Canvas(dest);
canvas.drawBitmap(source, null, targetRect, null);
return dest;
}
Hope it helps
Try this:
Bitmap image = Bitmap.createScaledBitmap(testImage, (int) 78 * (testImage.getWidth() / testImage.getHeight()), 78, true);
image = Bitmap.createBitmap(image, (int) (image.getWidth() - 78) / 2, 78);
Haven't tested this, as I'm on my way to bed, but it should accomplish what you want, so long as your image has a width greater than or equal to its height.
Regardless, I'd suggest you use BufferedImage instead of Bitmap.
The idea here would be resize your image using the same resize rate for width and height keeping the smaller size in 78. After that you can use a center point based crop to get the middle of your image and making it a squared image.
Image srcImage;
int widthSrc = 150;
int heightSrc = 180;
float resizeRate = 78 / min(widthSrc, heightSrc);
Image resizedImage = resizeImage($srcImage, resizeRate);
int widthDest = 78;
int heightDest = 78;
int cropX = ($widthSrc - $widthDest)/2;
int cropY = ($heightSrc - $heightDest)/2;
Image croppedImage = cropImage(resizedImage,$widthDest, $heightDest, $cropX, $cropY);
If the image is already square you can skip the crop part.

create bitmap to screen size keeping ratio

I'm trying to make a Wallpaper Application. And I had big trouble during set wallpaper with bitmap. I have tried to figure out the answer for a week.
I want to set Bitmap in to wallpaper like
avoid crop
scaleType:fit_center(align center vertical, keep original bitmap's ratio)
How can I make it? Do I have to create new bitmap?
You need to resize the picture to make a new bitmap according to your screen size.
Here's the code:
Bitmap bitmapOrg = BitmapFactory.decodeFile(getApplicationContext()
.getFilesDir().toString() + "/images/" + imagename);
Log.e("imageheight", "" + bitmapOrg.getHeight());
Log.e("imagewidth", "" + bitmapOrg.getWidth());
double imageheight = bitmapOrg.getHeight();
double imagewidth = bitmapOrg.getWidth();
DisplayMetrics metrics = getApplicationContext().getResources()
.getDisplayMetrics();
double screenwidth = metrics.widthPixels;
double sreeenheight = metrics.heightPixels;
Log.e("screennwidth", "" + screenwidth);
double newratio = screenwidth / imagewidth;
Log.e("newratio", "" + newratio);
double newratio1 = newratio * imageheight;
double newratio2 = newratio * (imagewidth - 10); // 10 margin in width
Log.e("newratio1", "" + newratio1);
int mainheight = (int) newratio1;
// int mainweidth = (int) imagewidth;
int mainweidth = (int) newratio2;
Log.e("Mainheight", "" + mainheight);
Log.e("Mainweidtht", "" + mainweidth);
// Here you will get the scaled bitmap
Bitmap new_ScaledBitmap = Bitmap.createScaledBitmap(bitmapOrg, mainweidth,mainheight, true);
// Use this bitmap as wallpaper
To fit the Bitmap to the screen without cutting anything of, you first have to decide if the aspect ratio is bigger than the one the screen has or smaller. If the image aspect ratio is bigger than the screen aspect ratio, that means the bitmap is higer and/or not as wide as the screen, like the second image in the question. So you should scale the image based on the height like this:
if(imageWidth/imageHeight > screenWidth/screenHeight){
scaleFactor = screenHeight/imageHeight;
imageXPosition = screenWidth/2-imageWidth/2;
imageYPosition = 0;
Else the image should be scaled based on the width like this:
}else{
scaleFactor = screenWidth/imageHeight;
imageXPosition = 0;
imageYPosition = screenWidth/2-imageWidth/2;
}
You can use these values to draw the bitmap using a Matrix or create a scaled Bitmap with the dimensions imageWidth*scaleFactor and imageHeight*scaleFactor and draw it at imageXPosition | imageYPosition (this is more memory saving.

How to re-scale bitmap to different screen sizes but keep it square?

Hello great people of stackoverflow.com, I have a stumbled upon a great difficulty in my code; that being not being able to resize my images in-line with my screen size.
I have an image that is SAY 65 pixels by 65 pixels but I want this image to be a certain percentage of the screen say 6% while keeping square.
My brain cannot process the mathematics for this (because it is slow :-( )
(_width & _height is the screen width and height)
What I've got so far is:
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int avg_screen_dimension = (_width + _height) / 2;
float scaleWidth =
((float) width + (avg_screen_dimension - _width)) / width;
float scaleHeight =
((float) height + (avg_screen_dimension - _height)) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// RECREATE THE NEW BITMAP
Bitmap resizedBitmap =
Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false);
return resizedBitmap;
Any help would be greatly appreciated!
It's little more than simple algebra - no complex math needed.
You'll have to choose between fraction of the width or height of your screen, unless they're equal.
Let h = screen height, w = screen width, f = fraction of screen dimension, x = image size.
Then x = h*f. If you want to make it a fraction of the width, substitute w for h.
(Just as an aside - in your pasted code, you've mixed up the variable names : you declare them as height and width but pass them as _width and _height

How to Center background in Andengine Scene

Is there a formula to use to center a background in the camera of Andengine.
My camera is the size of the Screen on the particular device which i retrieve using this...
final Display display = getWindowManager().getDefaultDisplay();
CAMERA_WIDTH = display.getWidth();
CAMERA_HEIGHT = display.getHeight();
int x = //formula to center background in camera or screen on the X axis
int y = //formula to center background in camera or screen on the Y axis
For my SpriteBackground, how could i figure out what is the x, and y to center it in the camera?
The x and y is supplied like this..
Sprite sprite = new Sprite(x,y,TextureRegion);
final float x = CAMERA_WIDTH / 2 - textureRegion.getWidth() / 2;
final float y = CAMERA_HEIGHT / 2 - textureRegion.getHeight() / 2;
Sprite sprite = new Sprite(x, y, textureRegion);
This will make sprite positioned in the middle of the scene. If the camera doesn't move around, it will be the center of the camera (hence screen) too.
You should check setBackground method for your scene. It will center your sprite and move it when your camera moves.
Sprite background = new Sprite(0,0, CAMERA_WIDTH, CAMERA_HEIGHT, backgroundTextureRegion);
scene.setBackground(new SpriteBackground(background));

Categories