扩展下载图像的大小以适合ImageView的 [英] scale the size of downloaded image to fit the imageview

查看:224
本文介绍了扩展下载图像的大小以适合ImageView的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次与一个ListView工作,这里是我的问题:
我有一个ImageLoader的类,下载,德codeS和从互联网上(从parse.com)缩放图像。整个过程运行正常,但是当图像被装上它不适合父宽度ImageView的...我想有类似Instagram的的显示屏,无论使用那填满整个屏幕的正方形图像的东西设备。

这是我反倒: http://i.imgur.com/OMDyG8I.jpg
这就是我想要的东西:
http://i.imgur.com/nGHADzK.jpg

我使用了一个教程中的Imageloader.java,我真的不understandd它到底是如何工作。所以,如果你们能向我解释如何它这样我就可以适应它会对我的列表视图方形全屏图像。在此先感谢:)

这是我的ImageLoader.java(没有我的特殊布局,现在......只能用火柴父母的身高和witdh一个ImageView的):

 包com.hichamridouane.smartshop;
进口的java.io.File;
进口java.io.FileInputStream中;
进口java.io.FileNotFoundException;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.OutputStream中;
importenter code这里java.net.HttpURLConnection中;
进口的java.net.URL;
进口java.util.Collections中;
进口的java.util.Map;
进口java.util.WeakHashMap中;
进口java.util.concurrent.ExecutorService中;
进口java.util.concurrent.Executors;进口android.os.Handler;
进口android.content.Context;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.widget.ImageView;公共类ImageLoader的{
     的MemoryCache的MemoryCache =新的MemoryCache();
    FileCache fileCache;
私人地图< ImageView的,字符串> imageViews =集合
        .synchronizedMap(新的WeakHashMap< ImageView的,字符串>());
ExecutorService的ExecutorService的;
//处理程序,以显示在UI线程图片
处理程序处理程序=新的处理程序();公共ImageLoader的(上下文的背景下){
    fileCache =新FileCache(背景);
    ExecutorService的= Executors.newFixedThreadPool(5);
}最终诠释stub_id = R.drawable.temp_img;公共无效DisplayImage(字符串URL,ImageView的ImageView的){
    imageViews.put(ImageView的,URL);
    位图位图= memoryCache.get(URL);
    如果(位图!= NULL)
        imageView.setImageBitmap(位图);
    其他{
        queuePhoto(URL,ImageView的);
        imageView.setImageResource(stub_id);
    }
}私人无效queuePhoto(字符串URL,ImageView的ImageView的){
    PhotoToLoad P =新PhotoToLoad(URL,ImageView的);
    executorService.submit(新PhotosLoader(P));
}私人位图getBitmap(字符串URL){
    文件F = fileCache.getFile(URL);    位图B =去codeFILE(F);
    如果(B!= NULL)
        返回b;    从网上下载//图片
    尝试{
        位图位图= NULL;
        URL =图片网址新的URL(网址);
        HttpURLConnection的康恩=(HttpURLConnection类)图片网址
                .openConnection();
        conn.setConnectTimeout(30000);
        conn.setReadTimeout(30000);
        conn.setInstanceFollowRedirects(真);
        InputStream为= conn.getInputStream();
        OutputStream的OS =新的FileOutputStream(F);
        Utils.CopyStream(是,OS);
        os.close();
        conn.disconnect();
        位=去codeFILE(F);
        返回位图;
    }赶上(Throwable的前){
        ex.printStackTrace();
        如果(前的instanceof的OutOfMemoryError)
            memoryCache.clear();
        返回null;
    }
}//德$ C $连拍影像的鳞它来减少内存消耗
私人位图德codeFILE(文件f){
    尝试{
        //德code图像尺寸
        BitmapFactory.Options O =新BitmapFactory.Options();
        o.inJustDe codeBounds = TRUE;
        的FileInputStream流1 =新的FileInputStream(F);
        BitmapFactory.de codeStream(STREAM1,空,O);
        stream1.close();        //找到正确的比例值。它应该是2的幂。
        最终诠释REQUIRED_SIZE = 600;
        INT width_tmp = o.outWidth,height_tmp = o.outHeight;
        int标= 1;
        而(真){
            如果(width_tmp / 2'; REQUIRED_SIZE
                    || height_tmp / 2'; REQUIRED_SIZE)
                打破;
            width_tmp / = 2;
            height_tmp / = 2;
            规模* = 2;
        }        //德code。与inSampleSize
        BitmapFactory.Options O2 =新BitmapFactory.Options();
        o2.inSampleSize =规模;
        流2的FileInputStream =新的FileInputStream(F);
        位图位图= BitmapFactory.de codeStream(流2,空,O2);
        stream2.close();
        返回位图;
    }赶上(FileNotFoundException异常五){
    }赶上(IOException异常五){
        e.printStackTrace();
    }
    返回null;
}//任务队列
私有类PhotoToLoad {
    公共字符串的URL;
    公共ImageView的ImageView的;    公共PhotoToLoad(字符串U,ImageView的我){
        URL = U;
        ImageView的= I;
    }
}类PhotosLoader实现Runnable {
    PhotoToLoad photoToLoad;    PhotosLoader(PhotoToLoad photoToLoad){
        this.photoToLoad = photoToLoad;
    }    @覆盖
    公共无效的run(){
        尝试{
            如果(imageViewReused(photoToLoad))
                返回;
            BMP位图= getBitmap(photoToLoad.url);
            memoryCache.put(photoToLoad.url,BMP);
            如果(imageViewReused(photoToLoad))
                返回;
            BitmapDisplayer BD =新BitmapDisplayer(BMP,photoToLoad);
            handler.post(BD);
        }赶上(的Throwable日){
            th.printStackTrace();
        }
    }
}布尔imageViewReused(PhotoToLoad photoToLoad){
    字符串标记= imageViews.get(photoToLoad.imageView);
    如果(标记== NULL ||!tag.equals(photoToLoad.url))
        返回true;
    返回false;
}//用在UI线程来显示位图
类BitmapDisplayer实现Runnable {
    位图位图;
    PhotoToLoad photoToLoad;    公共BitmapDisplayer(位图B,PhotoToLoad P){
        位= B;
        photoToLoad = P;
    }    公共无效的run(){
        如果(imageViewReused(photoToLoad))
            返回;
        如果(位图!= NULL)
            photoToLoad.imageView.setImageBitmap(位图);
        其他
            photoToLoad.imageView.setImageResource(stub_id);
    }
}公共无效clearCache(){
    memoryCache.clear();
    fileCache.clear();
}
}


解决方案

首先,你去codeFILE()方法,它应该去code从缓存中的图像文件,并将其调整到较小的位图不会调整到一见方大小。所以,当你加载德codeD位图到您的ImageView,没有在你的布局,或在您的ImageView任何指定的选项,你会得到你的形象与原来的尊重比。

我的建议这里是:


  1. 修复您的宽度和布局你的ImageView的高度。 (我不知道它的工作原理,因为设备的屏幕尺寸之间的差别)


  2. 我使用这个自定义的ImageView,以显示每平方ImageView的:


自定义广场的ImageView

和这里是在XML布局使用它的一个例子:

 < your.package.to.ENESquareImageView
    机器人:ID =@ + ID / mini_cover
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:contentDescription =@空
    机器人:scaleType =centerCrop/>

希望这有助于。

This is my first time working with a ListView, and here is my problem : I have an Imageloader class that downloads, decodes and scales images from the internet (from parse.com). The whole process works fine, but when the image is loaded on the imageview it doesn't fit the parent width...I'm trying to have something similar to instagram's display with a square image that fills the entire screen regardless of the used device.

this is what I have actually : http://i.imgur.com/OMDyG8I.jpg this is what I want : http://i.imgur.com/nGHADzK.jpg

I used the Imageloader.java that I found on a tutorial and I don't really understandd how it really works. So if you guys could explain to me how it does so i can adapt it to have a square full screen image on my listview. thanks in advance :)

this is my ImageLoader.java (Nothing special on my layout for now...only an imageview with match-parent height and witdh):

package com.hichamridouane.smartshop;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
importenter code here java.net.HttpURLConnection;
import java.net.URL;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.os.Handler;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;

public class ImageLoader {
     MemoryCache memoryCache = new MemoryCache();
    FileCache fileCache;
private Map<ImageView, String> imageViews = Collections
        .synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;
// Handler to display images in UI thread
Handler handler = new Handler();

public ImageLoader(Context context) {
    fileCache = new FileCache(context);
    executorService = Executors.newFixedThreadPool(5);
}

final int stub_id = R.drawable.temp_img;

public void DisplayImage(String url, ImageView imageView) {
    imageViews.put(imageView, url);
    Bitmap bitmap = memoryCache.get(url);
    if (bitmap != null)
        imageView.setImageBitmap(bitmap);
    else {
        queuePhoto(url, imageView);
        imageView.setImageResource(stub_id);
    }
}

private void queuePhoto(String url, ImageView imageView) {
    PhotoToLoad p = new PhotoToLoad(url, imageView);
    executorService.submit(new PhotosLoader(p));
}

private Bitmap getBitmap(String url) {
    File f = fileCache.getFile(url);

    Bitmap b = decodeFile(f);
    if (b != null)
        return b;

    // Download Images from the Internet
    try {
        Bitmap bitmap = null;
        URL imageUrl = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) imageUrl
                .openConnection();
        conn.setConnectTimeout(30000);
        conn.setReadTimeout(30000);
        conn.setInstanceFollowRedirects(true);
        InputStream is = conn.getInputStream();
        OutputStream os = new FileOutputStream(f);
        Utils.CopyStream(is, os);
        os.close();
        conn.disconnect();
        bitmap = decodeFile(f);
        return bitmap;
    } catch (Throwable ex) {
        ex.printStackTrace();
        if (ex instanceof OutOfMemoryError)
            memoryCache.clear();
        return null;
    }
}

// Decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        FileInputStream stream1 = new FileInputStream(f);
        BitmapFactory.decodeStream(stream1, null, o);
        stream1.close();

        // Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE = 600;
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE
                    || height_tmp / 2 < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        FileInputStream stream2 = new FileInputStream(f);
        Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o2);
        stream2.close();
        return bitmap;
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

