需求:
要求移动端图片宽高1:1显示.并且图片宽度要充满手机屏幕的宽度. 怎么办?
方法1: onMeasure()方法: @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if(mScale != -1){ super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec((int) (MeasureSpec.getSize(widthMeasureSpec) * mScale), MeasureSpec.getMode(widthMeasureSpec))); }else{ super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } 其中mScale 代表 height/width的值.sample<com.mingle.widget.ScaleLayout android:layout_width="fill_parent" app:scale="0.75" android:layout_height="wrap_content"> <ImageView android:layout_width="fill_parent" android:src="@mipmap/bg" android:scaleType="fitXY" android:layout_height="fill_parent" /> </com.mingle.widget.ScaleLayout>
用ScaleLayout 包裹ImageView,用 app:scale 设置宽高比: |