图片高度按原始宽高比例自适应
作者:
爱我O就直说 | 来源:发表于
2020-05-08 09:46 被阅读0次//图片高度按原始宽高比例自适应
@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);
}
}
}
本文标题:图片高度按原始宽高比例自适应
本文链接:https://www.haomeiwen.com/subject/smuxnhtx.html
网友评论