2012-10-19 16 views
1

我試圖設置我的ImageView調整大小,但它沒有通過位圖或其他方法工作我要把我的代碼,所以如果任何人都可以幫助我如何大小ImageView適合在錶行thnx內。我需要在我的表格行中編程設置imageView的大小

public class Test extends Activity 
{ 
    private TableLayout Table1; 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     int[] ImageArray={R.raw.hospital_image,R.raw.hotel_image}; 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.favorites); 
     Table1 = (TableLayout) findViewById(R.id.Table); 
     TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams(
       TableRow.LayoutParams.WRAP_CONTENT, 
       TableRow.LayoutParams.WRAP_CONTENT, 1.0f); 
     for(int i=0;i<ImageArray.length;i++) 
     { 
      TableRow TR = new TableRow(this); 
      TR.setLayoutParams(tableRowParams); 
      ImageView img=new ImageView(this); 
      //What should i do here to resize it ??? 
      Drawable d = getResources().getDrawable(ImageArray[i]); 
      img.setImageDrawable(d); 

      TR.addView(img, new TableRow.LayoutParams(
        TableRow.LayoutParams.WRAP_CONTENT, 
        TableRow.LayoutParams.WRAP_CONTENT, 1.0f)); 

      Table1.addView(TR); 
     } 
    } 

我的XML只持有這些:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ScrollView 
     android:id="@+id/scrollView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" > 

     <TableLayout 
      android:id="@+id/Table1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 
     </TableLayout> 
    </ScrollView> 

</RelativeLayout> 

回答

1

setLayoutParams()應該做的伎倆,你的情況,我相信你會需要TableRow.LayoutParams,就像這樣:

ImageView img = new ImageView(this); 
img.setLayoutParams(new TableRow.LayoutParams(w, h)); 

編輯

嘗試設置DrawablesetLayoutParams

ImageView img = new ImageView(this); 
Drawable d = getResources().getDrawable(ImageArray[i]); 
img.setImageDrawable(d); 
img.setLayoutParams(new TableRow.LayoutParams(w, h)); 
+0

我播種在另一篇文章中,我嘗試過但它保持相同的分辨率文我運行該應用程序。任何更多的解決方案plz –

+0

在'LayoutParams'之前設置'Drawable'並嘗試 – jnthnjns

+0

我做了你所說的但仍然沒有任何事情發生,但是如果你有任何其他想法讓我知道它們,請多多謝謝plz –

6

試試這個,

public class ResizableImageView extends ImageView { 
    public ResizableImageView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public ResizableImageView(Context context) { 
     super(context); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     Drawable d = getDrawable(); 
     if (d == null) { 
      super.setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); 
      return; 
     } 

     int imageHeight = d.getIntrinsicHeight(); 
     int imageWidth = d.getIntrinsicWidth(); 

     int widthSize = MeasureSpec.getSize(widthMeasureSpec); 
     int heightSize = MeasureSpec.getSize(heightMeasureSpec); 

     float imageRatio = 0.0F; 
     if (imageHeight > 0) { 
      imageRatio = imageWidth/imageHeight; 
     } 
     float sizeRatio = 0.0F; 
     if (heightSize > 0) { 
      sizeRatio = widthSize/heightSize; 
     } 

     int width; 
     int height; 
     if (imageRatio >= sizeRatio) { 
      // set width to maximum allowed 
      width = widthSize; 
      // scale height 
      height = width * imageHeight/imageWidth; 
     } else { 
      // set height to maximum allowed 
      height = heightSize; 
      // scale width 
      width = height * imageWidth/imageHeight; 
     } 

     setMeasuredDimension(width, height); 
    } 
} 

用它在你的佈局,你會普通的舊ImageView,只是改變標籤,這樣,

 <com.example.ResizableImageView 
      android:id="@+id/image" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:adjustViewBounds="true" 
      android:padding="1px" 
      android:scaleType="fitCenter" 
      android:src="..." /> 
+0

傑夫只是一個問題,這個類我最實施,以獲得我的圖像調整大小,我可以直接粘貼到我的當前班級? –

+0

這個類是爲了擴大以填充可用空間的圖像。 –

+0

我把它粘貼到一個新的類中,因爲你命名它,我現在在測試活動中如何在我的活動中調用它? 我知道iam問很多,但我真的很感激 –

相關問題