调整ImageView的大小,即使是ScaleType



我有一个问题,我正试图调整ImageView及其父容器的大小,但问题是ImageViewScaleType:"matrix",我无法调整图像或其容器的大小这是我的代码:

public void resize() {
    LayoutParams contet = relativeLayout.getLayoutParams();
    LayoutParams photo =  image.getLayoutParams();

    int widPhoto = image.getWidth();
    int hePhoto = image.getHeight();
    photo.width = ((widPhoto * 6) / 2);
    photo.height = ((hePhoto * 6) / 2);
    contenedor.width = widPhoto * 2;
    contenedor.height = hePhoto * 2;

    relativeLayout.setLayoutParams(contenedor); 
    image.setLayoutParams(photo );
}

和我的xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/containerImg"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="matrix" />
</RelativeLayout>

setLayoutParams()不做什么,但删除属性ScaleType,如果我调整一切正确,但我不能删除该属性。有办法调整大小吗?他们可以把财产ScaleType: NONE ?非常感谢,很抱歉我的英语不好

要调整ImageView的大小,我建议使用FrameLayout:

<FrameLayout
    android:id="@+id/fm_id"
    android:layout_width="img_width"
    android:layout_height="img_height"
    ... ...
    >
   <ImageView
     android:id="@+id/imageView1"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:adjustViewBounds="true"
     android:layout_gravity="center"
     android:gravity="center"
     />
</FrameLayout>

那么你只需要调整代码中的'img_width'和'img_height'的大小

我得到了解决方案,如果你正在使用Layout ScaleType: matrix你需要使用matrix.setScale ();来调整

相关内容

最新更新