ImageView loading high resolution image as very poor quality - android

I am using a ListFragment to display a ListView of Lazy Loaded ImageView objects using the Android Universal Image Loader. The correct data is coming from my datasource and I have the URL to my fairly high resolution images (about 1000px wide, 150kb). I am using these to completely fill each row that is 110dp high.
The problem I am having is the images are loading at a very poor resolution:
The problem isn't the source image as it is high enough quality:
http://www.puc.edu/__data/assets/image/0006/127581/IMG_5155.jpg
I am centering the image and then cropping it:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="110dp">
<TextView
android:id="#+id/galleryTitle"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_marginLeft="95dp"
android:layout_marginRight="5dp"
android:gravity="center_vertical"
android:hint="16dp"
android:lineSpacingExtra="0dp"
android:lines="3"
android:text="TextView"
android:textSize="16sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/galleryImageView"
android:layout_width="fill_parent"
android:layout_height="110dp"
android:scaleType="centerCrop"
android:src="#drawable/default_background" />
</RelativeLayout>
Displaying the image in the row:
public View getView(int position, View convertView, ViewGroup parent) {
View itemView;
TextView title;
// Get the news item object
PUCObjects.PUCPhotoGalleryItem item = items.get(position);
// Inflater Service
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.photos_gallery_row, parent, false);
title = (TextView) itemView.findViewById(R.id.galleryTitle);
ImageView imageView = (ImageView) itemView.findViewById(R.id.galleryImageView);
// Set the Image
imageLoader.DisplayImage(item.imageUrlExtraLarge, imageView);
// Set the text
title.setText(item.title);
return itemView;
}
Any ideas why it is displaying such poor resolution images?

The pictures appear to be down scaled. Looking at that library the default functionality is power of 2 image sampling. I would reckon this is source of your issue.
ImageScaleType
DisplayOptions
For instance:
DisplayImageOptions opts = new DisplayImageOptions.Builder().imageScaleType(ImageScaleType.NONE).build();
imageLoader.DisplayImage(item.imageUrlExtraLarge, imageView, opts);

Related

Cannot maintain image aspect ratio/quality in android application

As the title suggest, I am having a lot of issues maintaining the quality and aspect ratio of my images on my android application.
Basically I have an image view and I am trying to get images to fit the image view and maintain it's image quality. I cant use fitxy because that will stretch the image to fill the entire imageview, but I only desire the image to automatically scale it's self and show as crisp as possible like your phone gallery.
In my code, I use fitCenter as the scale type and the size of the image seems alright, however the image is terrible distorted. This is the code.
Ive also included a snapshot of the android and iphone version to show how the exact same image is showing differently on the same image.
<?xml version="1.0" encoding="utf-8"?>
<!-- This controls layout for the slider in Gallery -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="#+id/pagerimage"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center_horizontal"
/>
</RelativeLayout>
</LinearLayout>
iPhone
Android
public void handleGalleryImages() {
try {
for (int j = 0; j < imagesNaturalItemlist.size(); j++) {
Log.e("ImagePATH>>>>>", imagesNaturalItemlist.get(j).getImage());
String imageid = imagesNaturalItemlist.get(j).getUniqueId();
downgalleryimage = new DownloadImage(imagesNaturalItemlist.get(j).getImage(), imageid);
downgallerylist.add(downgalleryimage);
beachimages.add(imagesNaturalItemlist.get(j).getImage());
}
} catch (NullPointerException e) {
e.printStackTrace();
}
adapter
public Object instantiateItem(ViewGroup container, int position) {
View itemView = LayoutInflater.from(context).inflate(R.layout.sliding_pager_row, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.pagerimage);
if (images.get(position) != null && !images.get(position).equals("")) {
//Picasso.with(context).load(images.get(position)).into(imageView);
Glide.with(context)
.load(images.get(position))
.apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL).override(600, 250))
.into(imageView);
}
container.addView(itemView);
return itemView;
}

Android GridView not working properly on smaller and bigger screen devices

