运行循环检查整数,不工作

时间:2014-04-14 14:40:17

标签: java android eclipse

我试图获取用户手指触摸的位置并检测它是否在图像视图的范围内。我得到了所有正确的整数返回给我,这些值没有问题。我遇到的问题是在尝试检测returnX是否大于或小于imageview X int时。

ImageView img;
float eventX;
float eventY;
float x,y,x2,y2;
String TAG = "imgPos";
String TAG2 = "downcheck";
String TAG3 = "bounds";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    img = (ImageView)findViewById(R.id.imageView1);
}

public void fingerDownLoop(){
    if (SingleTouchEventView.userDown == true){
        Log.e(TAG2, "Down is true");
         Thread myThread = new Thread(myRunnable);
         myThread.start();
    } else if (SingleTouchEventView.userDown == false){
        Log.e(TAG2, "Down is false");
    }
}



public void getFingerLocation(){


    float returnX = SingleTouchEventView.eventX;
    float returnY = SingleTouchEventView.eventY;
    Log.e(TAG3, " - X -");
    Log.e(TAG3, " getX: " + returnX);
    Log.e(TAG3, " - Y -");
    Log.e(TAG3, " getY: " + returnY);
    if (returnX < x){
        Log.e(TAG3, "Outside bounds");
    }
    if (returnX > x){
        Log.e(TAG3, "Inside bounds");
    }
}

 Runnable myRunnable = new Runnable() {
      @Override
      public void run() {
           while (SingleTouchEventView.userDown == true) {
                try {
                    Log.e(TAG2, "FingerDown");
                    getFingerLocation();
x = img.getLeft();
                    Thread.sleep(500);

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } // Waits for 1 second (1000 milliseconds)

           }
      }
 };

}     

testText是一个按钮点击,它获取imageviews位置并将其存储为值。我遇到的问题是:

    if (returnX < x){
        Log.e(TAG3, "Outside bounds");
    }
    if (returnX > x){
        Log.e(TAG3, "Inside bounds");
    }

即使returnX小于X,所有方式都显示为内部边界。任何想法?

更新了获取img位置。

    int[] img_coordinates = new int[2];

    img.getLocationOnScreen(img_coordinates);
    double x_center = (double)img_coordinates[0] + img.getWidth()/2.0;
    double y_center = (double)img_coordinates[1] + img.getHeight()/2.0;

    int halfwidth = 150 / 2;
    int halfheight = 150 / 2;
    yboundtop = y_center - halfheight;
    yboundbottom = y_center + halfheight;
    xboundleft = x_center - halfwidth;
    xboundright = x_center + halfwidth;



    float returnX = eventX;
    float returnY = eventY;

    if (returnX < xboundleft || returnX > xboundright || returnY < yboundtop || returnY > yboundbottom) {
        Log.e(TAG3, "Outside bounds");
    }
    if (returnX > xboundleft && returnX < xboundright && returnY > yboundtop && returnY < yboundbottom) {
        Log.e(TAG3, "Inside bounds");
    }

2 个答案:

答案 0 :(得分:0)


我不确定您希望通过代码完成什么,但检查循环效率不高,在这种情况下可能没有必要。

您的imageView有一个方法getHitRect(),您可以像这样使用它:

Rect hitRect = new Rect();
imageView.getHitRect(hitRect);
if(hitRect.contains(touchEvent.getX(), touchEvent.getY())){
    // your touch is inside, do something useful
}

在触控方式中使用

答案 1 :(得分:0)

现在我知道你在找什么,我想我可以帮忙。覆盖活动的onTouchEvent()方法以捕获您的触摸事件。这比自己做循环要高效得多。

private boolean mTouchInImage = false;

public boolean onTouchEvent(MotionEvent event) {

    boolean handledTouch = false;

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        Rect hitRect = new Rect((int)img.getX(), (int)img.getY(), 
                (int)(mg.getX() + img.getWidth()), (int)(img.getY() + img.getHeight()));            
        handledTouch = mTouchInImage = hitRect.contains((int)event.getX(), (int)event.getY());
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        handledTouch = mTouchInImage;
        mTouchInImage = false;
        break;
    case MotionEvent.ACTION_MOVE:
        if (mTouchInImage) {
            img.setX(event.getX() - img.getWidth() / 2);
            img.setY(event.getY() - img.getHeight() / 2);
            img.invalidate();
            handledTouch = true;
        }
        break;
    }
    return handledTouch;
}