我有两个按钮,btn1和btn2。我想播放动画,而两个按钮保持在按下状态,当任何一个按钮被取消或甚至两个按钮都进入action_up状态(未按下)时动画停止。
这是我的代码:
final Animation animation = AnimationUtils.loadAnimation(this,
R.anim.aim);
animation.reset();
final ImageView maxName = (ImageView) findViewById(R.id.imageView1);
Button btn1 = (Button)findViewById(R.id.button3);
Button btn2 = (Button)findViewById(R.id.button2);
btn1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getActionMasked();
if (action == MotionEvent.ACTION_DOWN) {
maxName.startAnimation(animation);
} else if (action == MotionEvent.ACTION_UP
|| action == MotionEvent.ACTION_CANCEL) {
maxName.clearAnimation();
}
// TODO Auto-generated method stub
return false;
}
现在我正在用一个按钮播放它,但我想根据我上面写的内容进行更改。谢谢。等待帮助。
答案 0 :(得分:0)
final boolean oneIsPressed = false;
final boolean twoIsPressed = false;
final boolean isAnimating = false;
Button btn1 = (Button) findViewById(R.id.button3);
Button btn2 = (Button) findViewById(R.id.button2);
View.OnTouchListener touchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getActionMasked();
if (action == MotionEvent.ACTION_DOWN) {
if (v.getId() == R.id.button3) {
oneIsPressed = true;
} else if (v.getId() == R.id.button2) {
twoIsPressed = true;
}
if (oneIsPressed && twoIsPressed && !isAnimating) {
isAnimating = true;
maxName.startAnimation(animation);
}
} else if (action == MotionEvent.ACTION_UP
|| action == MotionEvent.ACTION_CANCEL) {
isAnimating = false;
maxName.clearAnimation();
}
return false;
}
};
btn1.setOnTouchListener(touchListener);
btn2.setOnTouchListener(touchListener);
}
答案 1 :(得分:0)
添加一个全局布尔变量,单击一个按钮时设置为true。例如:
private boolean isButtonOneClicked;
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getActionMasked();
if (action == MotionEvent.ACTION_DOWN) {
isButtonOneClicked = true;
} else if (action == MotionEvent.ACTION_UP
|| action == MotionEvent.ACTION_CANCEL) {
isButtonOneClicked = false;
}
为第二个按钮添加一个onTouchListener并为该按钮创建一个全局变量,与第一个按钮完全相同,它只有另一个名称。
创建一个方法,检查两个布尔值是否为真,然后启动动画
public void startAnimation() {
if(isButtonOneClicked && isButtonTwoClicked) {
maxName.startAnimation(animation);
} else {
maxName.clearAnimation();
}
}
现在,只要点击或取消选中按钮,就会调用此方法。