Created
September 19, 2019 23:04
-
-
Save nathan-fiscaletti/190a660620f6130e6a15962f59b21f22 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.my.app; | |
import android.content.Context; | |
import android.graphics.Matrix; | |
import android.graphics.PointF; | |
import android.graphics.drawable.Drawable; | |
import android.util.AttributeSet; | |
/** | |
* Class used to get detailed information about the size and position of an ImageView. | |
* | |
* @author Nathan Fiscaletti | |
*/ | |
public final class SizeAwareImageView extends androidx.appcompat.widget.AppCompatImageView | |
{ | |
/** | |
* Internal class for notating the size and position of an element. | |
*/ | |
@SuppressWarnings("WeakerAccess") | |
public class SizeAndPosition | |
{ | |
/** | |
* The Width of the element. | |
*/ | |
public float width; | |
/** | |
* The Height of the element. | |
*/ | |
public float height; | |
/** | |
* The top position of the element. | |
*/ | |
public float top; | |
/** | |
* The left position of the element. | |
*/ | |
public float left; | |
/** | |
* Create the SizeAndPosition. | |
* | |
* @param width The Width. | |
* @param height The Height. | |
* @param top The Top. | |
* @param left The Left. | |
*/ | |
SizeAndPosition(float width, float height, float top, float left) | |
{ | |
this.width = width; | |
this.height = height; | |
this.top = top; | |
this.left = left; | |
} | |
/** | |
* Check if this position is zero. | |
* | |
* @return True if it's zero, false otherwise. | |
*/ | |
boolean zero() | |
{ | |
return this.width == 0 && this.height == 0 && this.top == 0 && this.left == 0; | |
} | |
} | |
/** | |
* Create the SizeAwareImageView. | |
* | |
* @param context The Context. | |
*/ | |
public SizeAwareImageView(Context context) { | |
super(context); | |
} | |
/** | |
* Create the SizeAwareImageView. | |
* | |
* @param context The Context. | |
* @param attrs The attributes. | |
*/ | |
public SizeAwareImageView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
/** | |
* Create the SizeAwareImageView. | |
* | |
* @param context The Context. | |
* @param attrs The attributes. | |
* @param defStyle The Default Style. | |
*/ | |
public SizeAwareImageView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
/** | |
* Retrieve the Scaled Coordinates from within the ImageView. | |
* | |
* @param location The Location. | |
* @param multiplier The Multiplier. | |
* @return The Scaled Coordinates. | |
*/ | |
@SuppressWarnings("unused") | |
public PointF getScaledCoordinateWithPadding(PointF location, Float multiplier) | |
{ | |
SizeAndPosition sizeAndPadding = this.getBitmapScaledSizeAndPadding(); | |
if (! sizeAndPadding.zero()) { | |
float xCoordSize = sizeAndPadding.width / multiplier; | |
float yCoordSize = sizeAndPadding.height / multiplier; | |
return new PointF( | |
(xCoordSize * location.x) + sizeAndPadding.left, | |
(yCoordSize * location.y) + sizeAndPadding.top | |
); | |
} | |
return null; | |
} | |
/** | |
* Get the Scaled Size And Padding of the Bitmap within this ImageView. | |
* | |
* @return The Scaled Size and Padding. | |
*/ | |
public SizeAndPosition getBitmapScaledSizeAndPadding() | |
{ | |
int[] ret = new int[4]; | |
if (this.getDrawable() == null) | |
return new SizeAndPosition(0, 0, 0, 0); | |
// Get image dimensions | |
// Get image matrix values and place them in an array | |
float[] f = new float[9]; | |
this.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 = this.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 = this.getWidth(); | |
int imgViewH = this.getHeight(); | |
int top = (imgViewH - actH)/2; | |
int left = (imgViewW - actW)/2; | |
ret[0] = left; | |
ret[1] = top; | |
return new SizeAndPosition(ret[2], ret[3], ret[1], ret[0]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment