2012-12-29 50 views
0

我要縮放的位圖,來精確地裝配到線性佈局位圖(ImageView的)不正確縮放,以適合精確地的LinearLayout

我有一的LinearLayout

<LinearLayout 
         android:id="@+id/lin_layout1" 
         android:layout_width="wrap_content" 
         android:layout_height="fill_parent" 
         android:paddingLeft="10dip" 
         android:paddingRight="10dip" 
         android:paddingTop="20dip" > 

         <!-- <ImageView 
          android:id="@+id/image1" 
          android:layout_width="wrap_content" 
          android:layout_height="fill_parent" 
          android:layout_gravity="bottom" 
          android:layout_marginTop="10dip" 
          android:layout_weight="0.66" 
          android:layout_marginBottom="10dip" 
          android:src="@drawable/abc" 
          /> -->       
        </LinearLayout> 

我已經定義一個imageview的動態如下,

Imageview img = new ImageView(myclass); 

        LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
          LayoutParams.MATCH_PARENT, 1); 
        vp.setMargins(2, 20, 2, 20); 
        vp.gravity=Gravity.BOTTOM;   
        img.setLayoutParams(vp); 
        img.setAdjustViewBounds(true); 

我已經從數據庫中生成放大的位圖

我有如下

public static Bitmap decodeSampledBitmapFromDatabase(byte[] bs, 
      int length, Options options2,int reqwidth,int reqheight) 
    { 

     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 

     BitmapFactory.decodeByteArray(bs, 0, length, options2); 

     // Calculate inSampleSize 
     options.inSampleSize = calculateInSampleSize(options2, reqwidth, reqheight); 
     Log.d("In sample size", ""+options.inSampleSize); 

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     Bitmap bmap= BitmapFactory.decodeByteArray(bs, 0, length, options); 

     return Bitmap.createScaledBitmap(bmap, 130, reqheight, false); 

    } 

    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) 
    { 
     // Raw height and width of image 
     final int height = options.outHeight; 
     final int width = options.outWidth; 
     int inSampleSize = 1; 

     if (height > reqHeight || width > reqWidth) { 
      if (width > height) { 
       inSampleSize = Math.round((float)height/(float)reqHeight); 
      } else { 
       inSampleSize = Math.round((float)width/(float)reqWidth); 
      } 
     } 
     return inSampleSize; 
    } 

解碼,並最終生成位圖設置爲ImageView的

img.setImageBitmap(bmnew); 
img.setOnClickListener(smile_reader); 
img.setOnLongClickListener(smile_reader); 
l1.addView(img);//Added imageview to linearlayout 

,但它不正確縮放,

imageview的高度不匹配的機器人用的LinearLayout高度。

imageview看起來比要求的小,我不知道我在哪裏丟失 所以任何幫助appriciated。

在此先感謝。

+0

地獄o vijay,我也有同樣的問題。你有沒有解決這個問題..? – Dory

回答

0

,你可以這樣做下面...

- 計算最大可能的尺寸使用

options.inSampleSize = Math.max(inWidth/dstWidth, inHeight/dstHeight); //still it will return image larger than your targeted size 

-Load使用

Bitmap roughBitmap = BitmapFactory.decodeStream(in, null, options); 

- 最後調整爲使用

所需的尺寸圖像
Bitmap resizedBitmap = Bitmap.createScaledBitmap(roughBitmap, (int) (roughBitmap.getWidth() * values[0]), (int) (roughBitmap.getHeight() * values[4]), true);