Fit image into ImageView, keep aspect ratio and then resize ImageView to image dimensions?

How to fit an image of random size to an ImageView? When: Initially ImageView dimensions are 250dp * 250dp The image’s larger dimension should be scaled up/down to 250dp The image should keep its aspect ratio The ImageView dimensions should match scaled image’s dimensions after scaling E.g. for an image of 100*150, the image and … Read more

How to set margin of ImageView using code, not xml

I want to add an unknown number of ImageView views to my layout with margin. In XML, I can use layout_margin like this: <ImageView android:layout_margin=”5dip” android:src=”https://stackoverflow.com/questions/3416087/@drawable/image” /> There is ImageView.setPadding(), but no ImageView.setMargin(). I think it’s along the lines of ImageView.setLayoutParams(LayoutParams), but not sure what to feed into that. Does anyone know? 16 Answers 16

Scale Image to fill ImageView width and keep aspect ratio

I have a GridView. The data of GridView is request from a server. Here is the item layout in GridView: <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:background=”@drawable/analysis_micon_bg” android:gravity=”center_horizontal” android:orientation=”vertical” android:paddingBottom=”@dimen/half_activity_vertical_margin” android:paddingLeft=”@dimen/half_activity_horizontal_margin” android:paddingRight=”@dimen/half_activity_horizontal_margin” android:paddingTop=”@dimen/half_activity_vertical_margin” > <ImageView android:id=”@+id/ranking_prod_pic” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:adjustViewBounds=”true” android:contentDescription=”@string/app_name” android:scaleType=”centerCrop” /> <TextView android:id=”@+id/ranking_rank_num” android:layout_width=”wrap_content” android:layout_height=”wrap_content” /> <TextView android:id=”@+id/ranking_prod_num” android:layout_width=”wrap_content” android:layout_height=”wrap_content” /> <TextView android:id=”@+id/ranking_prod_name” android:layout_width=”wrap_content” android:layout_height=”wrap_content” /> … Read more

How to create a circular ImageView in Android? [duplicate]

This question already has answers here: How to make an ImageView with rounded corners? (58 answers) Closed 8 years ago. How could I create a rounded ImageView in Android? I have tried the following code, but it’s not working fine. Code: Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); BitmapShader shader = new BitmapShader (bitmap, TileMode.CLAMP, TileMode.CLAMP); … Read more

Changing ImageView source

I have an ImageView with a source image set in the xml using the following syntax: <ImageView android:id=”@+id/articleImg” style=”@style/articleImgSmall_2″ android:src=”https://stackoverflow.com/questions/2974862/@drawable/default_m” /> Now I need to change this image programmatically. What I need to do is delete the old image and add a new one though. What I have done is this: myImgView.setBackgroundResource(R.drawable.monkey); It works but … Read more