位图(imageview)无法正确缩放,以完全适合linearlayout

时间:2012-12-29 16:17:15

标签: bitmap android-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);

我已经从数据库生成了scaled-bitmap

我已将其解码如下

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高度与botlayout height匹配。

imageview看起来比要求的要小,我不知道我在哪里失踪 所以任何帮助都是适当的。

提前致谢。

1 个答案:

答案 0 :(得分:0)

你可以做下面这样的事情......

- 使用

计算最大可能的大小
options.inSampleSize = Math.max(inWidth/dstWidth, inHeight/dstHeight);  //still it will return image larger than your targeted size

- 使用

加载图像
Bitmap roughBitmap = BitmapFactory.decodeStream(in, null, options);

- 使用

最终调整到所需的尺寸
Bitmap resizedBitmap = Bitmap.createScaledBitmap(roughBitmap, (int) (roughBitmap.getWidth() * values[0]), (int) (roughBitmap.getHeight() * values[4]), true);