Android ImageView宽度顶边显示,高度保持比例 Android开发实现ImageView宽度顶边显示,高度保持比例的方法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android ImageView宽度顶边显示,高度保持比例 Android开发实现ImageView宽度顶边显示,高度保持比例的方法

zst1303939801   2021-03-28 我要评论
想了解Android开发实现ImageView宽度顶边显示,高度保持比例的方法的相关内容吗,zst1303939801在本文为您仔细讲解Android ImageView宽度顶边显示,高度保持比例的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,ImageView,宽度顶边显示,高度保持比例,下面大家一起来学习吧。

本文实例讲述了Android开发实现ImageView宽度顶边显示,高度保持比例的方法。分享给大家供大家参考,具体如下:

ImageView 图片宽度顶边显示,高度保持比例

1、在布局中设置

<ImageView
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:paddingLeft="5dp"
  android:paddingRight="2.5dp"
  android:layout_weight="1"
  android:scaleType="fitXY"
  android:adjustViewBounds="true"
  android:src="@drawable/default_wallpaper_collection_cover"/>

主要是代码:

android:scaleType="fitXY" :填充宽度match_parent
android:adjustViewBounds="true" :高度保持比例

2、代码实现

public class MImageView extends ImageView {
  public MImageView(Context context) {
    super(context);
  }
  public MImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }
  public MImageView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }
  @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);
    }
  }
}

希望本文所述对大家Android程序设计有所帮助。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们