0

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.

Mulgard
  • 9,877
  • 34
  • 129
  • 232

1 Answers1

1

The easiest way is to convert your Bitmap to a Drawable for scaling purposes.

How to convert a Bitmap to Drawable in android?

Although keep in mind memory (which was your first "solution") - large images, even when "scaled" as a drawable will consume a lot of memory.

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36