【问题标题】:How to load an image url to an imageview wherein the image view design declared inside a thread?如何将图像 url 加载到图像视图中,其中图像视图设计在线程内声明?
【发布时间】:2018-05-23 11:02:48
【问题描述】:
@SuppressWarnings({ "rawtypes" })
        public void addAttendance(ArrayList<Properties> attendanceusers) {
            //tl.removeView(tr);
            tl.removeAllViews();
            //addHeaderAttendance();
            ctr=0;
            for (Iterator i = attendanceusers.iterator(); i.hasNext();) {

                Properties p = (Properties) i.next();

                property_list.add(p);
                /** Create a TableRow dynamically **/
                tr = new TableRow(this);

                picurl=p.getPic();
                profile = new ImageView(this);

                profile.setPadding(20,50,20,50);
              /*  URL newurl = new URL(picurl); 
                bmp = BitmapFactory.decodeStream(newurl.openConnection().getInputStream()); 
                profile.setImageBitmap(bmp);*/
                try {

                      Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(picurl).getContent());
                      profile.setImageBitmap(bitmap); 
                    } catch (MalformedURLException e) {
                      e.printStackTrace();
                    } catch (IOException e) {
                      e.printStackTrace();
                    }

                profile.setOnClickListener(this);

                //myButton.setPadding(5, 5, 5, 5);
                Ll = new LinearLayout(this);

                params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT);
                params.setMargins(0, 0, 0, 0);

                Ll.setPadding(0, 0, 20, 0);
                Ll.addView(profile,params);
                tr.addView((View)Ll);


                 // Add the TableRow to the TableLayout
                tl.addView(tr, new TableLayout.LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));
            }
        }

我用于填充图像的代码不起作用,因为我的主要方法正在线程上工作。你能建议一个我可以放在 a 方法上的代码吗?这篇文章不是堆栈上任何其他帖子的重复。 我在我的另一个正在工作的类上尝试了这段代码,但是如果主方法(我使用的是一个线程)它会闪烁并闪烁:

new AsyncTask<Void, Void, Void>() {                  
                    @Override
                    protected Void doInBackground(Void... params) {
                        try {
                            InputStream in = new URL(picurl).openStream();
                            bmp = BitmapFactory.decodeStream(in);
                        } catch (Exception e) {
                           // log error
                        }
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void result) {
                        if (bmp != null)
                            profile.setImageBitmap(bmp);
                    }

               }.execute();

但是,如果我把它放在我粘贴的第一个代码中。 ui 会不断闪烁,因为它是线程内部的线程。我的图像太大,我无法正确调整它的大小,你能帮我吗?我会承诺给那些可以帮助我的人打勾加分,对于那些努力的人我也会给分

【问题讨论】:

  • 你可以使用 glide 库来做这个!它更容易!和android文档推荐!

标签: android eclipse


【解决方案1】:

我认为您不需要使用线程或类似的想法。

只需将PicassoGlide 添加到您的项目中

此库支持从资源、内存或网络外部链接加载图像。这将解决闪烁的问题,并且是缓存和安全内容加载的良好解决方案。

【讨论】:

  • 它正在工作,实际上我之前问过这个问题,但我懒于 doqnloading picasso,因为我认为它有太多的过程。但是毕加索的人非常容易使用。如果你们中有些人不知道如何导入毕加索,这是链接:stacktips.com/tutorials/android/…
猜你喜欢
  • 1970-01-01
  • 2018-01-10
  • 2013-03-12
  • 2017-07-12
  • 2015-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多