2015-01-14 5 views
0

내 응용 프로그램에는 사용자 프로필을 표시하는 Activity이 있습니다. 사용자는 기기의 카메라 사진이나 페이 스북 사진 등에서 선택하여 프로필 사진을 설정할 수 있습니다. 그러나 사진이 너무 커서 사진이 너무 커서 자동으로 크기를 조정해야합니다. 때문에 내 질문에 대한 답변에디스플레이 크기에 따라 이미지의 크기가 자동으로 조정됩니다.

편집

내가 뭔가를 시도했다.

public static int getDisplayWidth(Activity activity) { 
    DisplayMetrics displaymetrics = new DisplayMetrics(); 

    activity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 

    return displaymetrics.widthPixels; 
} 

public static int getDisplayHeight(Activity activity) { 
    DisplayMetrics displaymetrics = new DisplayMetrics(); 

    activity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 

    return displaymetrics.widthPixels; 
} 

public static int getDrawableWith(Context context, int id) { 
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), id); 

    return bitmap.getWidth(); 
} 

public static int getDrawableHeight(Context context, int id) { 
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), id); 

    return bitmap.getHeight(); 
} 

public static int getHeightOfImage(int originalWidth, int originalHeight, int targetWidth) { 
    double ratio = ((double)originalWidth/(double)originalHeight); 

    return (int)(targetWidth/ratio); 
} 

그리고 난 지금 크기가 조정 된 당김을 작성하고 내 이미지 뷰에 설정하려고 :이 그냥 디스플레이 크기에 따라 이미지 크기를 조정하기 위해 잘 작동

RelativeLayout.LayoutParams layoutParamsView = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); 

this.setLayoutParams(layoutParamsView); 

int originalWidth = Helper.getDrawableWith(this.getContext(), R.drawable.background_wave); 
int originalHeight = Helper.getDrawableHeight(this.getContext(), R.drawable.background_wave); 

int targetWidth = Helper.getDisplayWidth((Activity)this.getContext()); 
int targetHeight = Helper.getHeightOfImage(originalWidth, originalHeight, targetWidth); 

RelativeLayout.LayoutParams layoutParamsImageview = new RelativeLayout.LayoutParams(targetWidth, targetHeight); 
layoutParamsImageview.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
layoutParamsImageview.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 

Drawable drawable = Helper.getScaledDrawable(this.getContext(), R.drawable.background_wave, targetWidth, targetHeight); 

this.imageViewBackgroundWave = new ImageView(this.getContext()); 
this.imageViewBackgroundWave.setLayoutParams(layoutParamsImageview); 
this.imageViewBackgroundWave.setImageDrawable(drawable); 

this.addView(this.imageViewBackgroundWave); 

좀 도우미 방법을 만들어 .

답변

1

가장 쉬운 방법은 크기 조정을 위해 을 Drawable으로 변환하는 것입니다.

How to convert a Bitmap to Drawable in android?

(첫 번째 "솔루션"이었다) 마음 메모리에 유지하지만

- 큰 이미지를 그릴 수는 메모리를 많이 소모하므로 "확장"도.

+0

감사합니다. 이것은 정답이었고 솔루션을 보여주기 위해 내 게시물을 업데이트했습니다. – Mulgard

관련 문제