Пожалуйста, помогите
У меня есть 3 ImageViews, и я могу переместить эти 3 вида с TranslateAnimation, используя 3 Runnables.
Вот мой код:
private Runnable run1= new Runnable() {
public void run() {
if(t1)
{
LayoutParams params1=(LayoutParams) l1.getLayoutParams();
params1.x=x1;
params1.y=y1;
l1.setLayoutParams(params1);
x2=r.nextInt(720-80)+80;
y2=r.nextInt(400-80)+80;
TranslateAnimation ta1 = new TranslateAnimation(0, x2-x1, 0, y2-y1 );
ta1.setDuration(800);
ta1.setFillAfter(true);
l1.startAnimation(ta1);
x1=x2;
y1=y2;
handler.postDelayed(run1, 800);
}
}
};
тогда у меня есть Run2, run3, как это, и запустить их, позвонив
this.runOnUiThread(run1);
this.runOnUiThread(run2);
this.runOnUiThread(run3);
в OnCreate
Тогда у меня Array с int c ImageViews (например, c = 10, затем c = 20 ...)
for(j=0;j<c;j++)
{
mp[j] = MediaPlayer.create(getApplicationContext(), soundArray[i[j]]);
images[j]=new ImageView(getBaseContext());
lp[j]=new LayoutParams(50,50, x1[j], y1[j]);
images[j].setLayoutParams(lp[j]);
images[j].setBackgroundResource(imgArray[i[j]]);
images[j].setId(j);
images[j].setOnClickListener((OnClickListener)this);
abs.addView(images[j]);
}
Как я могу сделать что-то подобное с этим?
Как динамически создавать много Runnables?
Этот код не работает
for(j=0;j<c;j++)
{
run[j]=new Runnable()
{
public void run() {
if(t[j])
{
params[j]=(LayoutParams) images[j].getLayoutParams();
params[j].x=x1[j];
params[j].y=y1[j];
images[j].setLayoutParams(params[j]);
x2[j]=r.nextInt(720-80)+80;
y2[j]=r.nextInt(400-80)+80;
ta[j] = new TranslateAnimation(0, x2[j]-x1[j], 0, y2[j]-y1[j] );
ta[j].setDuration(200);
ta[j].setFillAfter(true);
images[j].startAnimation(ta[j]);
x1[j]=x2[j];
y1[j]=y2[j];
handler.postDelayed(run[j], 200);
}
}
};
for(j=0;j<c;j++)
{
this.runOnUiThread(run[j]);
}