How to dynamically change ImageView Height(如何动态更改 ImageView 高度)
问题描述
我有一个简单的线性布局用于 ListView 的单元格,它有一个 imageview.图片是从网上下载的,所以大小可以不同.
I have a simple linear layout used for ListView's cell, and it has an imageview. The image will be downloaded from internet, so the size can be different sizes.
但是,我想将imageview的宽度设置为fill_parent,这是固定的,并在运行时动态改变图像高度.设置图像高度的规则:如果图片的h/w比大于1,把ImageView做成正方形,意思是高度和宽度匹配.
However, I want to set the width of imageview to be fill_parent, which is fixed, and dynamically change the image height at runtime. Rules to set the image height: if the image's h/w ratio is more than 1, make the ImageView square, means match the height to the width.
如果图像的 h/w 比小于 1,则按比例调整大小.预计有两个样本如下.
if the image's h/w ratio is less than 1, size it proportionally. Expected two samples as below.
第一个是 h/w<1,而第二个Cat"是 h/w>1.
The First one is h/w<1, while the second one 'Cat' is h/w>1.
谢谢你的时间.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="@+id/postTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/postImg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:scaleType="centerCrop"
android:src="@drawable/dummy_image"
android:contentDescription="@string/postImage" />
</LinearLayout>
推荐答案
你需要继承 ImageView
You will need to subclass ImageView
覆盖onMeasure,我没有测试过,但是你需要的所有变量都在那里,这个想法是正确的.您只需将图像的纵横比应用到图像视图的高度,如果它大于宽度,则将其设置为宽度.
Override the onMeasure, I haven't tested this but all the variables you need are there and the idea is correct. You just perform the apply the aspect ratio of the image to the imageviews height, and if it's greater than the width, set it to the width.
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
Drawable drawable = getDrawable();
if (drawable != null)
{
//get imageview width
int width = MeasureSpec.getSize(widthMeasureSpec);
int diw = drawable.getIntrinsicWidth();
int dih = drawable.getIntrinsicHeight();
float ratio = (float)diw/dih; //get image aspect ratio
int height = width * ratio;
//don't let height exceed width
if (height > width){
height = width;
}
setMeasuredDimension(width, height);
}
else
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
这篇关于如何动态更改 ImageView 高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何动态更改 ImageView 高度
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01