【问题标题】:My UI is blinking and whenever i try to resize the imageview dynamically, the image disappear我的用户界面在闪烁,每当我尝试动态调整图像视图的大小时,图像就会消失
【发布时间】:2018-05-21 16:14:51
【问题描述】:
@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);

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




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

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

上面的代码是动态设计的。那里有一个图像,其中我将图像放在我的 android 屏幕上,这些图像是使用 Web 服务从我的数据库中获取的。问题是图像在闪烁,而且它太大了。我认为它正在闪烁,因为我正在线程中创建一个线程,但我仍然对如何修复它感到困惑。我的异步任务是如何获取图像。

public ArrayList<Properties> attendanceUse(String result) {
            ArrayList<Properties> attendanceusers = new ArrayList<Properties>();
            try {
                JSONArray jArray = new JSONArray(result);
                for (int i = 0; i < jArray.length(); i++) {
                    JSONObject json_data = jArray.getJSONObject(i);
                    Properties user = new Properties();

                    user.setPic(json_data.getString("student_ImgUrl"));
                    attendanceusers.add(user);
                    //attendanceusers.get(index)
                }
            } catch (JSONException e) {
                Log.e("log_tag", "Error parsing data " + e.toString());  
            }
            return attendanceusers;
        }

上面的代码是我如何获取与我的 htdocs 中的 php 查询相关的图像。 student_imgurl 是我从我的 mysql 数据库中选择的用于获取 url 的列。

private void update()
        {
            final Handler handler = new Handler();
            Timer timer = new Timer();
            String splithis;
         splithis=mySpinner.getSelectedItem().toString();
         splited = splithis.split(" ");
        course = splited[0];
        room = splited[1];
        sections = splited[2];

            TimerTask task = new TimerTask() {       
                @Override
                public void run() {
                    data = bw.getAttendanceFromDB(term, course,sections,day,room);
                    handler.post(new Runnable() {
                        public void run() {       
                            try {
                    //asyntask class ito
                                ArrayList<Properties> attendanceusers = attendanceUse(data);
                                addAttendance(attendanceusers); 

                            } catch (Exception e) {
                                // error, do something
                            }
                        }//run
                    });//handler mo
                }//runmo
            };//timer
            timer.schedule(task, 0,1000); //timer

        }

这是我的更新无效。它有一个计时器,因此图像会实时更新。

mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            data ="";


            new Thread(new Runnable() {
                public void run() {

                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                               update();                 
                        }
                    });   
                }
            }).start();
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

            return;
        }
    });

上面的代码是我在 Oncreate 中的代码。我有一个微调器/下拉菜单,他们将在其中选择课程室部分,然后当他们选择一个项目时,将显示该课程室部分中学生的图像。 我想要的是停止我的 UI 闪烁并调整图像视图的大小。

【问题讨论】:

    标签: java android eclipse


    【解决方案1】:

    我的想法是,这可以通过将 void update() 方法替换为 AsyncTask&lt;Void, Properties, Void&gt; 类来从数据库中提取与会者数据并同时在其 doInBackground() 方法中下载个人资料图像来更好地完成.加载每个与会者后,您可以通过 publishProgress(Properties)onProgressUpdate(Properties[]) 方法更新您的 UI。这应该会导致 UI 更新不那么卡顿。

    请记住,如果您同时运行多个 AsyncTask,您应该在 AsyncTask.THREAD_POOL_EXECUTOR 上执行它们以允许它们并行运行(默认执行器是 AsyncTask.SERIAL_EXECUTOR

    至于图像尺寸问题,可能是您没有在视图上设置正确的ImageView.ScaleType 以使其正确适配。我希望它应该是ImageView.ScaleType.CENTER_INSIDE

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-16
      • 1970-01-01
      • 2012-10-30
      • 2015-01-04
      • 1970-01-01
      相关资源
      最近更新 更多