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);
좀 도우미 방법을 만들어 .
감사합니다. 이것은 정답이었고 솔루션을 보여주기 위해 내 게시물을 업데이트했습니다. – Mulgard