2013-06-04 2 views
2
나는이 기능으로 이미지의 크기를 감소하고

:감소 BitmapDrawable 크기

Drawable reduceImageSize (String path) 
{ 
    BitmapDrawable bit1 = (BitmapDrawable) Drawable.createFromPath(path); 
    Bitmap bit2 = Bitmap.createScaledBitmap(bit1.getBitmap(), 640, 360, true); 
    BitmapDrawable bit3 = new BitmapDrawable(getResources(),bit2); 
    return bit3; 
} 

그리고 미세 I 앱이 느린지고이 함수를 여러 시간을 호출 할 때, 유일한 문제가되는 작업, 어떤 방식이 그 이 기능을 최적화 하시겠습니까? 아마 매트릭스를 통해 크기를 줄일 수 있을까요? 또한 SD 카드에서 이미지를 읽으며 애니메이션의 Drawable로 뒷면이 필요하며이 기능을 통해이 기능을 사용할 수 있습니다.

답변

4

사용 BitmapFactory.OptionsinJustDecodeBounds 그것을 축소하기 :

Bitmap bitmap = getBitmapFromFile(path, width, height);

public static Bitmap getBitmapFromFile(String path, int width, int height) { 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(path, options); 

    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, width, height); 

    options.inJustDecodeBounds = false; 
    Bitmap bitmap = BitmapFactory.decodeFile(path, options); 
    return bitmap; 
} 

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 
     if (width > height) { 
      inSampleSize = Math.round((float)height/(float)reqHeight); 
     } else { 
      inSampleSize = Math.round((float)width/(float)reqWidth); 
     } 
    } 
    return inSampleSize; 
} 

는 여기에 대해 자세히 알아보기 : 또한 Loading Large Bitmaps Efficiently

, 나는 당신이이 메서드를 호출하는 곳 모르겠지만 만약 당신이 그들 중 많은 숫자를 가지고 있는지 확인하십시오 비트 맵을

을 사용하여 캐싱하고 있는지 확인하십시오 210
관련 문제