java - 如何将解析的图像设置为背景而不是 src

标签 java android xml list

我有一个自定义的 ListView ,它解析列表的图像,但它将它们作为 src 读取

 <ImageView
    android:id="@+id/list_image"
    android:layout_height="420dp"
    android:layout_width="fill_parent"
    android:src="@drawable/posty"
    android:clickable="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"/>

无论如何都可以设置它,以便 android:src="@drawable/posty"可以是 android:background="@drawable/posty"但带有解析的图像,谢谢。

适配器:

public class LazyAdapter extends BaseAdapter {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;

public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.list_row, null);

    TextView title = (TextView)vi.findViewById(R.id.title); // title
    TextView artist = (TextView)vi.findViewById(R.id.author); //  name
    TextView duration = (TextView)vi.findViewById(R.id.duration); // description
    TextView id = (TextView)vi.findViewById(R.id.id); // Section
    ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image

    HashMap<String, String> song = new HashMap<String, String>();
    song = data.get(position);

    // Setting all values in listview
    id.setText(song.get(CustomizedListView.KEY_ID));
    artist.setText(song.get(CustomizedListView.KEY_ARTIST));
    duration.setText(song.get(CustomizedListView.KEY_DURATION));
    imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
    return vi;
}

}

和图像加载器:

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

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

最佳答案

您可以使用setBackgroundDrawable。从您的位图创建一个BitmapDrawable:

BitmapDrawable bd = new BitmapDrawable(yourBitamp);
imageView.setBackgroundDrawable(bd);

编辑:

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

关于java - 如何将解析的图像设置为背景而不是 src,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22596297/

相关文章:

java - 代码生成 - XML 到 Java

java - hibernate 映射: using same object multiple times

java - JList(与数组列表链接)添加元素导致内存不足或消失

java - 更新到 Android Studio 3.1 后,我遇到了这个错误 : Could not find org. jetbrains.kotlin :kotlin-stdlib-jre8:1. 2.0

java - Android:具有现有布局的自定义 View 的多个 View 子项

Python:使用 ElementTree 更新 XML 文件,同时尽可能保留布局

java - 如何从 Storm 拓扑主类调用关闭钩子(Hook)?

java - JPA EclipseLink 自定义实体/对象作为 IN 参数

android - 如何将 WiFi 级别(即 -45 、 -88 )转换为百分比?

java - ArrayList 丢失项目