本文實例講述了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_parentandroid: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相關內容感興趣的讀者可查看本站專題:《Android開發入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
新聞熱點
疑難解答