Java 如何多次重复该方法
我需要重复x次。我怎样才能做到这一点?非常感谢。 例如,x=3,我需要启动动画3次。对不起,英语不好Java 如何多次重复该方法,java,android,void,Java,Android,Void,我需要重复x次。我怎样才能做到这一点?非常感谢。 例如,x=3,我需要启动动画3次。对不起,英语不好 public void animation (View v){ view = new ImageView(getApplicationContext()); view.setImageResource(R.drawable.lemon_prig); view.setLayoutParams(new ViewGroup.LayoutParams
public void animation (View v){
view = new ImageView(getApplicationContext());
view.setImageResource(R.drawable.lemon_prig);
view.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
fl = (FrameLayout)findViewById(R.id.frameLayout1);
fl.addView(view);
Random random = new Random();
TranslateAnimation anim = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, random.nextFloat(),
Animation.RELATIVE_TO_PARENT, random.nextFloat(),
Animation.RELATIVE_TO_PARENT, -0.05f,
Animation.RELATIVE_TO_PARENT, 1.5f);
anim.setDuration(1800);
anim.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation paramAnimation) { }
public void onAnimationRepeat(Animation paramAnimation) { }
public void onAnimationEnd(Animation paramAnimation) {
fl.post(new Runnable() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
fl.removeAllViews();
}
});
}
});
}
});
view.startAnimation(anim);
}
public void limonplus(View v){
count=count + i;
animation(v);
}
您可以使用递归来实现这一点(假定您不会在一行中执行此方法数千次):
for(int i=0;iTail递归在非函数语言中通常是个坏主意。它在形式上等同于迭代,不涉及基本情况的堆栈开销和复杂性。非常简单。我是杜德。非常感谢您首先学习编程!!
public void animation (View v, int x){
if(x == 0)
return;
//method code here
animation(v, x-1);
}
for (int i=0; i<x; i++){
animation(v); // v is some View here.
}