I have a GridView in my Android app, which is having images. I have 10 rows and 2 columns in the GridView. With that, I have same size images in every grid i.e. 20 images.
The grid code is,
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:columnWidth="140dp"
android:drawSelectorOnTop="true"
android:gravity="center"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"
android:focusable="true"
android:clickable="true"
/>
Meanwhile in the java code, I have used an Adapter. Here's the code, which sets the individual grid size and images, with the scaleType,
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(340, 400));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setPadding(10, 10, 10, 10);
It's working fine on device screen size 5'0, but on other devices such as 5'7, 4'5, it's not working in the same way. The grids (and their individual images) are somewhat hiding/ cropping. Even under 5'0, it is showing some extra space, which looks poor.
How GridView works correctly on different screen sizes.
Here's a sample, how is it looking like in 5'7,
Here's a sample, how is it looking like in 5'0.
This is how I want it to be visible on every screen size.
Here's a sample, how is it looking like in 4'0. Here the images are getting overlapped.
You can use item view for your GridView same as ListView in your adapter as below:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_grid, null);
holder = new ViewHolder();
holder.mImage = (ImageView) convertView.findViewById(R.id.img);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mImage.setImageResource(imageIDs[position]);
return convertView;
}
public class ViewHolder {
private ImageView mImage;
}
I have created item_grid as below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/grid_image"
android:layout_width="match_parent"
android:layout_height="130dp" />
</LinearLayout>
Suggestion: You can use RecyclerView with GridLayoutManager with 2 columns
This code works like a charm for me in the Adapter:
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
//Calculation of ImageView Size - density independent.
//maybe you should do this calculation not exactly in this method but put is somewhere else.
Resources r = Resources.getSystem();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 60, r.getDisplayMetrics());
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams((int)px, (int)px));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//imageView.setPadding(8, 8, 8, 8);
imageView.setBackgroundColor(Color.BLUE);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
Source:
https://stackoverflow.com/a/11485625/7639113
After adding the code above, I also create a different layout for bigger screen, something like this:
normal_layout.xml
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:numColumns="2"
android:verticalSpacing="5dp"
/>
bigscreen_layout.xml
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:numColumns="4"
android:verticalSpacing="5dp"
/>
Note that I set numColumns differently for those layouts above.
Here is how I created layout for different screen sizes:
Create new Android resource directory in res
Change resource type to layout, select smallest screen width for the available qualifiers, then click ">>"
Fill in 600 in the smallest screen width, Android Studio will automatically generate layout-sw600dp directory. Click OK.
Now all you have to do is place normal_layout.xml in the layout directory, and place bigscreen_layout.xml in the layout-sw600dp directory.
You can look at the different screen size detail in the official Android guide Supporting Multiple Screens:

Android Mystery: Text Disappearing in random TextView

I have a problem in my application Life Gallery. In this application, I have a fragment that displays the user's media directory in a gridView. Here is a screenshot :
As you can see, each directory is represented with an ImageView and a TextView, this latter containing the name of the directory. If you check the third line, you will see that the text of two TextView are empty...This is my bug. The elements of my gridview that present such a bug varies if I scroll or if rotate my phone...
Here is my layout for an element of the gridView :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_global"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="1dp" >
<ImageView
android:id="#+id/directory_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/content_description_album_imageview" />
<TextView
android:id="#+id/directory_text"
style="#style/Act_MyOwnLife_TextView_large"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/directory_image"
android:background="#color/black_transparent"
android:gravity="right"
android:paddingRight="4dp"
android:singleLine="true"
/>
</RelativeLayout>
And here is the code for my Adapter, focusing on the function getView:
#Override
public View getView(int position, View view, ViewGroup parent) {
ViewHolder viewHolder = null;
if (view == null) {
LayoutInflater inflater = this.activity.getLayoutInflater();
view = inflater.inflate(R.layout.fragment_directory_gridview, parent, false);
viewHolder = new ViewHolder();
viewHolder.relativeLayout = (RelativeLayout)view.findViewById(R.id.layout_global);
viewHolder.textViewDirectory = (TextView) view.findViewById(R.id.directory_text);
viewHolder.imageViewDirectory = (ImageView) view.findViewById(R.id.directory_image);
view.setTag(R.id.viewHolder, viewHolder);
}else{
viewHolder = (ViewHolder) view.getTag(R.id.viewHolder);
}
viewHolder.relativeLayout.setLayoutParams(this.viewFragmentDirectory.layoutParams);
viewHolder.imageViewDirectory.setTag(position);
viewHolder.imageViewDirectory.setImageBitmap(null);
viewHolder.position = position;
ModelDirectoryAndMedia directoryAndImage =this.directoriesAndMedias.get(position);
viewHolder.textViewDirectory.setText(directoryAndImage.directoryView);
ImageView imageView = viewHolder.imageViewDirectory;
// creation/retrieve the thumbnail with a thread
job = new Job(imageView,
directoryAndImage.media, Options.OPTIONS_THUMBNAIL,
position);
this.bitmapCreate.addJobThumbnail(job);
return view;
}
What I did to find the bug :
I check that my String was not empty when I call "viewHolder.textViewDirectory.setText(s);"
I try to check with hierarchyViewer, but I can not browse an element if the TextView is empty...I do not know why...
I remove the transparency of the background, remove the background property of the textView, I set "ImageView.setImageBitmap(null);" to check that it was not due to the ImageView...
But without success...the bug was still here...
After more test, I found the reason of the bug: it is due to this line of code in the getView method :
viewHolder.relativeLayout.setLayoutParams(this.viewFragmentDirectory.layoutPara‌​ms);
I use this line to display the empty ImageView to the correct size. So that when the thumbnail is ready, the insertion in the ImageView will not change its size ( the settings of my LayoutParams is done with the size of my thumbnail )
Any idea ? any comments on my code ?
Thanks a lot !
Ok...The bug was due to the call to :
viewHolder.relativeLayout.setLayoutParams(this.viewFragmentDirectory.layoutParams);
But I do not understand why...anyway, I did this to ensure that my ImageView had the correct initial size to display the thumbnail, loaded by a thread.
So I found an other solution to set a minimum size to the ImageView even if they are empty :
viewHolder.imageView.setMinimumHeight(this.viewControlerAlbum.sizeThumbnail);
viewHolder.imageView.setMinimumWidth(this.viewControlerAlbum.sizeThumbnail);
My layout.xml file is a bit updated :
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/content_description_album_imageview"
android:scaleType="centerCrop"
android:adjustViewBounds="true"/>
I added the adjustViewBounds attribute.

