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

SetImageResource" " の代わりに " "を使用する必要がありますsetImage Drawable...!

于 2013-07-06T05:57:45.103 に答える
0

fitXY比率を考慮せずに画像を引き伸ばします。比率が重要な場合はcenter_crop、 、またはcenter_inside

ScaleTypeコード内も設定できます。

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 に答える