-
-
Save aitorvs/b09e9714d04dc365b616fd6355b0eaf9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.aitorvs.android.rsspace.util; | |
/* | |
* Copyright (C) 21/08/16 aitorvs | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
import android.graphics.Matrix; | |
import android.graphics.drawable.Drawable; | |
import android.widget.ImageView; | |
public class ImageViewUtil { | |
/** | |
* Returns the bitmap position inside an imageView. | |
* | |
* @param imageView source ImageView | |
* @return 0: left, 1: top, 2: width, 3: height | |
*/ | |
public static int[] getDisplayedImageLocation(ImageView imageView) { | |
int[] ret = new int[4]; | |
if (imageView == null || imageView.getDrawable() == null) | |
return ret; | |
// Get image dimensions | |
// Get image matrix values and place them in an array | |
float[] f = new float[9]; | |
imageView.getImageMatrix().getValues(f); | |
// Extract the scale values using the constants (if aspect ratio maintained, scaleX == scaleY) | |
final float scaleX = f[Matrix.MSCALE_X]; | |
final float scaleY = f[Matrix.MSCALE_Y]; | |
// Get the drawable (could also get the bitmap behind the drawable and getWidth/getHeight) | |
final Drawable d = imageView.getDrawable(); | |
final int origW = d.getIntrinsicWidth(); | |
final int origH = d.getIntrinsicHeight(); | |
// Calculate the actual dimensions | |
final int actW = Math.round(origW * scaleX); | |
final int actH = Math.round(origH * scaleY); | |
ret[2] = actW; | |
ret[3] = actH; | |
// Get image position | |
// We assume that the image is centered into ImageView | |
int imgViewW = imageView.getWidth(); | |
int imgViewH = imageView.getHeight(); | |
int[] imgViewScreenLoc = new int[2]; | |
imageView.getLocationOnScreen(imgViewScreenLoc); | |
// get the actual image location inside its image view | |
int left = imgViewScreenLoc[0] + (imgViewW - actW) / 2; | |
int top = imgViewScreenLoc[1] + (imgViewH - actH) / 2; | |
ret[0] = left; | |
ret[1] = top; | |
return ret; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment