2011-01-27 19 views
9

TableRow内でImageViewの幅をプログラムで設定するにはどうすればよいですか?私は、セルの幅を変更する必要はありません、セル内の画像の幅のみ。Android:TableRow内でImageViewの幅をプログラムで設定する方法

レイアウト:(省略記号は、コードを減らすためにある)

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal"> 
<TableRow android:gravity="center_vertical"> 
<Button>...</Button> 
<ImageView 
    android:id="@+id/fruit" 
    android:layout_width="120dp" 
    android:layout_height="44dp" 
    android:src="@drawable/apple" 
    android:scaleType="matrix" 
></ImageView> 
<Button>...</Button> 
</TableRow> 
<TableRow>...</TableRow> 
</TableLayout> 

ジャワ:

ImageView iv = (ImageView) findViewById(R.id.fruit); 
LayoutParams frame = iv.getLayoutParams(); 
frame.width = 100; 
iv.setLayoutParams(frame); 

EDIT:私は画像をトリミングしたいです私が設定した範囲内でテーブルセルを元の幅にします。以下はparagの提案のおかげで私のために働いたコード例です:

Bitmap bmp = BitmapFactory.decodeResource(getResources() , R.drawable.apple); 
int width = 50; 
int height = 44; 
Bitmap resizedbitmap = Bitmap.createBitmap(bmp, iv.getScrollX(), iv.getScrollY(), width, height); 
iv.setImageBitmap(resizedbitmap); 

答えて

31

uはビットマップ

ImageView img=(ImageView)findViewById(R.id.ImageView01); 
    Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.sc01); 
    int width=200; 
    int height=200; 
    Bitmap resizedbitmap=Bitmap.createScaledBitmap(bmp, width, height, true); 
    img.setImageBitmap(resizedbitmap); 
+0

まさに私が必要なもの – keybee

2

画像を拡大縮小しますか?またはそれをトリミングですか?

スケールするには、ImageViewを試してください。 setImageMatrixおよびマトリックス。 preScale

行列を把握したくない場合は、Rects(バウンディングボックス)を試すことができます。 ImageViewで元の矩形を取得します。 getDrawingRectを作成し、必要なRectを定義してMatrixを使用します。 setRectToRectを使用して画像マトリックスを作成します。

それ以外の場合は、ImageViewを試してみましたか? setMaxWidth

+0

setMaxWidth didntの仕事をリサイズもう一つの方法。私はあなたの提案を試みましたが、Matrix.setRectToRectには画像を拡大/縮小するBitmap.Configが必要です。 – caseadilla

関連する問題