Android ImageView设置长度高度为wrap_content时高度根据图片比例自适应

本文介绍了一种在Android中实现ImageView按图片原始比例缩放的方法,并推荐使用adjustViewBounds属性来简化这一过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现方法很简单,根据图片文件的宽度与ImageView的宽度比例关系算出ImageView的高度

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.util.AttributeSet;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;


/**
 * 按比例缩放
 * @author yjm
 * 2014年10月20日
 */
public class AdaptiveImageView extends ImageView {
	// 控件默认长、宽
	private int defaultWidth = 0;
	private int defaultHeight = 0;


	// 比例
	private float scale = 0;


	public AdaptiveImageView(Context context) {
		super(context);
	}


	public AdaptiveImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}


	public AdaptiveImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}


	@Override
	protected void onDraw(Canvas canvas) {
		Drawable drawable = getDrawable();
		if (drawable == null) {
			return;
		}
		if (getWidth() == 0 || getHeight() == 0) {
			return;
		}
		this.measure(0, 0);
		if (drawable.getClass() == NinePatchDrawable.class)
			return;
		Bitmap b = ((BitmapDrawable) drawable).getBitmap();
		Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
		if (bitmap.getWidth() == 0 || bitmap.getHeight() == 0) {
			return;
		}
		if (defaultWidth == 0) {
			defaultWidth = getWidth();


		}
		if (defaultHeight == 0) {
			defaultHeight = getHeight();
		}


		scale = (float) defaultWidth / (float) bitmap.getWidth();
		defaultHeight = Math.round(bitmap.getHeight() * scale);
		LayoutParams params = this.getLayoutParams();
		params.width = defaultWidth;
		params.height = defaultHeight;
		this.setLayoutParams(params);
		super.onDraw(canvas);
	}


}

图片要足够大



-------------------------------------------------------------------------------------------------华丽的一条大线---------------------------------------------------------------------------------------------


以上一个方法都是我年少,不懂事想出来的,经过一段摸索我找到了解决这个问题的正确方法就是设置ImageView的adjustViewBounds属性就好了,具体查阅adjustViewBounds的用法

<ImageView
            android:id="@+id/img_1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="centerInside"
            android:src="@drawable/img_bg" />


