图片高度按原始宽高比例自适应
2020-05-08 本文已影响0人
爱我O就直说
//图片高度按原始宽高比例自适应
@SuppressLint("AppCompatCustomView")
public class FitImageView extends ImageView {
public FitImageView(Context context) {
super(context);
}
public FitImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
Drawable drawable = getDrawable();
if(drawable!=null){
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = (int) Math.ceil((float) width * (float) drawable.getIntrinsicHeight() / (float) drawable.getIntrinsicWidth());
setMeasuredDimension(width, height);
}else{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}