2014-02-13 2 views
0

이미지의 크기를 다시 조정하고 가로 세로 비율을 유지하려고 시도 할 때 비트 맵 mBitmap은 1200x539를 측정하므로이를 1/3 정도 줄여야합니다.이미지 크기를 유지하는 리사이즈 이미지

mBitmap = Bitmap.createBitmap (mContent.getWidth(), mContent.getHeight(), Bitmap.Config.RGB_565);; 

       int H = (int)mBitmap.getHeight();  
       int W =(int)mBitmap.getWidth();    
       nBitmap = BitmapScaler.setBitmapScale(mBitmap, W,H); 

나는 보스턴의 거리에 의해 제공이 대답을 발견하고 내 응용 프로그램에서 사용하려고했습니다하지만 난 변수를 엉망 수 있으며, 내가 원래 할 수있는 사람 쇼로 빈 이미지를 같은 크기로 무엇입니까 어떻게 올바르게 이것을 달성 할 수 있습니까?

Scaled Bitmap maintaining aspect ratio

코드는 오류없이 실행되지만 원래 같은 이미지를 같은 크기를 반환합니다!

public static Bitmap setBitmapScale(Bitmap originalImage, int width, int height){ 

      Bitmap background = Bitmap.createBitmap((int)width, (int)height, Config.ARGB_8888); 
      float originalWidth = originalImage.getWidth(), originalHeight = originalImage.getHeight(); 
      Canvas canvas = new Canvas(background); 
      float scale = width/originalWidth; 
      float xTranslation = 0.0f, yTranslation = (height - originalHeight * scale)/2.0f; 
      Matrix transformation = new Matrix(); 
      transformation.postTranslate(xTranslation, yTranslation); 
      transformation.preScale(scale, scale); 
      Paint paint = new Paint(); 
      paint.setFilterBitmap(true); 
      canvas.drawBitmap(originalImage, transformation, paint); 
      return background; 
     } 
+0

안녕하세요 알리, 많은 감사는 당신의 코드 조각이 내 문제를 해결했습니다 도움이 될 수 있습니다. – joebohen

답변

0

아래는 내가 내 자신의 목적을 위해 사용하고있는 두 개의 함수입니다, 이것은 당신이

/************************ Calculations for Image Sizing *********************************/ 
public Drawable ResizeImage (int imageID) { 

int newWidth = 1000; //This is new width which can be (1/3) * orignalWidth 

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; 
} 
관련 문제