Prevent Image Scaling in Android App - android

I have an image that I am downloading from the internet in my android application. I do not want this image to be resized/scaled. Is there anyway I can stop the image from being scaled?
Here is my XML entry for the ImageView:
<ImageView
android:id="#+id/conditionImage"
android:scaleType="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textMain"
android:contentDescription="#string/noaaLogoDescription" />
Here is my code for showing it in the image view:
BitmapFactory.Options bmpOptions = new Options();
bmpOptions.inScaled = false;
URL url = new URL("URLPATHTOIMAGE");
ImageView conditionImageView = (ImageView)findViewById(R.id.conditionImage);
InputStream content = (InputStream)url.getContent();
conditionImage = BitmapFactory.decodeStream(content, null, bmpOptions);
conditionImageView.setImageBitmap(conditionImage);
I have tried changing the scaleType in the ImageView XML element to use matrix, center, and centerInside. My last try was using the above code to set inScale to false. Does anyone have any other ideas?
The odd part is this is not happening in the emulator.

Have you put all images in simple "drawable" folder. I mean without extending hdpi,mdpi etc.

Try set
android:layout_width="wrap_content"
android:adjustViewBounds="true"
EDIT:
Not sure why, but changing
conditionImageView.setBackgroundDrawable(getResources().getDrawable(R.id.conditionImage));
to
conditionImageView.setImageDrawable(getResources().getDrawable(R.id.conditionImage));
solves my problem.

Adnan, using your example of giving the image view a fixed width and height and also removing the scaleType=centerImage fixed the issue.

This is a simple problem. The scaling happens when the ImageView is a lot larger than the rendered image itself. To avoid this ugly pixelated scailing, just "wrap content" the imageview and you should get your original image quality.

I resolved this problem by hardcoding height of imageview ( as I know it in advance) and then set the scaleType as cropCenter
see the below code for more clearity.
<ImageView
android:id="#+id/iv"
android:layout_width="match_parent"
android:layout_height="75dp"
android:scaleType="centerCrop" />

Related

Android Picasso resize() not consistent

I am using Picasso to resize my background images on my Android interface.
I am stretching the width of the image to fill the width, but I cannot adjust the height, as the background image needs to line up with the circular image in the middle.
Problem is that on different devices, the height appears to adjust as the background is not positioned consistently. I would post an example but I can't as I dont have 10 reputation yet.
Here is my java
ImageView imgBackground = (ImageView) v.findViewById(R.id.imgBackground);
aParent = (MainActivity) getActivity();
//Picasso.with(getActivity()).load(R.drawable.morn_blur_bg).fetch();
Picasso.with(aParent.getApplicationContext())
.load(R.drawable.morn_blur_bg)
.resize(aParent.getBackgroundWidth(), aParent.getBackgroundHeight())
.into(imgBackground);
And this is my XML
<ImageView
android:id="#+id/imgBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:layout_alignParentTop="true" />
Any help is greatly appreciated.
It turns out that the imageview was stretching the image after picasso did its thing.
I tried to set the imageview height, but the OS overrode me.
In the end I set imageView.scaletype="matrix", and now it does not stretch my image.
hope that helps someone else, i've spent a lot of time on this.
In my case, I put android:scaleType="centerCrop" on my ImageView and then no need to apply centerCrop() through Picasso.

Is it possible to display original image in android without fit to device views?

