Search code examples
androidimagebitmapimage-resizinguniversal-image-loader

Set large image in image view using universal image downloader


I am using universal image downloader in my app with following configuration -

 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
       .memoryCacheExtraOptions(1000, 1200)
       .threadPoolSize(4)
       .threadPriority(Thread.MIN_PRIORITY + 2)
       .denyCacheImageMultipleSizesInMemory()
       .memoryCache(new LruMemoryCache(1024*1024*5)) // You can pass your own memory cache implementation
       .discCacheFileNameGenerator(new HashCodeFileNameGenerator())
       .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
       .build();
       ImageLoader.getInstance().init(config);

Now image i want to display in list can be of any size may be image with high resolution or image with lower resolution but i want to display image with width = device width and hight= fix height say-300dp without blur or stretched image. Is this possible to create this kind of view.Instagram is using this process.see image below-

enter image description here


Solution

  • Try this:

    public class CustomImageView extends ImageView {
    int width = 300;// just setting random value
    int height = 300;// pixels
    Context context;
    
    
    public CustomImageView(Context context) {
        super(context);
        this.context = context;
        this.width = getWidthOfScreen();
    }
    
    public CustomImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        this.width = getWidthOfScreen();
    }
    
    public CustomImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
        setMeasuredDimension(width, height);//the most important line
    }
    
    public int getWidthOfScreen() {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        return display.getWidth();
    }
    }
    

    Here, width is the screen width and height is constant as you wanted.