0

我正在尝试在运行时设置 ImageButton 的图像。它应该填充屏幕的宽度并使用正确缩放所需的高度空间。

ImageButton 声明如下:

    <ImageButton
        android:id="@+id/map_plan_topview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:onClick="onMapImageClicked"
        android:background="#0000"
        android:scaleType="fitXY" />

现在我正在尝试使用以下方式设置图像:

        ImageButton topView = (ImageButton) findViewById(R.id.map_plan_topview);
        Drawable d = Drawable.createFromPath(path);
        topView.setImageDrawable(d);

图像宽度使用 fill_parent 缩放,但高度未正确拉伸。如果我 topView.setImageResource(R.drawable.button_where_citymap);改用,一切正常,但我确实想在运行时从文件路径设置源。

我究竟做错了什么?

4

2 回答 2

0

fitXY stretches the image without respecting proportions. If proportions matter, then use center_crop, or center_inside

You can set the ScaleType in code as well:

ImageButton topView = (ImageButton) findViewById(R.id.map_plan_topview);
Drawable d = Drawable.createFromPath(path);
topView.setScaleType(ScaleType.CENTER_CROP);
topView.setImageDrawable(d);
于 2012-12-03T15:26:50.200 回答
0

您应该使用“ SetImageResource”代替“ setImage Drawable”...!

于 2013-07-06T05:57:45.103 回答