Reputation: 949
I am setting the profile image of an account by selecting the image from gallery as following
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.profile_image :
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), GALLERY);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GALLERY && resultCode != 0) {
profileImage.setImageBitmap(null);
if (Image != null)
Image.recycle();
Uri mImageUri = data.getData();
try {
Image = MediaStore.Images.Media.getBitmap(this.getContentResolver(), mImageUri);
profileImage.setImageBitmap(Image);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
this works perfect but one problem is there, image height and width get changing for different image. what I want is to fix the image size and width to 150 dp? following is my xml code
<ImageView
android:id="@+id/profile_image"
android:layout_width="150dp"
android:layout_height="150dp"
app:srcCompat="@android:drawable/sym_def_app_icon"
android:clickable="true"/>
Upvotes: 2
Views: 2739
Reputation: 774
you can use "centerCrop" scaleType for this purpose.
<ImageView
android:id="@+id/profile_image"
android:layout_width="150dp"
android:layout_height="150dp"
android:scaleType="centerCrop"/>
Other scale types are :
CENTER
Center the image in the view, but perform no scaling.
CENTER_CROP
Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding).
CENTER_INSIDE
Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding).
FIT_CENTER
Scale the image using CENTER.
FIT_END
Scale the image using END.
FIT_START
Scale the image using START.
FIT_XY
Scale the image using FILL.
MATRIX
Scale using the image matrix when drawing.
Upvotes: 3
Reputation: 6546
You should set LayoutParams to the View you want to resize:
private void resizeImageView(ImageView iv, int w, int h) {
//set width and height in pixels
iv.setLayoutParams(new ViewGroup.LayoutParams(w, h));
//or if w and h are in dp:
//iv.setLayoutParams(new ViewGroup.LayoutParams(dpToPx(this, w), dpToPx(this, h)));
}
private int dpToPx(Activity activity, int dp) {
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, metrics);
}
Upvotes: 1
Reputation: 65
<ImageView
android:id="@+id/profile_image"
android:layout_width="150dp"
android:layout_height="150dp"
app:srcCompat="@android:drawable/sym_def_app_icon"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:clickable="true"/>
please add above two properties to your ImageView
Upvotes: 0