How to avoid image flickering in a listview

前端 未结 5 1105
伪装坚强ぢ
伪装坚强ぢ 2021-01-14 02:09

I have a listivew that display a bunch of images. am using Universal Image Loader to load this images from files to imageviews.

This images have dif

5条回答
  •  一向
    一向 (楼主)
    2021-01-14 02:21

    After hours of research, i was able to know the method that i can use to calculate new imageview height while maintaining image aspect ratio.

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
    
    //Returns null, sizes are in the options variable
    BitmapFactory.decodeFile("/sdcard/image.png", options);
    int width = options.outWidth;
    int height = options.outHeight;
    
    //calculating image aspect ratio
    float ratio =(float) height/(float) width;
    
    //calculating my image height since i want it to be 360px wide
    int newHeight = Math.round(ratio*360);
    
    //setting the new dimentions
     imageview.getLayoutParams().width = 360;
     imageview.getLayoutParams().height = newHeight;
    
     //i'm using universal image loader to display image
     imaheview.post(new Runnable(){
      ImageLoader.getInstance().displayImage(imageuri,imageview,displayoptions);
     });
    

提交回复
热议问题