我要处理一个大显示下的文本长度适配:不选择文件的情况下,在中文语言下显示正常,切换到文本较长的语言并且字体设置超大,显示设置较大后,文本会把按钮挤到屏幕边缘,怎么处理<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/dialog_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="@dimen/dimen_84dp" android:orientation="vertical"> <com.google.android.material.appbar.COUIDividerAppBarLayout android:id="@+id/appbar" style="@style/CommonAppBarStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/color_transparent" android:clickable="true" android:focusable="true" android:paddingLeft="@dimen/dimen_0dp" android:paddingRight="@dimen/dimen_0dp" app:elevation="@dimen/toolbar_elevation"> <com.coui.appcompat.toolbar.COUIToolbar android:id="@+id/toolbar" style="@style/COUIToolBarInAppBarLayoutStyle" android:layout_width="match_parent" android:background="@null" app:supportTitleTextAppearance="@style/textAppearanceSecondTitle" app:titleCenter="false" /> <com.coui.appcompat.tablayout.COUITabLayout android:id="@+id/tab_layout" android:background="@color/color_transparent" style="@style/COUISmallTabLayoutStyle" /> </com.google.android.material.appbar.COUIDividerAppBarLayout> <RelativeLayout android:id="@+id/content_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <com.filemanager.common.view.ViewPagerWrapperForPC android:id="@+id/view_pager_wrapper" android:layout_width="match_parent" android:layout_height="match_parent"> <com.filemanager.common.view.viewpager.RTLViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/appbar" android:clipChildren="false" android:clipToPadding="false" android:orientation="horizontal" /> </com.filemanager.common.view.ViewPagerWrapperForPC> </RelativeLayout> </LinearLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="?attr/couiColorBackgroundElevatedWithCard" android:minHeight="@dimen/operation_btn_background_height"> <View android:id="@+id/button_divider" android:layout_width="match_parent" android:layout_height="@dimen/divider_background_height" android:layout_gravity="top" android:alpha="0" android:background="?attr/couiColorDivider" android:forceDarkAllowed="false" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <LinearLayout android:id="@+id/select_root_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginStart="@dimen/dimen_16dp" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="false" android:orientation="horizontal"> <TextView android:id="@+id/select_title_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/not_selected_file" android:textSize="16sp" android:lineHeight="22dp" android:textColor="?attr/couiColorLabelPrimary" android:textAppearance="@style/couiTextHeadlineXS" android:visibility="visible"/> <ImageView android:id="@+id/select_arraw_up" android:layout_width="18dp" android:layout_height="18dp" android:src="@drawable/arrow_up" android:layout_marginLeft="@dimen/dimen_6dp" android:visibility="gone" android:layout_gravity="center"/> </LinearLayout> <TextView android:id="@+id/select_body_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:lineHeight="20dp" android:textSize="14sp" android:textAppearance="?attr/couiTextBodyXS" android:textColor="?attr/couiColorLabelSecondary" android:visibility="gone" android:text="@string/selected_size"/> </LinearLayout> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1"/> <com.coui.appcompat.button.COUIButton android:id="@+id/btn_add_file" style="@style/Widget.COUI.Button.Large" android:layout_width="@dimen/dimen_96dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginEnd="@dimen/dimen_16dp" android:text="@string/label_add_recent_file" /> </LinearLayout> </FrameLayout> </FrameLayout>
最新发布
07-29
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/chat_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:background="#000000" android:keepScreenOn="true" android:layout_height="match_parent"> <LinearLayout android:id="@+id/rl_title" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="wrap_content" android:background="@color/black_alpha_5"> <View android:id="@+id/statusBar" android:layout_width="match_parent" android:layout_height="0dp" android:background="#000000"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="?actionBarSize"> <LinearLayout android:layout_alignParentLeft="true" android:id="@+id/exit" android:visibility="gone" android:layout_width="?actionBarSize" android:gravity="center" android:layout_height="?actionBarSize"> <TextView android:layout_width="wrap_content" android:text="退出" android:textColor="#FFFFFF" android:layout_height="wrap_content"/> </LinearLayout> <TextView android:id="@+id/memberName" android:layout_width="wrap_content" android:text="音频会议" android:textColor="#FFFFFF" android:layout_centerInParent="true" android:layout_height="wrap_content"/> <LinearLayout android:visibility="gone" android:id="@+id/switchCamera" android:layout_centerVertical="true" android:layout_toLeftOf="@id/memberMenu" android:layout_width="44dp" android:gravity="center" android:layout_height="44dp"> <ImageView android:layout_alignParentRight="true" android:layout_width="24dp" android:scaleType="fitXY" android:tint="#FFFFFF" android:src="@mipmap/rc_voip_switch_camera" android:layout_height="24dp"/> </LinearLayout> <LinearLayout android:id="@+id/memberMenu" android:layout_alignParentRight="true" android:layout_width="44dp" android:gravity="center" android:visibility="gone" android:layout_centerVertical="true" android:layout_height="44dp"> <ImageView android:layout_alignParentRight="true" android:layout_width="24dp" android:scaleType="fitXY" android:src="@mipmap/icon_menu_more_member" android:layout_height="24dp"/> </LinearLayout> </RelativeLayout> </LinearLayout> <FrameLayout android:layout_below="@id/rl_title" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="wrap_content" android:layout_gravity="right|bottom" android:visibility="gone" android:layout_marginBottom="150dp" android:layout_height="wrap_content" android:orientation="vertical"> <!--本地预览view--> <org.webrtc.mozi.video.view.RTCSurfaceView android:id="@+id/sf_local_view" android:layout_width="120dp" android:layout_height="150dp" android:layout_marginRight="16dp" android:layout_marginTop="4dp" android:layout_marginBottom="2dp"/> </LinearLayout> <!--会议View--> <FrameLayout android:visibility="gone" android:id="@+id/admin_surface_container" android:layout_width="match_parent" android:layout_height="match_parent" /> <RelativeLayout android:layout_width="match_parent" android:background="@color/dk_color_4c00C9F4" android:layout_height="match_parent"> <LinearLayout android:layout_below="@id/bottomLayout" android:layout_alignParentTop="true" android:layout_width="match_parent" android:orientation="vertical" android:layout_alignParentBottom="true" android:layout_above="@id/bottomLayout" android:layout_height="wrap_content"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <View android:id="@+id/otherView" android:background="#00000000" android:layout_width="match_parent" android:layout_height="match_parent"/> <androidx.recyclerview.widget.RecyclerView android:id="@+id/chat_content_user_list" android:layout_width="match_parent" android:layout_gravity="right" android:background="#50000000" android:layout_height="match_parent"/> </FrameLayout> </LinearLayout> <!--底部按钮--> <LinearLayout android:id="@+id/bottomLayout" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:orientation="vertical" android:layout_gravity="bottom|right" android:layout_height="wrap_content"> <TextView android:id="@+id/time2" android:layout_width="wrap_content" android:textColor="#FFFFFF" android:layout_gravity="center" android:layout_marginBottom="16dp" android:layout_height="wrap_content"/> <LinearLayout android:layout_width="match_parent" android:orientation="horizontal" android:gravity="center" android:layout_marginBottom="40dp" android:layout_height="wrap_content"> <ImageView android:id="@+id/mute" android:layout_width="wrap_content" android:src="@drawable/rc_voip_mute_selector" android:layout_height="wrap_content"/> <ImageView android:id="@+id/hangup" android:layout_marginLeft="44dp" android:layout_marginRight="44dp" android:layout_width="wrap_content" android:src="@mipmap/rc_voip_hang_up" android:background="@null" android:layout_height="wrap_content"/> <ImageView android:id="@+id/audio" android:layout_width="wrap_content" android:src="@drawable/rc_voip_speaker_selector" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout> </RelativeLayout> </FrameLayout> </RelativeLayout> 这是一个安卓页面的xml代码 我想在页面中间展示一个用户列表中的用户名 用户名均以相同大小的圆形展示 用户名过长仅显示前两个字符 列表中的用户过多 按照一定策略换行处理
05-29
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值