I try to display 3 images medium size.
The problem is that they display in low quality and it damages the UI a lot,I checked over the internet and the best solution is to use is Piccaso,but I want to know how can I display the image in it's full quality without third-library?
I load images like this:
<ImageView
android:id="#+id/btnring"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/itayring1"
/>
and I also tried this:
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.itayicon);
b1.setImageBitmap(bb);
When I used Eclipse I could load images easly. There must be a way to do in Android Studio too,
Thanks.
Put Images in xhdpi drawable image only and try this code :
<ImageView
android:id="#+id/btnring"
android:layout_height="50dp"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:src="#drawable/itayring1"
/>
Related
I am working with an application in which I have more work with images. I want to show profile pictures and Article images in my app. For article images I have tried scale type of ImageView to "fitcenter or centercrop or fitxy" but nothing works fine. My image pixel's are distorted. I am using Glide library to show image on sever. I have even tried .fitcenter() etc. Glide methods but nothing works so that my image work fine with ImageView.
Code:
<ImageView
android:id="#+id/imgArticle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/blank_image" />
Glide Code:
Glide.with(context).load(articleList.get("article_image"))
.thumbnail(0.5f)
.placeholder(context.getResources().getDrawable(R.drawable.blank_image))
.fitCenter()
.into(imgBanner);
I have tried various methods i.e. fitcenter, centercrop etc. but nothing works.
android:scaleType="fitXY" will stretch your image. You can try this code so that image will not stretch.
<ImageView
android:id="#+id/imgArticle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/blank_image" />
Or you can adjust the height and width as per your UI
<ImageView
android:id="#+id/imgArticle"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#drawable/blank_image" />
I have a RecyclerView (It is actually a chat). The users can send images and text. The problem is when I scroll it feels laggy because the images are being loaded (If I go down and up, it takes to much time to Picasso to bring the image back again). And I'm using this line of code to do it.
Picasso.with(itemView.getContext())
.load(((ChatMediaMessage) message).getUrl())
.into(imageView);
If I use the resize option is not laggy at all, but the images lose the image ratio obviously.
Picasso.with(itemView.getContext())
.load(((ChatMediaMessage) message).getUrl())
.resize(400, 400)
.into(imageView);
The layout of the ViewHolder looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp">
<com.stfalcon.chatkit.utils.ShapeImageView
android:id="#id/messageUserAvatar"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_marginRight="8dp" />
<com.google.android.flexbox.FlexboxLayout
android:id="#id/bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/message_incoming_bubble_margin_right"
android:layout_toRightOf="#id/messageUserAvatar"
android:orientation="vertical"
app:alignContent="stretch"
app:alignItems="stretch"
app:flexWrap="wrap"
app:justifyContent="flex_end">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerInside" />
<TextView
android:id="#id/messageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#id/messageTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/messageText"
android:layout_marginLeft="8dp"
app:layout_alignSelf="center" />
</com.google.android.flexbox.FlexboxLayout>
</RelativeLayout>
Don't use adjustViewBounds for dynamically-loaded images, because it will trigger a full relayout after each image completes loading. Set a fixed size to your ImageViews instead, and use centerInside scale mode if you want to preserve aspect ratio, or centerCrop if you prefer to crop the image. This tip applies to any image library.
Furthermore, when the ImageView has a fixed size, Picasso or Glide allows you to resize the image to it automatically, which reduces memory usage and improves performance. For Picasso, use:
Picasso.with(itemView.getContext())
.load(((ChatMediaMessage) message).getUrl())
.fit()
.centerCrop()
.into(imageView);
You can also use centerInside() instead of centerCrop().
Glide has a similar API. It will provide slightly better performance than Picasso because it caches the resized images to disk as well, while Picasso only caches the full-size images to disk and will have to resize them each time they are loaded back from disk.
Have you try Glide? Give it a try. Just add the library on dependencies and only replace the Picasso word.
Is your image size bigger, like 2Mb above? Try resizing or optimizing the image before upload it anywhere.
Is your image JPG? You can use RIOT http://luci.criosweb.ro/riot/
to optimize your JPG/PNG.
Or it's advisable to convert your JPG images into WebP. Android Studio has the built in converter to try. WebP loads faster and smaller in size.
I am using Universal Image Loader in my app.
It is the best libary to download images, but I have one problem.
I download images that they are smaller than width of device.
I need the image have width = device width and scaled height.
Any idea how I can do this?
you can use imageview like this
<ImageView
android:id="#+id/imageView_clock"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
android:src="#drawable/ic_icon" />
and its depends on your image size from web
android:scaleType="centerCrop" // use this
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" />
I know there are a lot of questions around this subject, but I cannot find something which perfectly fits my problem so I open this question in order to, at least, gather here EVERY aspect of the problem.
Let's start! I have my imageview and this imageview has to have fixed dimensions:
<ImageView
android:id="#+id/my_image"
android:layout_width="250dp"
android:layout_height="170dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:contentDescription="#string/cell_item_image"
android:scaleType="fitXY" />
my bitmap comes from an url and is in the .jpg format, so my code is:
bitmap = BitmapFactory.decodeStream(conn.getInputStream());
its dimensions are 435x312 pixels, bigger than my screen density so scaling it should not lead to a loss of quality...how come I get an obfuscated image?
Is there a way to rescale bitmap and preseve quality?
Maybe it's that fitXY in the scale type? But what's the difference between scaling in code or in layout? I see many does that in code but, trying, I get exactly the same result
EDIT: read over the .jpg part
Try:
Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap source = BitmapFactory.decodeResource(a.getResources(), path, options);
If this doesn't work, try displaying the image without scaling (this way you can see what causes the blur, the loading of the image or the displaying/scaling)