我有一个按钮,我试图让它变得可触摸,所以当我按住它时,它会持续移动一个ImageView按住它的时间,但相反,它每次按下它时只会移动一次ImageView。
final ImageView box = (ImageView) findViewById(R.id.box);
currentX = (int) box.getX();
Button rightarrow = (Button) findViewById(R.id.right);
rightarrow.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, final MotionEvent e) {
v.performClick();
if (e.getAction() == MotionEvent.ACTION_DOWN) {
v.setPressed(true);
handlerRight.postDelayed(new Runnable() {
@Override
public void run() {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
currentX += 5;
box.setX(currentX);
handlerRight.postDelayed(this, 100);
} else {
}
}
}, 0);
} else {
v.setPressed(false);
}
return false;
}
});
答案 0 :(得分:0)
在你在那里运行的那个新线程中,你需要创建一个在按下按钮时运行的while循环。在while循环中,只需移动图像即可。这是代码:
final ImageView box = (ImageView) findViewById(R.id.box);
currentX = (int) box.getX();
Button rightarrow = (Button) findViewById(R.id.right);
rightarrow.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, final MotionEvent e) {
v.performClick();
if (e.getAction() == MotionEvent.ACTION_DOWN) {
v.setPressed(true);
handlerRight.postDelayed(new Runnable() {
@Override
public void run() {
while (v.isPressed()){
currentX += 5;
box.setX(currentX);
//handlerRight.postDelayed(this, 100);
}
}
}, 0);
} else {
v.setPressed(false);
}
return false;
}
});
注意:我不确定那里
handlerRight.postDelayed(this, 100);
应该是因为我不熟悉它所以你只是把它。但这是我希望的!