【问题标题】:resize image in android drawable在android drawable中调整图像大小
【发布时间】:2014-11-05 00:24:59
【问题描述】:

我在drawable 文件夹中有全分辨率图片需要根据手机屏幕尺寸调整大小怎么做?

【问题讨论】:

标签: android drawable image-resizing


【解决方案1】:

这样做

/************************ Calculations for Image Sizing *********************************/ 
public Drawable ResizeImage (int imageID) { 
//Get device dimensions 
Display display = getWindowManager().getDefaultDisplay(); 
double deviceWidth = display.getWidth(); 

BitmapDrawable bd=(BitmapDrawable) this.getResources().getDrawable(imageID); 
double imageHeight = bd.getBitmap().getHeight(); 
double imageWidth = bd.getBitmap().getWidth(); 

double ratio = deviceWidth / imageWidth; 
int newImageHeight = (int) (imageHeight * ratio); 

Bitmap bMap = BitmapFactory.decodeResource(getResources(), imageID); 
Drawable drawable = new BitmapDrawable(this.getResources(),getResizedBitmap(bMap,newImageHeight,(int) deviceWidth)); 

return drawable; 
} 

/************************ Resize Bitmap *********************************/ 
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) { 

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

float scaleWidth = ((float) newWidth) / width; 
float scaleHeight = ((float) 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(bm, 0, 0, width, height, matrix, false); 

return resizedBitmap; 
}

【讨论】:

    【解决方案2】:

    要获得宽度,您应该这样做:

    Display display = getWindowManager().getDefaultDisplay();
    double deviceWidth;
    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB_MR1)
                deviceWidth= display.getWidth();
            else {
                display.getSize(size);
                deviceWidth=size.x;
            }
    

    【讨论】:

      猜你喜欢
      • 2011-10-24
      • 1970-01-01
      • 2023-04-03
      • 2015-06-04
      • 2012-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多