使用ontouch监听器获取手指位置

时间:2014-03-21 19:01:54

标签: android android-imageview ontouchlistener

我试图使用ontouch监听器来获取手指的位置但是没有任何东西被打印到logcat。我的代码怎么了?

              ImageView Im2;
 FrameLayout.LayoutParams layout;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Im2 = (ImageView)findViewById(R.id.imageView2);
    Im2.setOnTouchListener(new OnTouchListener() {    
        @Override
        public boolean onTouch(final View v, final MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {

               Log.d("  main ", " value of x  down" + event.getX());
            } 
            else if(event.getAction() == MotionEvent.ACTION_MOVE) {
                  Log.d("  main ", " value of x  move " + event.getX());
            }
            return false;
        }
    });




}

1 个答案:

答案 0 :(得分:0)

您的问题是您的onTouch功能始终返回 false 所以试试这个代码而不是你的代码

 Im2.setOnTouchListener(new OnTouchListener() {    
    @Override
    public boolean onTouch(final View v, final MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
           Log.d("  main ", " value of x  down" + event.getX());
           return true;
        } 
        else if(event.getAction() == MotionEvent.ACTION_MOVE) {
              Log.d("  main ", " value of x  move " + event.getX());
              return true;
        }
        return false;
    }
});