Android開發(fā)實(shí)現(xiàn)ImageView寬度頂邊顯示,高度保持比例的方法
本文實(shí)例講述了Android開發(fā)實(shí)現(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、代碼實(shí)現(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);
}
}
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android TextSwitcher實(shí)現(xiàn)文字上下翻牌效果(銅板街)
這篇文章主要介紹了Android TextSwitcher實(shí)現(xiàn)文字上下翻牌效果(銅板街),需要的朋友可以參考下2017-05-05
Android實(shí)現(xiàn)監(jiān)聽音量的變化
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)監(jiān)聽音量的變化,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
Android中ImageView實(shí)現(xiàn)選擇本地圖片并顯示功能
本文主要介紹了android中ImageView實(shí)現(xiàn)選擇本地圖片并顯示功能的示例代碼。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04
Android編程實(shí)現(xiàn)只顯示圖片一部分的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)只顯示圖片一部分的方法,涉及Android針對(duì)圖片的局部顯示操作技巧,需要的朋友可以參考下2016-10-10
Android開發(fā)中ViewPager實(shí)現(xiàn)多頁面切換效果
ViewPager用于實(shí)現(xiàn)多頁面的切換效果,該類存在于Google的兼容包里面,所以在引用時(shí)記得在BuilldPath中加入“Android-support-v4.jar”。具體詳情大家可以參考下本文2016-11-11
解析離線安裝Eclipse的Android ADT開發(fā)插件的具體操作(圖文)
本篇文章是對(duì)離線安裝Eclipse的Android ADT開發(fā)插件的具體操作進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android超清晰6.0權(quán)限申請(qǐng)AndPermission
這篇文章主要介紹了Android超清晰6.0權(quán)限申請(qǐng)AndPermission,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
android實(shí)現(xiàn)左右側(cè)滑菜單效果
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)左右側(cè)滑菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10

