0

我有一个高度为 200 和 400 像素的图像。我想显示所有这些图像的方式是 200 像素的高度。我的意思是说无论图像的大小如何,在显示该图像时,我想将图像显示到 200 像素的高度。图像的其余部分被隐藏。那怎么做呢?我使用了一个代码进行解码,但在这里它拉伸了更大尺寸的图像,然后显示它。就我而言,我不希望图像拉伸,而只显示高度为 200 像素的图像。

我使用的代码:

private Bitmap decodeFile(File f)
{
    try 
    {
        // decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        
        // Find the correct scale value.
        final int REQUIRED_SIZE = 200;
        int height_tmp = o.outHeight;
        while(true)
        {
            if(height_tmp/2 < REQUIRED_SIZE)
                break;          
            height_tmp/=2;
        }

        o.inSampleSize = height_tmp;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o);
    } 
    catch (FileNotFoundException e) 
    {}
    return null;
}
4

2 回答 2

1

好的,所以尝试 layoutParams 以编程方式执行相同的任务。

  public void taskCompleted ()
{

    ImageView iv = new ImageView(this);


    Bitmap completionBitmap = null;
    completionBitmap = BitmapFactory.decodeFile(CommonMessageConstants.BASE_IMAGE_FOLDER_ON_DEVICE +"completionimage.png");


    iv.setImageBitmap(completionBitmap);
    RelativeLayout rl = (RelativeLayout) findViewById(R.id.randomImageView);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    //lp.setMargins(200, 200, 0, 0);

    lp.addRule(RelativeLayout.CENTER_IN_PARENT);


    rl.addView(iv, lp);

    Toast.makeText (getApplicationContext(), msg, Toast.LENGTH_SHORT).show ();

} 
于 2012-06-20T06:24:20.753 回答
1

只需编辑您的 main.xml

    android:layout_width="200dp"
    android:layout_height="200dp"
于 2012-06-20T05:55:04.527 回答