自定义一个按照宽高比固定的imageview,默认ScaleType.FIT_XY,需要改动的自己修改源码即可
public class MyImageView extends AppCompatImageView {
//宽高比float类型
private float mRatio = 1.0f;
//宽高比String类型
private String mScale = "1:1";
//
private String mTag = "default";
public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyImageView);
// mRatio = typedArray.getFloat(R.styleable.MyImageView_ratio, 0.0f);
mScale = typedArray.getString(R.styleable.MyImageView_scale);
mTag = typedArray.getString(R.styleable.MyImageView_mTag);
typedArray.recycle();
this.setScaleType(ScaleType.FIT_XY);
init();
}
//计算比列
private void init() {
if (!mScale.contains(":")) {
System.out.println("--无法设定宽高比,输入的比列格式不正确--");
thro