In my application i have an Activity
to show the users profile. The user can set its profile picture by selecting it from the devices camera pictures or facebook pictures, etc. However those pictures could be very very big so i need to somehow automatically scale the pcitures down if they are too big.
EDIT
Due to an answer to my question i tried something. I created some Helper methods:
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);
}
And i now tried to create a resized drawable and to set it to my imageview:
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);
This just works fine to resize the image depending on the display size.