Today, I faced a problem that the GIF images can't be displayed in a custom ListView. Should I use a third-party library to achieve it?
(I working with android studio)
Sample image: http://www.dongabank.com.vn/images/flag/AUD.gif
My code:
public View getView(int position, View v, ViewGroup parent) {
if(v == null){
v = LayoutInflater.from(context).inflate(R.layout.custom_layout, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.image = (ImageView) v.findViewById(R.id.imageViewFlag);
viewHolder.type = (TextView)v.findViewById(R.id.textViewType);
viewHolder.muatienmat = (TextView)v.findViewById(R.id.textViewMua);
viewHolder.bantienmat = (TextView)v.findViewById(R.id.textViewBan);
v.setTag(viewHolder);
}
Exchange ex = arrayExchange.get(position);
if(ex != null){
ViewHolder viewHolder = (ViewHolder)v.getTag();
Picasso.with(getContext()).load("http://www.dongabank.com.vn/images/flag/AUD.gif")
.into(viewHolder.image);
viewHolder.type.setText(ex.getType());
viewHolder.muatienmat.setText(ex.getMuatienmat());
viewHolder.bantienmat.setText(ex.getBantienmat());
}
return v;
}
You can use Glide instead of Picasso.
It's almost same as Picasso.And it supports gif.
https://github.com/bumptech/glide
Picasso.with(getContext()).load("http://www.dongabank.com.vn/images/flag/AUD.gif")
.asGif().into(viewHolder.image);
This is a native library: https://github.com/koral--/android-gif-drawable
It`s save my life a lot of time.
Related
I'm working on Imgur API and I fetched some image links. In a gridview, i am showing all pictures but sometimes api gives ".gif" and picasso cannot load this animated pictures. I want to pass if its a gif which convertview didn't load. As you can see, I'dont want to that gray areas. Do you have any idea?
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView ) {
convertView = inflater.inflate(R.layout.listview_item_image, parent, false);
}
ImageView img = (ImageView) convertView.findViewById(R.id.im);
if(datas.get(position).getLink().endsWith(".jpg") ){
Picasso.with(context)
.load(datas.get(position).getLink())
.fit()
.into(img);
}else convertView.setVisibility(View.GONE);
return convertView;
}
1: []
You must remove all objects which ends with .gif from the datas ArrayList, or you must not add them in the first place, you can give a condition while adding in ArrayList
if(data.getLink().endsWith(".jpg"))
datas.add(data)
I am building a Tinder-like Android app with this library : https://github.com/Diolor/Swipecards and Picasso for image loading. Everything works fine, except the first view of the list. The image is not displayed, although the text is correct.
Views are stored in a custom subclass of ArrayAdapter. Here is the code:
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
switch (getItemViewType(position)) {
case TYPE_MOVIE:
ViewHolderMovie holder;
if (convertView == null) {
holder = new ViewHolderMovie();
convertView = inflater.inflate(R.layout.movie_card, parent, false);
holder.movieCover = (ImageView)convertView.findViewById(R.id.movieCover);
holder.movieTitle = (TextView)convertView.findViewById(R.id.movieTitle);
holder.movieGenre = (TextView)convertView.findViewById(R.id.movieGenre);
convertView.setTag(holder);
} else {
holder = (ViewHolderMovie) convertView.getTag();
}
Movie movie = (Movie) data.get(position);
Picasso.with(convertView.getContext())
.load(movie.imagePath)
.resize(500, 500)
.into(holder.movieCover); // this doesn't work for the first view
holder.movieTitle.setText(movie.title);
holder.movieGenre.setText("Film au cinéma ("+movie.genre+")");
return convertView;
}
return null;
}
Do you have an idea please?
Check Your image URL path ,especially slash '/'.
Picasso don't show first image of ArrayAdapter when '/' has twice in your imageurl path.
But it show truly when you scroll Gridview or Listview
I am getting crash while scrolling down and up a listview from my app but the error is not quite understandable. I am attaching the bug report screenshot from Google Developer Console.
Please go through it.
Adapter getView Code:
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.adapter_item_list,
parent, false);
holder.labelName = (TextView) convertView
.findViewById(R.id.item_label);
holder.labelInfo = (TextView) convertView
.findViewById(R.id.item_info);
holder.mImgArrow = (ImageView) convertView
.findViewById(R.id.iv_arrow);
holder.mImgIcon = (RoundCornerImage) convertView
.findViewById(R.id.grid_item_image);
holder.relative_cell_view = (RelativeLayout) convertView
.findViewById(R.id.relative_cell_view);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
appSharedPrefrence = AppSharedPrefrence.getsharedprefInstance(context);
final FileBean dataBean = dataList.get(position);
holder.labelName.setText(position+" : "+dataBean.getName());
holder.labelInfo.setText(replace_comma(dataBean.getinfo()));
try{
ImageLoader.getImageLoader(context).DisplayImage(dataBean.getImageUrl(), holder.mImgIcon,R.drawable.logo);
}catch(OutOfMemoryError e){
}
return convertView; }
This is the above crash report i am getting , please let me know and suggest me some solution.
Your code fails in a native function named jpeg_start_decompress() in the library libjpeg.so, so my best guess is that you are trying to display a corrupted JPEG file or something like that.
If you have access to the images, I would suggest you to try to open them all on your computer and see if there isn't one that fails to load.
I'm using ListView with ImageView is a element of List. I have got problem when fast scrolling my List : The system seems to be recycling views until it loads the correct position's view in my ListView, resulting in duplicated images and text for a few seconds. After that, ImageView display images correctly. I'm using Universal Image Loader library to load images.
#Override
public View getView(int position, View v, ViewGroup parent) {
final PhotoViewHolder holder;
if(v == null){
LayoutInflater li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.photo_item_horizonal_layout, parent, false);
holder = new PhotoViewHolder();
holder.imgPhoto = (ImageView)v.findViewById(R.id.imgPhotoIcon);
v.setTag(holder);
}else{
holder = (PhotoViewHolder)v.getTag();
}
//item variable is a element at position of ArrayList
ImageLoader.getInstance().displayImage(item.getImageUrl(), holder.imgPhoto);
...
}
Can anyone help me?
Thanks in advanced
Do one thing Download Aquery from This link put that .jar File in your Lib folder and change your code to this:
#Override
public View getView(int position, View v, ViewGroup parent) {
final PhotoViewHolder holder;
if(v == null){
LayoutInflater li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.photo_item_horizonal_layout, parent, false);
holder = new PhotoViewHolder();
holder.imgPhoto = (ImageView)v.findViewById(R.id.imgPhotoIcon);
v.setTag(holder);
}else{
holder = (PhotoViewHolder)v.getTag();
}
//item variable is a element at position of ArrayList
AQuery aQuery=new AQuery(v)
aQuery.id(holder.imgPhoto).image(item.getImageUrl().toString());
}
Use a placeholder image while image is loading . You can do that by UIL option:
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.placeholder)
......
.build();
This should solve your problem.
Are you changing the image of the ImageView by yourself in a later part of the code?
if you do, you should call
ImageLoader.getInstance().cancelDisplayTask(holder.imgPhoto);
so that the library knows that the link between the image and the imageview is broken
Is there any faster alternative to Gallery widget in Android, because Gallery is so slow and lagging, i tryed to use view holder to reuse views, but without result. i used classic method
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.image, null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.ImageView01);
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
holder.icon.setImageDrawable(getPicture(items[position]));
return convertView;
}
This is a couple of various galleries created by other and uploaded to GitHub. You might need to checkout them to see how they are and how fast they are.