国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android開發(fā)實現(xiàn)ImageView寬度頂邊顯示,高度保持比例的方法

2019-10-22 18:15:41
字體:
供稿:網(wǎng)友

本文實例講述了Android開發(fā)實現(xiàn)ImageView寬度頂邊顯示,高度保持比例的方法。分享給大家供大家參考,具體如下:

ImageView 圖片寬度頂邊顯示,高度保持比例

1、在布局中設(shè)置

<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、代碼實現(xiàn)

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程序設(shè)計有所幫助。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 正阳县| 丁青县| 山西省| 张家港市| 东乡族自治县| 谷城县| 岢岚县| 绥化市| 平顶山市| 阿勒泰市| 磐安县| 电白县| 永仁县| 永昌县| 青川县| 郧西县| 哈巴河县| 微山县| 阿拉善左旗| 即墨市| 西青区| 石首市| 旅游| 宁化县| 金阳县| 棋牌| 焉耆| 西宁市| 阜新| 慈溪市| 阜平县| 桂平市| 博爱县| 原阳县| 衢州市| 互助| 石城县| 惠东县| 浠水县| 聂拉木县| 兰考县|