ImageView动态设置宽高

要求宽高比:宽高640240px
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
app:layout_constraintTop_toBottomOf="@+id/linearLayout1">

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="match_parent"
            android:layout_height="120dp"
            android:scaleType="centerCrop"
            android:src="@mipmap/image032504" />
    </LinearLayout>

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);
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容