0

我想缩放位图,以完全适合线性布局

我有一个线性布局

 <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 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 高度确实与线性布局高度匹配。

imageview 看起来比要求的要小,我不知道我错过了哪里,所以需要任何帮助。

提前致谢。

4

1 回答 1

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);
于 2013-04-11T08:45:56.683 回答