I am getting image in gallery but some image width is not fit in device width so how to set that image width is device width in my ImageView but height is same for original image height.
below is my set image code here
Uri selectedImageUri = data.getData();
if (null != selectedImageUri) {
// Get the path from the Uri
String path = getPathFromURI(selectedImageUri);
Log.i("", "Image Path : " + path);
// Set the image in ImageView
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
} catch (IOException e) {
e.printStackTrace();
}
imgEdit.setImageURI(selectedImageUri);
}
You have to extend ImageView, this will match_parent width and proportional height:
package com.yourpackage.ui;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
public class DynamicImageView extends ImageView {
public DynamicImageView(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
final Drawable d = this.getDrawable();
if (d != null) {
final int width = MeasureSpec.getSize(widthMeasureSpec);
final int height = (int) Math.ceil(width * (float) d.getIntrinsicHeight() / d.getIntrinsicWidth());
this.setMeasuredDimension(width, height);
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
Then in your XML layout:
<com.yourpackage.ui.DynamicImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"/>
change your image view with it.
<ImageView
android:id="#+id/background"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
/>
Try this
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT));
Or if you want to fill width
imageView.setLayoutParams(new LayoutParams((LayoutParams.FILL_PARENT,LayoutParams.MATCH_PARENT));
Use this and please change aspect ratio as you want
Display display = ((NavigationHome) context).getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight();
int sliderHeight = (int) ((width) / 2.4);
if (height != 0)
imageView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,sliderHeight));
try setting the height to match_parent
something like
<ImageView
android:layout_width="match_parent"
android:layout_height="50dp"/>
Related
i use Universal image loader to load some imageview(large height) to listview.
this is listview(custom for zoom in/out) in activity:
<noname.test1.CustomListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listviewImg" />
this is list_img.xml for listview:
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageview1">
</ImageView>
this is image i want to load (size 400px X 5312px):
link
this is result:
I want image when show is full screen or larger.
How to fix it?
create this class and use this instead of imageView
/**
* Created by farazkhonsari on 1/26/16.
*/
public class ScaledImageView extends ImageView {
public ScaledImageView(Context context) {
super(context);
}
public ScaledImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ScaledImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
try {
Drawable drawable = getDrawable();
if (drawable == null) {
setMeasuredDimension(0, 0);
} else {
int measuredWidth = MeasureSpec.getSize(widthMeasureSpec);
int measuredHeight = MeasureSpec.getSize(heightMeasureSpec);
int width = measuredWidth;
int height = width * drawable.getIntrinsicHeight() / drawable.getIntrinsicWidth();
setMeasuredDimension(width, height);
}
} catch (Exception e) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
and use this class in layout like this:
<ir.farazGroup.YeJoke.tools.ScaledImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/c"
android:scaleType="fitCenter"
/>
you should change scaleType of imageViwe and set adjustViewBound true:
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageview1"
android:scaleType="centerCrop"
android:adjustViewBounds="true">
</ImageView>
you can try other option for scaleType and see which one is good for you!
and you can read about scaleType here:
scaleTypes
Try this solution: [Set this inside onCreate or onCreateView after inflating the list]
imageView = (ImageView) findViewById(R.id. imageview1);
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
float screenWidth = metrics.widthPixels;
imageView.getLayoutParams().width = (int) screenWidth; // Takes the whole screen width
try this i'll hope i will help you
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/imageView"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:src="#drawable/locked_image"
android:scaleType="fitXY"
/>
I have this image:
This is how its appearing on small devices:
It looks kinda squishy. I'd like it that it DOESN'T resize it, displays it as it is, not showing the part of the image that doesn't fit in the screen. Like this
If you haven't noticed, the phone's screen in the pic above has a background which wan't resized, it just took as much as fits the screen from the center image. It cuts the parts that don't fit the screen and takes as much as it needs from the center.
When I use it on a tablet, it works perfectly, since the tablets have a big screen size:
How can I do this?
Currently, this is my code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_sign_in"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.alibdeir.irecipify.SignInActivity"
android:background="#drawable/food_wallpaper">
</android.support.constraint.ConstraintLayout>
I fixed it by making a ScaleImageView java class:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RelativeLayout;
/**
* Created by Ab-Me on 25-Aug-16.
* This is the imageView for the background image
*/
#SuppressWarnings("all")
public class ScaleImageView extends ImageView {
private ImageChangeListener imageChangeListener;
private boolean scaleToWidth = false; // this flag determines if should measure height manually dependent of width
public ScaleImageView(Context context) {
super(context);
init();
}
public ScaleImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public ScaleImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
this.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
}
#Override
public void setImageBitmap(Bitmap bm) {
super.setImageBitmap(bm);
if (imageChangeListener != null)
imageChangeListener.changed((bm == null));
}
#Override
public void setImageDrawable(Drawable d) {
super.setImageDrawable(d);
if (imageChangeListener != null)
imageChangeListener.changed((d == null));
}
#Override
public void setImageResource(int id) {
super.setImageResource(id);
}
public interface ImageChangeListener {
// a callback for when a change has been made to this imageView
void changed(boolean isEmpty);
}
public ImageChangeListener getImageChangeListener() {
return imageChangeListener;
}
public void setImageChangeListener(ImageChangeListener imageChangeListener) {
this.imageChangeListener = imageChangeListener;
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
/**
* if both width and height are set scale width first. modify in future if necessary
*/
if (widthMode == MeasureSpec.EXACTLY || widthMode == MeasureSpec.AT_MOST) {
scaleToWidth = true;
} else if (heightMode == MeasureSpec.EXACTLY || heightMode == MeasureSpec.AT_MOST) {
scaleToWidth = false;
} else
throw new IllegalStateException("width or height needs to be set to match_parent or a specific dimension");
if (getDrawable() == null || getDrawable().getIntrinsicWidth() == 0) {
// nothing to measure
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
return;
} else {
if (scaleToWidth) {
int iw = this.getDrawable().getIntrinsicWidth();
int ih = this.getDrawable().getIntrinsicHeight();
int heightC = width * ih / iw;
if (height > 0)
if (heightC > height) {
// dont let hegiht be greater then set max
heightC = height;
width = heightC * iw / ih;
}
this.setScaleType(ScaleType.CENTER_CROP);
setMeasuredDimension(width, heightC);
} else {
// need to scale to height instead
int marg = 0;
if (getParent() != null) {
if (getParent().getParent() != null) {
marg += ((RelativeLayout) getParent().getParent()).getPaddingTop();
marg += ((RelativeLayout) getParent().getParent()).getPaddingBottom();
}
}
int iw = this.getDrawable().getIntrinsicWidth();
int ih = this.getDrawable().getIntrinsicHeight();
width = height * iw / ih;
height -= marg;
setMeasuredDimension(width, height);
}
}
}
}
(It's OK to simply copy and paste this class, it's the same as using a Library)
Then in your XML (Your parent must be RelativeLayout):
<!-- This is the UI of the class you created -->
<com.your.package.name.ScaleImageView
android:id="#+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/food_wallpaper"
tools:ignore="ContentDescription" />
android:scaleType="centerCrop" makes the image crop to the center. The alignX attributes make the image full screen.
instead of
android:background="#drawable/food_wallpaper"
try
android:src="#drawable/food_wallpaper"
try to add in your xml
android:scaleType="fitXY"
I'm really new at this and I'm trying to load profile pics from gallery and camera into ImageButton. I'm able to add image from gallery but the image does not fit completely into the ImageButton. Some space still gets left out from the imagebutton. I want the image to automatically fit into imagebutton.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.sam.sport.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/LL1" >
<LinearLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp" >
<ImageButton
android:id="#+id/profile_pic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/ic_launcher"
android:scaleType="fitXY"
android:adjustViewBounds="true" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Activity class is as below:-
public class MainActivity extends Activity {
ImageButton ib;
private static Bitmap Image = null;
private static final int GALLERY = 1;
private static Bitmap rotateImage = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ib = (ImageButton)findViewById(R.id.profile_pic);
ib.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ib.setImageBitmap(null);
if (Image != null)
Image.recycle();
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) {
Uri mImageUri = data.getData();
try {
Image = Media.getBitmap(this.getContentResolver(), mImageUri);
if (getOrientation(getApplicationContext(), mImageUri) != 0) {
Matrix matrix = new Matrix();
matrix.postRotate(getOrientation(getApplicationContext(), mImageUri));
if (rotateImage != null)
rotateImage.recycle();
rotateImage = Bitmap.createBitmap(Image, 0, 0, Image.getWidth(), Image.getHeight(), matrix,
true);
ib.setImageBitmap(rotateImage);
} else
ib.setImageBitmap(Image);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static int getOrientation(Context context, Uri photoUri) {
/* it's on the external media. */
Cursor cursor = context.getContentResolver().query(photoUri,
new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);
if (cursor.getCount() != 1) {
return -1;
}
cursor.moveToFirst();
return cursor.getInt(0);
}
}
Convert it to Drawable then use setBackgroundDrawable insted of setImageBitmap
Drawable drawable = new BitmapDrawable(rotateImage);
ib.setBackgroundDrawable(drawable);
Use below line of code for re-sizing your bitmap
Bitmap BITMAP_IMAGE = Bitmap.createBitmap(IMAGE_VIEW.getWidth(),IMAGE_VIEW.getHeight(), Bitmap.Config.RGB_565);
Bitmap resizedBitmap = Bitmap.createScaledBitmap(BITMAP_IMAGE, 100, 100, false); ////Here BITMAP_IMAGE is your bitmap which you want to resize
Get the imageButton height and width
Bitmap bitmapScaled = Bitmap.createScaledBitmap(Image, ib_width, ib_height, true);
Drawable drawable = new BitmapDrawable(bitmapScaled);
ib.setBackgroundDrawable(drawable);
Keep in mind about OOM exception.
To get imageButton height and width look into this and this
I've got the perfect solution. To resize imageview I used the below code as provided in the [Android ImageView adjusting parent's height and fitting width
public class ResizableImageView extends ImageView {
public ResizableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
Drawable d = getDrawable();
if(d!=null){
// ceil not round - avoid thin vertical gaps along the left/right edges
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
setMeasuredDimension(width, height);
}else{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
]1
I've googled a lot finding a lot of solutions for problems similar to mine but not equal.
I have to show a jpg file (as an Ad banner not full screen) that is stored on a server (internet).
I've tried using WebView, but, when the screen width is bigger than the image width, the image appears smaller than the screen.
Is it possible to use ImageView instead of WebView?
If 'Yes' how can I scale the downloaded image to fit different screen resolutions?
Thanks in advance.
Here the solution find there https://stackoverflow.com/a/9288544/2252143:
MainActivity.java:
public class MainActivity extends Activity {
ImageView imgProva = null;
FunzioniUtili Funzioni = new FunzioniUtili();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgProva = (ImageView)findViewById(R.id.imgProva);
// show The Image
new DownloadImageTask((ImageView) findViewById(R.id.imgProva)).execute("http://www.softlive.net/advs/banner_adolfo.jpg");
}
public void onClick(View v) {
startActivity(new Intent(this, MainActivity.class));
finish();
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
int DisplayWidth = 0;
DisplayWidth = Funzioni.ScreenWidth (MainActivity.this);
double CardResizeFactor=1.0;
//This is the function that I use to resize... / CardResizer is redundant, I know!
Funzioni.scaleImage(bmImage,(int)(DisplayWidth / CardResizeFactor),0,0);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
My resize function:
/**
* public void scaleImage(ImageView view, int boundBoxInDp)
*
* Resize the given ImageView.
*
* #param view The ImageView.
* #param boundBoxInDp Scaling factor in Dp.
* #param layoutType 0 = RelativeLayout 1 = LinearLayout 2 = TableLayout 3 = TableRow 4 = FrameLayout.
* #param colNumber Column number if the selected layout is TableRow. If other, put 0
*/
public void scaleImage(ImageView view, int boundBoxInDp, int layoutType, int colNumber)
{
// Get the ImageView and its bitmap
Drawable drawing = view.getDrawable();
Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
// Get current dimensions
int width = bitmap.getWidth();
int height = bitmap.getHeight();
// Determine how much to scale: the dimension requiring less scaling is
// closer to the its side. This way the image always stays inside your
// bounding box AND either x/y axis touches it.
float xScale = ((float) boundBoxInDp) / width;
float yScale = ((float) boundBoxInDp) / height;
float scale = (xScale <= yScale) ? xScale : yScale;
// Create a matrix for the scaling and add the scaling data
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
// Create a new bitmap and convert it to a format understood by the ImageView
Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
BitmapDrawable result = new BitmapDrawable(scaledBitmap);
width = scaledBitmap.getWidth();
height = scaledBitmap.getHeight();
// Apply the scaled bitmap
view.setImageDrawable(result);
// Now change ImageView's dimensions to match the scaled image
if(layoutType == 0)
{
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
params.width = width;
params.height = height;
view.setLayoutParams(params);
}
else if(layoutType == 1)
{
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
params.width = width;
params.height = height;
view.setLayoutParams(params);
}
else if(layoutType == 2)
{
TableLayout.LayoutParams params = (TableLayout.LayoutParams) view.getLayoutParams();
params.width = width;
params.height = height;
view.setLayoutParams(params);
}
else if(layoutType == 3)
{
TableRow.LayoutParams params = (TableRow.LayoutParams) view.getLayoutParams();
params.width = width;
params.height = height;
params.column = colNumber;
}
else if(layoutType == 4)
{
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view.getLayoutParams();
params.width = width;
params.height = height;
view.setLayoutParams(params);
}
}
Here my Activity_Main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<ImageView
android:id="#+id/imgProva"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
you can set the width of the image to 100% with css
see
http://www.w3schools.com/cssref/pr_dim_width.asp
I am using this to display an png image. It will stretch the image to fill the screen vertically keeping its aspect ratio. It seems to only work on API 11+ tho. It's in a RelativeLayout.
<ImageView
android:id="#+id/main_imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/img_alchemy2" />
Ok, I've found the solution and I've edited the question adding the code.
Thanks.
Update : I solved this issue by using the method described in this answer
I'm a bit stuck with this issue, which I think should be pretty simple.
So my app downloads an image, and renders the bitmap in an ImageView, a child element of a RelativeLayout.
I would like the ImageView to fit the parent width, and to adapt it's size to keep the aspect ratio.
Here is my XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout android:id="#+id/banner" android:layout_width="fill_parent" android:layout_height="wrap_content"></RelativeLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
</LinearLayout>
And the code :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RelativeLayout banner = (RelativeLayout) findViewById(R.id.banner);
ImageView imgV = new ImageView(this);
imgV.setScaleType(ImageView.ScaleType.CENTER_CROP);
// I tried all the scale types : CENTER_INSIDE : same effect, FIT_CENTER : same effect...
imgV.setBackgroundColor(0x00FFFF00);
imgV.setAdjustViewBounds(Color.BLUE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
banner.addView(imgV,params);
// Some code downloading the image stream
bitmap = BitmapFactory.decodeStream(stream);
imgV.setImageBitmap(bitmap);
}
Desired :
Result :
Thanks to #Julien and #js.
Here is complete solution of ImageView that will stretch bitmap height preserving aspect ratio even if bitmap is smaller than ImageView.
public class ResizableImageView extends ImageView {
public ResizableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
Drawable d = getDrawable();
if(d!=null){
// ceil not round - avoid thin vertical gaps along the left/right edges
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
setMeasuredDimension(width, height);
}else{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
You can use this class in your xml layouts instead ImageView.
<com.example.ResizableImageView
android:id="#+id/banner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/banner" />
You're probably looking for android:adjustViewBounds="true" in xml or imageView.setAdjustViewBounds(true) in Java.
As I mentioned in a comment, I subclassed the ImageView. I found my code, here you go :
protected class ResizableImageView extends ImageView
{
private Bitmap mBitmap;
// Constructor
public ResizableImageView(Context context)
{
super(context);
}
// Overriden methods
#Override
protected void onMeasure(int widthMeasureSpec,
int heightMeasureSpec) {
if(mBitmap != null)
{
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = width * mBitmap.getHeight() / mBitmap.getWidth();
setMeasuredDimension(width, height);
}
else
{
super.onMeasure(widthMeasureSpec,
heightMeasureSpec);
}
}
#Override
public void setImageBitmap(Bitmap bitmap)
{
mBitmap = bitmap;
super.setImageBitmap(bitmap);
}
}
I think you should change your imgV width to "match_parent"
You should change the scale type to imgV.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
I made a slight change to #Seraphim S's solution to account for cases where the image may be wide, and the view's bounds also wide (for example, rotating the device to landscape mode).
public class ResizableImageView extends ImageView {
public ResizableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable d = getDrawable();
if (d != null) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (width >= height) {
width = (int) Math.ceil((float) height * (float) d.getIntrinsicWidth() / (float) d.getIntrinsicHeight());
} else {
height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
}
setMeasuredDimension(width, height);
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}