如何以编程方式调整位图图像的大小而不丢失数据

时间:2014-05-19 07:24:40

标签: android android-layout

我正在开发一个Android应用程序,我希望在屏幕顶部设置一个来自服务器的横幅图像,我需要根据设备大小以编程方式动态调整它。

3 个答案:

答案 0 :(得分:0)

试试这个,

int iWidth = bmp.getWidth();         int iHeight = bmp.getHeight();

    Display display = getWindowManager().getDefaultDisplay();
    DisplayMetrics dm = new DisplayMetrics();
    display.getMetrics(dm);

    int dWidth=dm.widthPixels;
    int dHeight=dm.heightPixels;

    float sWidth=((float) dWidth)/iWidth;
    float sHeight=((float) dHeight)/iHeight;

    if(sWidth>sHeight) sWidth=sHeight;
    else sHeight=sWidth;

    Matrix matrix=new Matrix();
    matrix.postScale(sWidth,sHeight);
    newImage=Bitmap.createBitmap(bmp, 0, 0, iWidth, iHeight, matrix, true);
    imageview.setImageBitmap(newImage);

答案 1 :(得分:0)

假设您要设置在布局中声明的ImageView(例如,使用属性android:layout_width="match_parent" android:layout_height="wrap_content"),您可以调整以下代码:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;  // just compute size, don't create bitmap
BitmapFactory.decodeStream(stream, null, options); // stream is the InputStream representing the image you want to display
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
imageViewWidth = imageView.getWidth(); // imageView is the View container for the image you want to display
imageViewHeight = imageView.getHeight();
// Compute the sample size
double sampleSize = computePowerOfTwoInSampleSize(options, imageViewWidth, imageViewHeight);
if(sampleSize<1) { 
    options.inSampleSize = 1; 
}
else { 
    options.inSampleSize = (int) sampleSize; 
}
options.inJustDecodeBounds = false; // compute size and create bitmap  
bitmap = BitmapFactory.decodeStream(stream, null, options);
imageView.setImageBitmap(bitmap);
try { 
    stream.close(); 
} 
catch (IOException e) { 
    e.printStackTrace(); 
}

方法computePowerOfTwoInSampleSize就是这样的(考虑到调整为2的幂比其他值更有效):

private double computePowerOfTwoInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    int height = options.outHeight;
    int width = options.outWidth;
    double inSampleSize = 1;        
    // compute the up-sample coefficient (power of 2)
    if(height < reqHeight && width < reqWidth) {
        while(height < reqHeight && width < reqWidth) {
            height = height*2;
            width = width*2;
            if(height < reqHeight && width < reqWidth) {
                inSampleSize = inSampleSize/2;
            }
        }
    }
    // compute the down-sample coefficient
    else {
        while(height > reqHeight || width > reqWidth) { 
            // Calculate ratios of height and width to requested height and width (power of 2)
            height = Math.round((float)height/2);
            width = Math.round((float)width/2); 
            inSampleSize = inSampleSize*2;
        }
    }
    return inSampleSize;
}

答案 2 :(得分:0)

您可以先查看xml中ImageView的scale属性

使用此

调整大小
public static Bitmap getResizedBitmap(Bitmap image)
{
    return Bitmap.createScaledBitmap(image, x, y, true);
}