触摸Android缩放按钮 [英] Android scale button on touch
本文介绍了触摸Android缩放按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道如何将按钮缩放到确定的值,但是是否有一种方法可以使用户每次触摸按钮时都增加/减小按钮的大小?像这样:
I know how to scale the button to a determinated value, but is there a way to increase/decrease the button size per time as long the user is touching it? Something like this:
Button myButton = (Button)findViewById(R.id.myButton);
myButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Some timer action here or is there a better way?
v.setScaleX(v.getScaleX() + 0.1f);
v.setScaleY(v.getScaleY() + 0.1f);
return true;
}
else if(event.getAction() == MotionEvent.ACTION_UP) {
v.setScaleX(1);
v.setScaleY(1);
return true;
}
return false;
}
});
其他想法-不起作用:
myButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Timer timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
myButton.setScaleX(myButton.getScaleX() + 0.1f);
myButton.setScaleY(myButton.getScaleY() + 0.1f);
}
};
while(event.getAction() != MotionEvent.ACTION_UP){ //Seems to be an infinite loop
timer.schedule(timerTask, 100);
}
}
else if(event.getAction() == MotionEvent.ACTION_UP) {
v.setScaleX(1);
v.setScaleY(1);
}
return false;
}
});
有没有办法用xml(可绘制和动画)来做整个事情?
And is there a way to do the whole thing with xml (drawable and animations)?
推荐答案
尝试以下操作:
@Override
public boolean onTouch(View v, MotionEvent motionEvent) {
int action = motionEvent.getAction();
if (action == MotionEvent.ACTION_DOWN) {
v.animate().scaleXBy(100f).setDuration(5000).start();
v.animate().scaleYBy(100f).setDuration(5000).start();
return true;
} else if (action == MotionEvent.ACTION_UP) {
v.animate().cancel();
v.animate().scaleX(1f).setDuration(1000).start();
v.animate().scaleY(1f).setDuration(1000).start();
return true;
}
return false;
}
这应该可以解决问题;)
This should do the trick ;)
这篇关于触摸Android缩放按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文