Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 3.03 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Creating Runnables dynamically
  2. private Runnable run1= new Runnable() {  
  3.     public void run() {
  4.         if(t1)
  5.         {
  6.             LayoutParams params1=(LayoutParams) l1.getLayoutParams();
  7.             params1.x=x1;
  8.             params1.y=y1;
  9.             l1.setLayoutParams(params1);
  10.             x2=r.nextInt(720-80)+80;
  11.             y2=r.nextInt(400-80)+80;
  12.  
  13.         TranslateAnimation ta1 = new TranslateAnimation(0, x2-x1, 0, y2-y1 );
  14.         ta1.setDuration(800);
  15.         ta1.setFillAfter(true);
  16.         l1.startAnimation(ta1);
  17.         x1=x2;
  18.         y1=y2;
  19.  
  20.         handler.postDelayed(run1, 800);
  21.  
  22.         }
  23.     }
  24.        
  25. this.runOnUiThread(run1);
  26.     this.runOnUiThread(run2);
  27.     this.runOnUiThread(run3);
  28.        
  29. for(j=0;j<c;j++)    
  30.     {
  31.  
  32.         mp[j] = MediaPlayer.create(getApplicationContext(), soundArray[i[j]]);
  33.         images[j]=new ImageView(getBaseContext());
  34.         lp[j]=new LayoutParams(50,50, x1[j], y1[j]);
  35.         images[j].setLayoutParams(lp[j]);
  36.         images[j].setBackgroundResource(imgArray[i[j]]);
  37.         images[j].setId(j);
  38.         images[j].setOnClickListener((OnClickListener)this);
  39.         abs.addView(images[j]);
  40.     }
  41.        
  42. for(j=0;j<c;j++)    
  43.     {
  44.         run[j]=new Runnable()
  45.         {
  46.             public void run() {
  47.                 if(t[j])
  48.  
  49.                 {
  50.  
  51.                     params[j]=(LayoutParams) images[j].getLayoutParams();
  52.                     params[j].x=x1[j];
  53.                     params[j].y=y1[j];
  54.                     images[j].setLayoutParams(params[j]);
  55.  
  56.                     x2[j]=r.nextInt(720-80)+80;
  57.                     y2[j]=r.nextInt(400-80)+80;
  58.  
  59.                     ta[j] = new TranslateAnimation(0, x2[j]-x1[j], 0, y2[j]-y1[j] );
  60.                     ta[j].setDuration(200);
  61.                     ta[j].setFillAfter(true);
  62.                     images[j].startAnimation(ta[j]);
  63.                     x1[j]=x2[j];
  64.                     y1[j]=y2[j];
  65.  
  66.                     handler.postDelayed(run[j], 200);  
  67.                 }
  68.             }
  69.  
  70.       };
  71.     for(j=0;j<c;j++)    
  72.     {
  73.         this.runOnUiThread(run[j]);
  74.     }
  75.        
  76. public class Single implements Runnable{
  77.     public ImageView img=new ImageView(getBaseContext());
  78.     public boolean t=true;
  79.     public int x2=0;
  80.     public int y2=0;
  81.  
  82.     public void run() {
  83.         if(t)
  84.  
  85.         {
  86.             LayoutParams params=(LayoutParams) img.getLayoutParams();
  87.             params.x=x2;
  88.             params.y=y2;
  89.             img.setLayoutParams(params);
  90.  
  91.             x2=r.nextInt(randX);
  92.             y2=r.nextInt(randY);
  93.  
  94.             TranslateAnimation ta = new TranslateAnimation(0, x2-params.x, 0, y2-params.y );
  95.             ta.setDuration(1000);
  96.             ta.setFillAfter(true);
  97.             img.startAnimation(ta);
  98.  
  99.             handler.postDelayed(this, 1000);    
  100.         }
  101.     }
  102.  
  103. }
  104.        
  105. public Single[] images;
  106. images=new Single[c];
  107.  
  108.     for(j=0;j<c;j++)    
  109.     {
  110.         images[j]=new Single();
  111.         images[j].x2=r.nextInt(randX);
  112.         images[j].y2=r.nextInt(randY);
  113.         images[j].t=true;
  114.     }
  115.     for(int j1=0;j1<c;j1++)
  116.     {
  117.  
  118.         this.runOnUiThread(images[j1]);
  119.     }