我使用sdk创建一个“添加”按钮,所以当我按下“添加”按钮时我想把它移到左边,当我再按一次时回到原来的位置。我遇到的问题是按钮只能在按下时向左移动,并且在我再次按下它之后无法回到原来的位置。我在if ... else函数上遇到了什么问题吗?任何帮助都会很感激。
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 :(得分:0)
我认为
有问题final Animation animation2 = new TranslateAnimation(100,0,0,0);
应该是
final Animation animation2 = new TranslateAnimation(0,-100,0,0);
:)