位图(imageview)没有正确缩放,以适应线性布局



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

我有一个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 height与linearlayout height不匹配

imageview看起来比需要的小,我不知道我在哪里失踪了因此,任何帮助都是非常感谢的。

你可以这样做…

-计算使用

的最大可能大小
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);

最新更新