如何动态更改 ImageView 高度

How to dynamically change ImageView Height(如何动态更改 ImageView 高度)

本文介绍了如何动态更改 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="QGRyYXdhYmxlL2R1bW15X2ltYWdl"
        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 高度