Android ImageView未缩放到屏幕宽度

Android ImageView未缩放到屏幕宽度,android,imageview,scale,Android,Imageview,Scale,我使用以下功能将任何图像缩放到屏幕宽度: DisplayMetrics metrics = new DisplayMetrics(); ((Activity)context) .getWindowManager() .getDefaultDisplay() .getMetrics(metrics); int width = bitmap.getWidth(); int height = bitmap.getHeight(); // Calculate the ratio between he

我使用以下功能将任何图像缩放到屏幕宽度:

DisplayMetrics metrics = new DisplayMetrics();
((Activity)context)
.getWindowManager()
.getDefaultDisplay()
.getMetrics(metrics);

int width = bitmap.getWidth();
int height = bitmap.getHeight();

// Calculate the ratio between height and width of Original Image
float ratio = (float) height / (float) width;

int newWidth = metrics.widthPixels; // This will be equal to screen width
float newHeight = newWidth * ratio; // This will be according to the ratio calulated

// calculate the scale
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = newHeight / height;

// create a matrix for the manipulation
Matrix matrix = new Matrix();

// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);

// recreate the new Bitmap
Bitmap resizedBitmap
    = Bitmap.createBitmap(
                bitmap, 0, 0,
                width, height,
                matrix, true
);

// make a Drawable from Bitmap to allow to set the BitMap
// to the ImageView, ImageButton or what ever
return new BitmapDrawable(resizedBitmap);
进行一些检查后,值为,metrics.widthPixels=540,新位图的宽度也为540。这意味着位图或Imageview应该使用全屏宽度。相反,生成的图像视图缺少全屏宽度。我包括一个截图:

如图所示,屏幕的剩余空白部分显示为黑色

创建ImageView的代码是:

ImageView imageBanner = new ImageView(context);
imageBanner.setLayoutParams(new
    LinearLayout.LayoutParams(
        Globals.wrapContent,
        Globals.wrapContent));
imageBanner.setBackgroundResource(R.drawable.imv_banner);
new SyncImage(context, imageBanner, urlImage).execute();
Globals.wrapContent是显式常量,包含标准布局参数的相同值,因此不要认为它们不同。 SyncImage用于在ImageView中下载和显示图像的异步类

请提供将图像缩放至全屏宽度的解决方案,并且图像应保持其原始尺寸比

多谢各位
Ram Ram将imageView的宽度设置为MatchParent而不是WrapContent,并计算高度以保持图像的纵横比。尝试以下代码来调整图像的大小:

    Display display = getActivity().getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int newWidth = size.x;

    //Get actual width and height of image
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    // Calculate the ratio between height and width of Original Image
    float ratio = (float) height / (float) width;
    float scale = getApplicationContext().getResources().getDisplayMetrics().density;
    int newHeight = (int) (width * ratio)/scale;

    return Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true);

看看这里,问题仍然存在。我又申请了一些支票,这是我得到的。我的原始图像比例是1:0.43。在安卓系统中,如果我将图像缩放到这个比例,图像看起来像是拉伸的高度,但如果我按照1:0.3缩放图像,则它非常适合。在计算屏幕宽度时似乎缺少一点。请检查更新的代码。我在计算所需图像的高度时包含了比例因子。这对我有用。