Saturday, January 28, 2012

Get rectangle and circle axis click on imageview

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;

public class MypracticeActivity extends Activity {
    private float cX=555;
    private float cY=211;
    private float cR=50;
    
    private float topRX = 829;
    private float topRY = 120;
    private float bottomLX = 612;
    private float bottomLY = 244;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent e) {
        // TODO Auto-generated method stub
        float x = e.getX();
        float y = e.getY();
        switch (e.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if(checkRect(x,y)){
                //Toast.makeText(getApplicationContext(), "X-"+String.valueOf(x)+"Y-"+String.valueOf(y), Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(), "Inside rect", Toast.LENGTH_SHORT).show();
            }
            
        }
        return super.onTouchEvent(e);
    }
    
    public boolean checkRect(float pX,float pY)
    {
        if(pX>=bottomLX && pX <= topRX)
        {
            if(pY>=topRY && pY <= bottomLY)
            {
                return true;
            }
        }
        return false;
    }
    
    public boolean checkPoints(float pX,float pY)
    {
        //(x-center_x)^2 + (y - center_y)^2 < radius^2
        double x = Math.pow((pX-cX), 2);
        double y = Math.pow((pY-cY), 2);
        if(cR>=(float) Math.sqrt(x+y))
            return true;
        return false;
    }
}

No comments:

Post a Comment