我尝试为Android开发一个App。我有以下代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
CardImage = (ImageView)findViewById(R.id.CardImage);
CardImage.setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_UP)
{
System.out.println("Touch!");
}
return true;
}
如何在模拟器中调试Ontouch功能?我试图拖动图像切换鼠标但没有任何事情发生。
谢谢你的帮助!
答案 0 :(得分:0)
尝试此代码(注意:未经测试)
CardImage.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int X = (int) event.getX();
int Y = (int) event.getY();
int action = event.getAction();
if (action == MotionEvent.ACTION_UP){
TranslateAnimation anim = new TranslateAnimation(CardImage.getLeft(),X,CardImage.getTop()
,Y);
anim.setFillAfter(true);
anim.setDuration(1000);
CardImage.startAnimation(anim);
}
return true;
}
});
答案 1 :(得分:0)
它刚刚使用了以下代码。
public class TestTouch extends Activity implements View.OnTouchListener{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.flag); // my image
iv.setOnTouchListener(this);
setContentView(iv);
}
public boolean onTouch(View v, MotionEvent event)
{
System.out.println(event.getAction());
if(event.getAction() == MotionEvent.ACTION_UP)
{
System.out.println("Touch!");
}
return true;
}
}