使用ImageView创建自定义视图,Asynctask无法正常工作

时间:2011-09-10 11:47:03

标签: java android image android-asynctask

我想以编程方式将View添加到ViewFlipper:

flipper.addView(anEpisode(name, null, description), i); 

我的方法是:

public View anEpisode(String n, String i, String d){
    View v;
    LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = inflater.inflate(R.layout.episodedescription, null);

    TextView nameOfEpisode = (TextView) v.findViewById(R.id.episodedescriptionname);
    ImageView imageOfEpisode = (ImageView) v.findViewById(R.id.episodedescriptionimage);
    TextView descriptionOfEpisode = (TextView) v.findViewById(R.id.episodedescriptiondescription);

    nameOfEpisode.setText(n);
    descriptionOfEpisode.setText(d);
    createUrlImage(imageOfEpisode, i);

    return v;
}

,createUrlImage为:

private class CreateImage extends AsyncTask<String, Void, Drawable> {
    ImageView image;
    public CreateImage(ImageView img) {
        image = img;
    }
    protected void onPreExecute() {
    }

    protected Drawable doInBackground(String... urls) {
        InputStream is;
        Drawable d = null ;
        try {
            is = (InputStream)new URL(urls[0]).getContent();
            d = Drawable.createFromStream(is, "Image");
            return d;
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return d;
    }
    protected void onPostExecute(Drawable d) {
        image.setBackgroundDrawable(d);
    }
}
public void createUrlImage(ImageView img, String url){
    new CreateImage(img).execute(url);
}

事情是:它根本不显示图像所以我不知道该怎么做。

1 个答案:

答案 0 :(得分:0)

我认为问题在于可绘制边界。在onPostExecute()方法中设置边界。

 protected void onPostExecute(Drawable d) {
    d.setBounds (0, 0, getIntrinsicWidth(), getIntrinsicHeight());
    image.setBackgroundDrawable(d);
}