Created
July 19, 2017 07:52
-
-
Save wyon/f41e498a46b14161d826b9a387ac26c2 to your computer and use it in GitHub Desktop.
CenterCrop的imageview,水平、垂直方向如果有放大会在该方向移动以居中显示;这里CenterBottomRoundImageView效果如同CenterCrop,并保持水平方向居中能力,但是垂直方向则距底显示。
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
public class CenterBottomRoundImageView extends RoundCornerImageView { | |
private Matrix mMatrix; | |
public CenterBottomRoundImageView(Context context) { | |
super(context); | |
init(); | |
} | |
public CenterBottomRoundImageView(Context context, @Nullable AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
public CenterBottomRoundImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(); | |
} | |
public CenterBottomRoundImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
init(); | |
} | |
private void init() { | |
mMatrix = new Matrix(); | |
super.setScaleType(ScaleType.MATRIX); | |
} | |
@Override | |
public void setImageBitmap(Bitmap bm) { | |
super.setImageBitmap(bm); | |
updateBounds(this, mMatrix); | |
} | |
@Override | |
public void setImageDrawable(@Nullable Drawable drawable) { | |
super.setImageDrawable(drawable); | |
updateBounds(this, mMatrix); | |
} | |
@Override | |
public void setImageIcon(@Nullable Icon icon) { | |
super.setImageIcon(icon); | |
updateBounds(this, mMatrix); | |
} | |
@Override | |
public void setImageURI(@Nullable Uri uri) { | |
super.setImageURI(uri); | |
updateBounds(this, mMatrix); | |
} | |
@Override | |
public void setImageResource(@DrawableRes int resId) { | |
super.setImageResource(resId); | |
updateBounds(this, mMatrix); | |
} | |
@Override | |
protected boolean setFrame(int l, int t, int r, int b) { | |
final boolean result = super.setFrame(l, t, r, b); | |
updateBounds(this, mMatrix); | |
return result; | |
} | |
@Override | |
public void setScaleType(ScaleType scaleType) { | |
} | |
private static void updateBounds(final ImageView iv, @NonNull Matrix matrix) { | |
if (iv == null || iv.getDrawable() == null) { | |
return; | |
} | |
final int width = iv.getWidth(); | |
final int height = iv.getHeight(); | |
if (width == 0 || height == 0 || iv.getDrawable().getIntrinsicHeight() == 0 || iv.getDrawable().getIntrinsicWidth() == 0) { | |
return; | |
} | |
matrix.reset(); | |
float scaleWidth = width / (float) iv.getDrawable().getIntrinsicWidth(); | |
float scaleHeight = height / (float) iv.getDrawable().getIntrinsicHeight(); | |
float scaleFactor = (scaleWidth > scaleHeight) ? scaleWidth : scaleHeight; | |
matrix.setScale(scaleFactor, scaleFactor, 0, 0); | |
if (scaleFactor == scaleHeight) { | |
float tanslateX = ((iv.getDrawable().getIntrinsicWidth() * scaleFactor) - width) / 2; | |
matrix.postTranslate(-tanslateX, 0); | |
} else { | |
float tanslateY = (iv.getDrawable().getIntrinsicHeight() * scaleFactor) - height; | |
matrix.postTranslate(0, -tanslateY); | |
} | |
iv.setImageMatrix(matrix); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment