我正在开发一款卡罗姆游戏。我试图通过在队列中添加动画来动画大约20个ImageViews(硬币)......单个视图(硬币)的动画很好但是当我完全动画时它变得非常慢。我希望所有的硬币同时没有问题。这是我写的代码。
private void startAnimations(int ID, float[] values) {
switch (ID) {
case 1: // 43 float values
/*
*
* long timeinmill = System.currentTimeMillis();
System.out.println ("\n++++++++ JAVA : ANIMATION METHOD ENDING TIME = "+timeinmill+" +++++++++++\n");
try {
FileWriter fstream = new FileWriter("/sdcard/carromjava.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write(Long.toString(timeinmill));
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Close the output stream
*/
int counter=0;
int conX=0,conY=1;
boolean go=false;
if(watch==0){
pos=getPositions();
watch=1;
}
long duration = (long) (values[41]*1000); //(values[40] * 1000);
LinearInterpolator linear_interpolator = new LinearInterpolator(); //AccelerateInterpolator();
DecelerateInterpolator linear_interpolator1=new DecelerateInterpolator();
SetAnimation setAnimation = new SetAnimation();
setAnimation.duration = duration;
setAnimation.checkPocketFlag = (int) (values[40]);
setAnimation.produce_sound_type = (int) (values[42]);
// AnimatorSet animatorSet = new AnimatorSet();
// ArrayList<Animator> animationFull=new ArrayList<Animator>();
//System.out.println("Values length ="+values.length);
/* double dx=Math.abs(pos[0]-(values[0]- board_striker/2.0f));
double dy=Math.abs(pos[1]-(values[1]- board_striker/2.0f));
double distance=Math.sqrt((double)(dx*dx)+(dy*dy));
//double velx=dx/values[41];
//double vely=dy/values[41];
double duraX= Math.abs((dx/values[43]))*1000;
double duraY= Math.abs((dy/values[44]))*1000;
double velocity=Math.sqrt((double)(values[43]*values[43])+(values[44]*values[44]));
double dura=distance/velocity;*/
for (int i = -1; i < 19; i++) {
if (i == -1) {
//System.out.println("Coins");
//System.out.println("x="+pos[0]+"Y="+pos[1]);
//System.out.println("Animx="+(values[0]- board_striker/2.0f)+"Animy="+(values[1]- board_striker/2.0f));
ObjectAnimator anim1 = ObjectAnimator.ofFloat(striker,
"translationX",pos[counter], values[counter++] - board_striker/2.0f );
ObjectAnimator anim2 = ObjectAnimator.ofFloat(striker,
"translationY",pos[counter], values[counter++] - board_striker/2.0f );
//Setting Interpolator
anim1.setInterpolator(linear_interpolator);
anim2.setInterpolator(linear_interpolator);
anim1.setDuration((long)values[41]);
anim2.setDuration((long)values[41]);
// if((pos[conX] != (values[conX]-board_striker / 2.0f)) && (pos[conY]!=(values[conY]-board_striker / 2.0f))){
//System.out.println("Animating");
// animationFull.add(anim1);
// animationFull.add(anim2);
AnimatorSet animatorSet = new AnimatorSet();
// animatorSet.play(anim1);
// animatorSet.play(anim2);
animatorSet.playTogether(anim1, anim2);
// animatorSet.setDuration((long)values[41]);
animatorSet.setInterpolator(linear_interpolator);
//animatorSet.start();
setAnimation.anim_queue.add(animatorSet);
setAnimation.last_anim = animatorSet;
// }
conX=+2;
conY=+2;
continue;
//break;
}
ObjectAnimator anim1 = ObjectAnimator.ofFloat(
pieces[i].coinImv, "translationX",
values[counter++] - board_carrommen/2.0f);//2*i+2
ObjectAnimator anim2 = ObjectAnimator.ofFloat(
pieces[i].coinImv, "translationY",
values[counter++] - board_carrommen/2.0f);//2*i+2+1
anim1.setInterpolator(linear_interpolator);
anim2.setInterpolator(linear_interpolator);
anim1.setDuration((long)values[41]);
anim2.setDuration((long)values[41]);
// if((pos[conX] != (values[conX]- board_carrommen / 2.0f)) && (pos[conY]!=(values[conY]- board_carrommen / 2.0f))){
// animationFull.add(anim1);
// animationFull.add(anim2);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(anim1, anim2);
animatorSet.setInterpolator(linear_interpolator);
//animatorSet.setDuration((long)values[41]);
setAnimation.last_anim = animatorSet;
setAnimation.anim_queue.add(animatorSet);
// }
conX+=2;
conY+=2;
}
if(watch!=0){
for(int i=0;i<40;i++){
if(i==0 || i==1)
pos[i]=values[i]- board_striker/2.0f;
else
pos[i]=values[i]-board_carrommen / 2.0f;
}
}
/* animatorSet.playTogether(animationFull);
animatorSet.setDuration((long) (duration));
animatorSet.setInterpolator(linear_interpolator);
setAnimation.last_anim=animatorSet;*/
// Create a job and push to job queu
animation_queue_set.add(setAnimation);
if (read_from_queue) {
start_set_anim();
read_from_queue = false;
}
}
}
anim_listener = new AnimatorListener() {
long start_time ;
public void onAnimationStart(Animator animation) {
start_time = System.currentTimeMillis();
}
public void onAnimationRepeat(Animator animation) {
// TODO Auto-generated method stub
}
public void onAnimationEnd(Animator animation) {
// TODO Auto-generated method stub
//long exit = System.currentTimeMillis();
//logger.info("\t Duration = "+animation.getDuration()+ ", Animation Start time = "+start_time +" , +++++++ animation actual end = "+ exit +"\n");
start_set_anim();
}
public void onAnimationCancel(Animator animation) {
// TODO Auto-generated method stub
// onAnimCancel(animation);
}
};
anim_listener_check_pocket = new AnimatorListener() {
public void onAnimationStart(Animator animation) {
// TODO Auto-generated method stub
// onAnimStart(animation);
//System.out.println ("\n +++++++ Animation Start delay = "+animation.getStartDelay());
animation.setStartDelay(0);
}
public void onAnimationRepeat(Animator animation) {
// TODO Auto-generated method stub
}
public void onAnimationEnd(Animator animation) {
// TODO Auto-generated method stub
nativeInterface.IonAnimationEnd();
start_set_anim();
}
public void onAnimationCancel(Animator animation) {
// TODO Auto-generated method stub
// onAnimCancel(animation);
}
};
private void start_set_anim() {
if (animation_queue_set.isEmpty()) {
read_from_queue = true;
return;
}
SetAnimation recent_anim = animation_queue_set.removeFirst();
Iterator<AnimatorSet> it = recent_anim.anim_queue.listIterator();
try{
if (recent_anim.checkPocketFlag != 0) {
if(recent_anim.last_anim!=null)
recent_anim.last_anim.addListener(anim_listener_check_pocket);
else
start_set_anim();
} else {
if(recent_anim.last_anim!=null)
recent_anim.last_anim.addListener(anim_listener);
else
start_set_anim();
}
}catch(Exception e){
System.out.println("Exception Raised "+e);
e.printStackTrace();
}
/* recent_anim.last_anim.setStartDelay(0);
recent_anim.last_anim.start();*/
while (it.hasNext()) {
AnimatorSet aset = it.next();
aset.setStartDelay(0);
aset.start();
//(it.next()).setStartDelay(0);
//(it.next()).start();
it.remove();
}
/*
* if (animation_queue_set.isEmpty()) { read_from_queue = true; }
*/
}
请帮我如何做所有的动画顺利一起。我知道去OpenGL,但没有任何方法可以做到这一点。
答案 0 :(得分:0)
如果您使用UI类为每个对象设置动画并拥有许多具有同步动画的对象,则始终建议使用OpenGL等低级API。或者,如果您对学习OpenGL不满意,可以使用Cocoas 2D或其他优秀的API,这些API只能在低级API上创建强大的框架。