Created
August 19, 2016 07:16
-
-
Save nguyenngan/47094ddd1437f0f8e72a8b91a19b7be4 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
import android.content.Context; | |
import android.graphics.Matrix; | |
import android.widget.ImageView; | |
/** | |
* ImageView to display top-crop scale of an image view. | |
* | |
* @author Chris Arriola | |
*/ | |
public class TopCropImageView extends ImageView { | |
public TopCropImageView(Context context) { | |
super(context); | |
setScaleType(ScaleType.MATRIX); | |
} | |
@Override | |
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { | |
super.onLayout(changed, left, top, right, bottom); | |
recomputeImgMatrix(); | |
} | |
@Override | |
protected boolean setFrame(int l, int t, int r, int b) { | |
recomputeImgMatrix(); | |
return super.setFrame(l, t, r, b); | |
} | |
private void recomputeImgMatrix() { | |
final Matrix matrix = getImageMatrix(); | |
float scale; | |
final int viewWidth = getWidth() - getPaddingLeft() - getPaddingRight(); | |
final int viewHeight = getHeight() - getPaddingTop() - getPaddingBottom(); | |
final int drawableWidth = getDrawable().getIntrinsicWidth(); | |
final int drawableHeight = getDrawable().getIntrinsicHeight(); | |
if (drawableWidth * viewHeight > drawableHeight * viewWidth) { | |
scale = (float) viewHeight / (float) drawableHeight; | |
} else { | |
scale = (float) viewWidth / (float) drawableWidth; | |
} | |
matrix.setScale(scale, scale); | |
setImageMatrix(matrix); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment