要求宽高比:宽高640240px

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="15dp"

app:layout_constraintTop_toBottomOf="@+id/linearLayout1">

android:id="@+id/imageView3"

android:layout_width="match_parent"

android:layout_height="120dp"

android:scaleType="centerCrop"

android:src="@mipmap/image032504" />

int width = ViewUtil.getScreenWidth(context);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, (width - ViewUtil.dp2px(context, 32)) * 240 / 640);

imageView3.setLayoutParams(params);

Glide.with(context).load(dataBean.getImages()).into(imageView3);

用到所需方法:

private static int mScreenWidth;

/**

* 获取手机屏幕宽度

*

* @param context 上下文

* @return 屏幕的宽度

*/

public static int getScreenWidth(Context context) {

if (mScreenWidth <= 0) {

mScreenWidth = context.getResources().getDisplayMetrics().widthPixels;

}

return mScreenWidth;

}

/**

* 将dp值转换为px值

*/

public static int dp2px(Context context, float dpValue) {

final float scale = context.getResources().getDisplayMetrics().density;

return (int) (dpValue * scale + 0.5f);

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