Created
June 7, 2017 12:31
-
-
Save MensObscura/c7537dde73c5e64fd35b8a42be67ea1e 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.util.AttributeSet; | |
import android.widget.ImageView; | |
public class StartCropImageView extends ImageView { | |
Matrix matrix = new Matrix(getImageMatrix()); | |
public StartCropImageView(Context context) { | |
super(context); | |
setScaleType(ScaleType.MATRIX); | |
setPivotX(0); | |
} | |
public StartCropImageView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
setScaleType(ScaleType.MATRIX); | |
setPivotX(0); | |
} | |
public StartCropImageView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
setScaleType(ScaleType.MATRIX); | |
} | |
@Override | |
protected boolean setFrame(int l, int t, int r, int b) { | |
matrix.set(getMatrix()); | |
if (getDrawable() != null) { | |
float scale; | |
final int viewWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight(); | |
final int viewHeight = getMeasuredHeight() - 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; | |
} | |
if (scale > 0) { | |
matrix.setScale(scale, scale); | |
setImageMatrix(matrix); | |
} | |
} | |
return super.setFrame(l, t, r, b); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment