使解析的图像填充imageview

时间:2014-04-03 00:58:33

标签: java android image-loading

你好我所有的解析图像都是500x500,我希望它适合一个设置为包装内容的图像视图。目前imege不适合视图并且不断重叠其他图像。无论如何我可以解析这个图像并让它填满我的图像视图(好像我将图像设置为背景而不是src)谢谢!

    <?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="wrap_content"
    android:background="@drawable/list_selector1"
    android:orientation="horizontal"
    >

    <ImageView
        android:id="@+id/list_image"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:src="@drawable/posty"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />


    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/listback1"

        android:id="@+id/imageView2"
        android:layout_alignBottom="@+id/list_image"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
    <!--Author-->

    <TextView
        android:id="@+id/duration"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17dip"
        android:text="The state of Canada's National Post has lead peopele to belive that the nations staple service my be comming to a grinding halt"
        android:paddingLeft="5dp"
        android:textAlignment="textStart"
        android:typeface="serif"
        android:gravity="center|center_horizontal"
        android:paddingRight="5dp"
        android:layout_alignWithParentIfMissing="false"
        android:paddingBottom="3.3dp"
        android:paddingTop="3.3dp"
        android:layout_below="@+id/id"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#e74c3c"
        android:text="External"
        android:textSize="10dip"
        android:textStyle="bold"
        android:layout_weight="18.75"
        android:paddingBottom="5dp"
        android:layout_alignTop="@+id/imageView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/author"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#e74c3c"
        android:textSize="15dip"
        android:text="Madeleine Wieler "
        android:textStyle="italic"
        android:layout_alignBottom="@+id/imageView2"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

图像加载器类

 public class ImageLoader {

MemoryCache memoryCache=new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews=Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;

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

final int stub_id = R.drawable.no_image;
public void DisplayImage(String url, ImageView imageView)
{
    imageViews.put(imageView, url);
    Bitmap bitmap=memoryCache.get(url);
    if(bitmap!=null) {
        BitmapDrawable bd = new BitmapDrawable(bitmap);
        imageView.setBackgroundDrawable(bd);
    }
    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);

    //from SD cache
    Bitmap b = decodeFile(f);
    if(b!=null)
        return b;

    //from web
    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();
        bitmap = decodeFile(f);
        return bitmap;
    } catch (Exception ex){
        ex.printStackTrace();
        return null;
    }
}

    //decodes image and scales it to reduce memory consumption
//decodes image and scales it to reduce memory consumption
    private Bitmap decodeFile(File f){
        try {
            return BitmapFactory.decodeStream(new FileInputStream(f));
        } catch (FileNotFoundException e) {}
        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() {
        if(imageViewReused(photoToLoad))
            return;
        Bitmap bmp=getBitmap(photoToLoad.url);
        memoryCache.put(photoToLoad.url, bmp);
        if(imageViewReused(photoToLoad))
            return;
        BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad);
        Activity a=(Activity)photoToLoad.imageView.getContext();
        a.runOnUiThread(bd);
    }
}

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();
}
}

1 个答案:

答案 0 :(得分:0)

您的第二张图片视图应使用layout_toLeftOf="@+id/list_image"来防止重叠,并且图片不适合您应该使用scaleType属性