I have an Image of size 1000px*1200px and i want it to be displayed without being scaled!.
I have searched in stackover flow and google i am not getting good one.
I want full image without scaled,
I have tried like this
Step1:-
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image"
/>
Step2:-
I have tried with scaleType also
add a scaleType to your ImageView
<ImageView
android:scaleType="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image" />
But this is also not working,
In sclate type i have tried with all possible properties (matrix,fitXY like this).
is there any possibility there for doing to dispaly original image in android?
Plz help me on this
You can try this approach,first get image from drawable and create a bitmap of it.Then get width and height of bitmap, resize your image view and then display image in it.It will create imageview with your original image size.
See below code -
BitmapDrawable bitmap = (BitmapDrawable) this.getResources().getDrawable(R.drawable.icon);
int bitmapHeight= bitmap .getBitmap().getHeight();
int bitmapWidth = bitmap .getBitmap().getWidth();
Now you have dimension of your original image.
Re-size image view -
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
imageView.getLayoutParams().height = bitmapHeight;
imageView.getLayoutParams().width = bitmapWidth;
and in last step set bitmap to your image view -
imageView.setImageBitmap(bitmap);
Hope it will help you.
EDITED
Use below code after resize image view -
replace - `imageView.setImageBitmap(bitmap);`
by
imageView.setImageResource(R.drawable.icon);
By your comments, I think what you need is a library like PhotoView.Hope it can help you
Although I would highly condemn such UX but whatever works for you.
1) Use the following link to make a two dimensional scroll.
Two dimensional scrolling... a suggestion that needs feedback
2) Add an imageview to the same and set your image dimensions for the imageview in px at runtime.
as far as i understand your question is that u want natural size of image without ImageView tag modifying it. This worked for me
<LinearLayout
android:id="#+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/image_here"
android:orientation="horizontal|vertical"/>
let me know in the comments.
Make your image layout width and height to wrap content. Make scaleType centre. Check your device width and height through device manager then give image height and width in %
After many trails i have find a solution for above question.
Here is the code:
MainActivity:-
public class MainActivity extends ActionBarActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) this.findViewById(R.id.imageview);
}
}
activity_main.xml:-
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="#+id/ScrollView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none"
xmlns:android="http://schemas.android.com/apk/res/android">
<HorizontalScrollView android:id="#+id/HorizontalScrollView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:id="#+id/imageview"
android:src="#drawable/desert"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:adjustViewBounds="true">
</ImageView>
</HorizontalScrollView>
</ScrollView>
This is working perfectly for my requirement.
Hope this helps to others.

Android ImageView dynamic ScaleType

I have an Android Activity with an ImageView.
I put the ScaleType of the imageview on CENTER_CROP, so the image itself would not stretch.
<ImageView
android:id="#+id/imgView"
android:layout_width="fill_parent"
android:layout_height="135dp"
android:scaleType="centerCrop"
android:src="#drawable/capture" />
The image that's loaded at first does what I expect, it's cropped to the center, filling the whole ImageView.
Now, if I set the image of the ImageView programatically,
imgView.setImageBitmap(b)
imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
The picture gets displayed, but it's completely stretched out to fill the ImageView...
AdjustViewBounds has no effect what so ever, nor does anything else.
The difference between the working XML and the not-working java code is that in XML I'm putting the image as a resource, and the code is putting a Drawable in the ImageView from a web-service...
Does anyone have an idea to the solution?
I am not sure but android:scaleType="fitXY" or android:scaleType="fitCenter" may be the solution of your problem.
I solved this by using the original image from my camera-intent. For some reason, all thumbnail bitmaps were stretched out and in landscape mode.

ImageView doesn't rescale Image to selected size

I'm using a ImageView with a fixed size for adding an icon to a menu. In my application, I use it a lot of times, but on this ImageView the Layout Params seem to not work.
Unlike the others ImageViews, in this case, I'm using a template directly, but I think that's not the problem.
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="1dp"
android:layout_height="1dp"
android:scaleType="fitCenter"
android:src="#drawable/ic_menu_moreoverflow_normal_holo_dark"
android:contentDescription="ICON" />
Its been used in code as follows.
ImageView iview =(ImageView) View.inflate(context, R.layout.icon, null);
Theoretically, It should resize automatically the image, however, the images continues with the original size, although the size was 1dp. Where is the problem? Thanks a lot!
android:scaleType="fitCenter"
you should remove it.
Change the ImageView assignment of the resource from src to background.

Swapping Image in ImageView

I've been trying to implement simple (I think) thing in my app. I thought it would be simple, but somehow it doesn't work as I would wanted it to.
What I'm trying to do is:
There is an activity with ImageView in center - it has it's own image (well, there's two of them).
I want to press that ImageView (or it could be ImageButton), open the gallery/take picture with camera.
Then, after taking/picking a picture, I want to make that ImageView display the thumbnail of picture.
My layout
<ImageView
android:id="#+id/add_photo_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:src="#drawable/add_photo_button" />
<ImageView
android:id="#+id/add_photo_right"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="true"
android:src="#drawable/add_photo_button" />
</LinearLayout>
My code - after getting picture path
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inSampleSize = 8;
Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/5.png",bmpFactoryOptions);
lastClicked.setImageBitmap(bitmap);
//lastClicked.setImageResource(R.drawable.green_circle);
What I wanted to do is to read the resized file (using inSampleSize) from sdcard, and then fill ImageView (or ImageButton) with it.
But after using setImageBitmap function, the view disappears - like it just loaded empty image (commented line works properly - it's just green dot).
Maybe someone has approached similar problem? I would much appreciate any kind of help.
Greetz,
scana
EDIT:
ofc lastClicked:
lastClicked = (ImageView)findViewById(R.id.add_photo_left);
Why don't you try lastClicked.invalidate() ?
I do so in my code at least and it works this way ;)

Categories

Resources