increase images sizes loads in carousel view android

I created app to display images from internet in carousel view. I loaded images into carousel view using imageview. just like following way.
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.showsmain, null);
ImageView image=(ImageView)vi.findViewById(R.id.imageView1);
// RelativeLayout rlayout=(RelativeLayout)vi.findViewById(R.id.rlayouts);
image.setImageResource(R.drawable.black);
orgWidth = image.getDrawable().getIntrinsicWidth();
orgHeight = image.getDrawable().getIntrinsicHeight();
imageLoader.DisplayImage(data[position], image);
imageLoader.getDimension(widthScreen, heightScreen);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
widthScreen, heightScreen/3);
params.width=widthScreen;
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.FIT_XY);
//rlayout.addView(image);
return vi;
}
and my carousel layout xml file is,
<com.touchmenotapps.carousel.simple.HorizontalCarouselLayout
android:id="#+id/carousel_layout_event"
android:layout_width="600dp"
android:layout_height="500dp"
android:layout_above="#+id/relativeLayout1"
android:layout_alignTop="#+id/carousel_layout" >
</com.touchmenotapps.carousel.simple.HorizontalCarouselLayout>
then image show as following way in carousel,
![enter image description here][1]
Here i want to increase the image width & height. It means one images must load fill with in the screen
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.showimage, null);
ImageView image=(ImageView)vi.findViewById(R.id.imageView1);
//LinearLayout rlayout=(LinearLayout)vi.findViewById(R.id.layout);
image.setImageResource(R.drawable.black);
orgWidth = image.getDrawable().getIntrinsicWidth();
orgHeight = image.getDrawable().getIntrinsicHeight();
imageLoader.DisplayImage(data[position], image);
imageLoader.getDimension(widthScreen, heightScreen);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
widthScreen,heightScreen/3);
params.width=widthScreen/2;
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.FIT_XY);
//rlayout.addView(image);
return vi;
}
this way I add the imageview into carousel view.
how to do that. I asked question similer this one. But i didn't get answer. pls help me someone. pls.....
my imageview xml layout file..
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/rlayouts">
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:scaleType="fitXY"
android:layout_alignParentTop="true" />
</RelativeLayout>
I think you should use a Horizontal oriented Linear layout inside a Horizontal ScrollView then you should add ImageViews with FillParent Height and Width in this Linear layout.
It would be great if you elaborate your question a bit more.

Android, custom views don't fill the screen in a Gallery

I want to display a custom view, composed of a TextView above an ImageView in a vertical LinearLayout, one at a time in a Gallery.
The problem is that my custom view does not fill the screen. I can see a part of the other views on the sides and I don't want this issue.
Here is the xml of my custom view : gallery_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gallery_item_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/gallery_item_cardname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20dp"
android:textStyle="bold"
android:text="#string/contrebandiers_lvl1"
android:textColor="#android:color/darker_gray"
/>
<ImageView
android:id="#+id/gallery_item_cardimg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:contentDescription="#string/app_name"
android:src="#drawable/contrebandiers_lvl1"
/>
</LinearLayout>
Here is the code of the method getVew of my adapter : GTRoundDeckAdapter
public View getView(int position, View convertView, ViewGroup parent)
{
GalleryItem galleryItem;
if(convertView == null)
{
galleryItem = new GalleryItem();
convertView = mInflater.inflate(R.layout.gallery_item, null);
galleryItem.cardName = (TextView) convertView.findViewById(R.id.gallery_item_cardname);
galleryItem.cardImg = (ImageView) convertView.findViewById(R.id.gallery_item_cardimg);
convertView.setTag(galleryItem);
}
else
{
galleryItem = (GalleryItem) convertView.getTag();
}
GTCard gtCard = (GTCard) mRoundDeck.get(position);
galleryItem.cardName.setText(gtCard.getNameId());
galleryItem.cardImg.setImageResource(gtCard.getImgId());
return convertView;
}
I thank you for your help.
Samuel
I would recommend that you use the ViewPager instead of Gallery. In my personal experience, I have found Gallery to slightly buggy. If your requirement is to show only one View at a time, then ViewPager (which lets you swipe left and right to go one by one) should suit your needs.
You will need to include the Android Support package to use ViewPager.
Link to support package: http://developer.android.com/sdk/compatibility-library.html
Link on how to use ViewPager: http://blog.stylingandroid.com/archives/537
you should use both the width of textview and imageview fillparent.then it will be fill the screen of the gallery...

Categories

Resources