// Task for the queue
private class PhotoToLoad {
    public String url;
    public ImageView imageView;

    public PhotoToLoad(String u, ImageView i) {
        url = u;
        imageView = i;
    }
}

class PhotosLoader implements Runnable {
    PhotoToLoad photoToLoad;

    PhotosLoader(PhotoToLoad photoToLoad) {
        this.photoToLoad = photoToLoad;
    }

    @Override
    public void run() {
        try {
            if (imageViewReused(photoToLoad))
                return;
            Bitmap bmp = getBitmap(photoToLoad.url);
            memoryCache.put(photoToLoad.url, bmp);
            if (imageViewReused(photoToLoad))
                return;
            BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
            handler.post(bd);
        } catch (Throwable th) {
            th.printStackTrace();
        }
    }
}

boolean imageViewReused(PhotoToLoad photoToLoad) {
    String tag = imageViews.get(photoToLoad.imageView);
    if (tag == null || !tag.equals(photoToLoad.url))
        return true;
    return false;
}

// Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable {
    Bitmap bitmap;
    PhotoToLoad photoToLoad;

    public BitmapDisplayer(Bitmap b, PhotoToLoad p) {
        bitmap = b;
        photoToLoad = p;
    }

    public void run() {
        if (imageViewReused(photoToLoad))
            return;
        if (bitmap != null)
            photoToLoad.imageView.setImageBitmap(bitmap);
        else
            photoToLoad.imageView.setImageResource(stub_id);
    }
}

public void clearCache() {
    memoryCache.clear();
    fileCache.clear();
}
}

解决方案

First of all, your decodeFile() method, which supposed to decode the image file from cache and resize it to smaller bitmap, will not resize it to a SQUARE SIZE. so, when you load your decoded bitmap into your ImageView, without any specified option in your layout or in your ImageView, you will get your image with original respect ratio.

What I suggest here is:

  1. Fix your width and height of your ImageView in your layout. (I'm not sure it works because of the difference between devices' screen size)

  2. I'm using this custom ImageView to show every ImageView in square:

Custom Square ImageView

and here is an example of using it in an xml layout:

<your.package.to.ENESquareImageView
    android:id="@+id/mini_cover"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:contentDescription="@null"
    android:scaleType="centerCrop" />

Hope this helps.

这篇关于扩展下载图像的大小以适合ImageView的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