我知道如何将按钮缩放到一个确定的值,但是只要用户触摸它,就有办法增加/减少每次按钮的大小吗?像这样的东西:
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(可绘制和动画)完成整个事情?
请尝试以下方法:
@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; }
这应该做的伎俩;)