Frage

hello all I have a parsed image that's 500x500 and I want it to fit an image view that is set to wrap content. currently the imege doesnt fit the view and constantly overlaps other images. is there anyway i can parse this image and have it fill my image view (as if I set an image as background instead of src) thanks!

    <?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>

the image loader class

 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();
}
}
War es hilfreich?

Lösung

Your second imageview should use layout_toLeftOf="@+id/list_image" to prevent the overlap and about the image not fitting you should probably play around with the scaleType property

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top