Android Textview 延时加载图片

来源:互联网 发布:冬天保湿面霜推荐 知乎 编辑:程序博客网 时间:2024/05/16 01:20
public class URLImageParser implements Html.ImageGetter {
Context c;
View container;
/***
* Construct the URLImageParser which will execute AsyncTask and refresh the container
* @param t
* @param c
*/
public URLImageParser(View t, Context c) {
   this.c = c;
   this.container = t;
}
public Drawable getDrawable(String source) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.add_label_1).copy(Bitmap.Config.ARGB_8888, true);
AnimationDrawable urlDrawable =new AnimationDrawable(); 
   
   urlDrawable.setBounds(0, 0,400,400);
   ImageGetterAsyncTask asyncTask = 
       new ImageGetterAsyncTask(urlDrawable);
   asyncTask.execute(source);

   return urlDrawable;
}
public class ImageGetterAsyncTask extends AsyncTask<String, Void, Bitmap>  {
AnimationDrawable urlDrawable;

   public ImageGetterAsyncTask(AnimationDrawable d) {
       this.urlDrawable = d;
   }
   @Override
   protected Bitmap doInBackground(String... params) {
       String source = params[0];
       return fetchDrawable(source);
   }
   @Override
   protected void onPostExecute(Bitmap bitmap) {
    Drawable drawable = new BitmapDrawable(bitmap);
       urlDrawable.addFrame(drawable, 1);
       urlDrawable.setOneShot(true);
       urlDrawable.setBounds(0, 0, 0+bitmap.getWidth(), 0+bitmap.getHeight()); 
       urlDrawable.start();
       URLImageParser.this.container.invalidate();
   }


   /***
    * Get the Drawable from URL
    * @param urlString
    * @return
    */
   public Bitmap fetchDrawable(String urlString) {
       try {
           URL aURL = new URL(urlString);
           final URLConnection conn = aURL.openConnection(); 
           conn.connect(); 
           final BufferedInputStream bis = new BufferedInputStream(conn.getInputStream()); 
           final Bitmap bm = BitmapFactory.decodeStream(bis);
           return bm;
       } catch (Exception e) {
        e.printStackTrace();
           return null;
       } 
   }
}

}




public View getView(final int position, View convertView, ViewGroup parent) {

TextView mtextView = initTextView();

URLImageParser uRLImageParser=new URLImageParser(mtextView, ArticleActivity.this);

mtextView.setText(Html.fromHtml(vo.getContentDetail(),uRLImageParser, null));

}

诶诶  哪个用了的和我说一声啊

转载的注意啦


原创粉丝点击