quicky_XY返回位图,内部循环名为quicky_XY,显示图像,但只显示最后一个图像。问题是什么

时间:2011-05-31 06:03:48

标签: android

public void onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);
   setContentView(R.layout.main); 
   setTitle("XY Plot");
   bitArray=new ArrayList<Bitmap>(s);     

   image = (ImageView) findViewById(R.id.image);

   width =  220;
   height = 220;
   while(k<s)
   {
   Bitmap temp=quicky_XY();
   image.setImageBitmap(temp); //displaying the image
   k++;
   try
   {
   Thread.sleep(500); //waiting for 500 ms
   }
   catch(InterruptedException e)
   {
   e.printStackTrace();
   }
   }
   }

public Bitmap quicky_XY()
{
      Bitmap Bmap1 = Bitmap.createBitmap(220,220, Config.ARGB_8888);
      Canvas canvas = new Canvas(Bmap1);

      final int color = 0xff0B0B61; 
      final Paint paint = new Paint();
      final Rect rect = new Rect(0, 0, width, height);  
      paint.setAntiAlias(true);
      canvas.drawARGB(0, 0, 0, 0);
      paint.setColor(color);
      canvas.drawRect(rect,paint);
      draw_the_grid(canvas);

      getdata();          //getting the data
      getmax(data_2_plot);

      offsetx = 10;                     //do not care abt these lines till offsety
      xscale = (210-offsetx )/(size-1);
      yscale = (200)/(m[0] -m[1]);
      offsety = (int)(10-(yscale*m[1]));       

      plot(canvas); //does all the drawing on canvas
      canvas.drawBitmap(Bmap1, rect, rect, paint);
      return Bmap1;              //returning the image

}    

1 个答案:

答案 0 :(得分:1)

您应该在另一个Thread中编写循环,以使其与Android UI线程分开运行。 例如:

Handler handler;
public void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);
       setContentView(R.layout.main); 
       setTitle("XY Plot");
       bitArray=new ArrayList<Bitmap>(s);     

       image = (ImageView) findViewById(R.id.image);
       handler = new Handler();
       width =  220;
       height = 220;
       new displayImage().start();
}
class displayImage extends Thread{
    public void run(){
        while(k<s)
        {
           Bitmap temp=quicky_XY();
           handler.post(new Runnable() {
            public void run() 
            {
                image.setImageBitmap(temp); //displaying the image
            }
           });
           k++;
           try
           {
           Thread.sleep(500); //waiting for 500 ms
           }
           catch(InterruptedException e)
           {
           e.printStackTrace();
           }
       }
   }
}
public Bitmap quicky_XY()
{
     Bitmap Bmap1 = Bitmap.createBitmap(220,220, Config.ARGB_8888);
     Canvas canvas = new Canvas(Bmap1);

     final int color = 0xff0B0B61; 
     final Paint paint = new Paint();
     final Rect rect = new Rect(0, 0, width, height);  
     paint.setAntiAlias(true);
     canvas.drawARGB(0, 0, 0, 0);
     paint.setColor(color);
     canvas.drawRect(rect,paint);
     draw_the_grid(canvas);

     getdata();          //getting the data
     getmax(data_2_plot);

     offsetx = 10;                     //do not care abt these lines till offsety
     xscale = (210-offsetx )/(size-1);
     yscale = (200)/(m[0] -m[1]);
     offsety = (int)(10-(yscale*m[1]));       

     plot(canvas); //does all the drawing on canvas
     canvas.drawBitmap(Bmap1, rect, rect, paint);
     return Bmap1;              //returning the image

}