动画(移动)ImageView的工作方式,但不是其他

动画(移动)ImageView的工作方式,但不是其他

问题描述:

我创建一个“添加”按钮通过使用sdk,所以我想它将它移动到左边,当我按下“添加”按钮,并返回到原来的地方当我再次按下。我遇到的问题是按下按钮时,按钮只能向左移动,并且在我尝试再次按下按钮后无法回到原来的位置。它是否在if ... else函数上存在一些问题?任何帮助将不胜感激。动画(移动)ImageView的工作方式,但不是其他

public class TestImage2 extends Activity { 
/** Called when the activity is first created. */ 

ImageView image_plus; 
ImageView image_subtract; 
ImageView image_multiply; 
ImageView image_div; 

String op; 

boolean pergi = false; 
final Animation animation = new TranslateAnimation(0,100,0,0); 
final Animation animation2 = new TranslateAnimation(100,0,0,0); 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    image_plus  = (ImageView)findViewById(R.id.test_image);  
    image_subtract = (ImageView)findViewById(R.id.test_image2); 
    image_multiply = (ImageView)findViewById(R.id.test_image3); 
    image_div  = (ImageView)findViewById(R.id.test_image4); 

    image_plus.setOnClickListener(new OnClickListener(){ 

     public void onClick(View view) { 

       if(pergi == false) 
       { 

         animation.setDuration(1000); 
         image_plus.startAnimation(animation); 
         pergi= true; 

         animation.setFillAfter(true); 
       } 
       else 
       { 

         animation2.setDuration(1000); 
         image_plus.startAnimation(animation2); 
         pergi = false; 
         animation2.setFillAfter(true); 
       } 
       }}); 
+0

现在看来似乎只能在运行,如果它..函数,直到else ...函数。我是否需要将“真”/“假”替换为“1”/“0”等其他字符? – stevenlim 2012-04-06 17:37:27

+0

p/s:对不起,我仍然是新手和编码的初学者。 – stevenlim 2012-04-06 17:40:51

+0

你可以使用调试器吗?我可以教你:)在你的代码的左边点红点 - 让我们说'public void onClick(View view)',然后在你的JAVA IDE中单击调试。然后,你的应用程序将开始,做你必须做的,然后点击你的应用程序中的按钮 - 启动你的方法,调试将停止在你选择的行上:)添加通过选择变量'pergi'并点击鼠标右键>选择'add to watch',然后在你的JAVA IDE的底部,你会看到它的值变量。如果您有IntelliJ,请点击F8转发代码。 – deadfish 2012-04-06 18:36:08

我认为这有什么不对的

final Animation animation2 = new TranslateAnimation(100,0,0,0); 

应该

final Animation animation2 = new TranslateAnimation(0,-100,0,0); 

:)

+0

它不工作。按钮仍然不会回来。无论如何感谢您的帮助。 :) – stevenlim 2012-04-06 18:16:09