ZooZ
ZooZ

Reputation: 309

How to add zoom feature without loosing the x and y layout coordinates

I have an image which shows different toasts when touched in different sections. Here is the code.

public class MainActivity extends Activity implements OnTouchListener
{
    String xcoordinate, ycoordinate;
    Integer xint, yint;
    Double xdoub, ydoub;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final View touchView = findViewById(R.id.lyt);
        touchView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) 
            {
                xcoordinate = String.valueOf(event.getX());
                ycoordinate = String.valueOf(event.getY());

                xdoub = Double.valueOf(xcoordinate);
                ydoub = Double.valueOf(ycoordinate);

                xint =  xdoub.intValue();
                yint =  ydoub.intValue();

                Toast.makeText(getApplicationContext(), "Touch coordinates : " +
                String.valueOf(event.getX()) + "x and " + String.valueOf(event.getY()) + "y", Toast.LENGTH_SHORT).show();
                LaunchPopup();    
                return true;
            }
        });
    }

    public void LaunchPopup()
    {
        if(xint > 243 && xint < 307 && yint > 315 && yint < 410)
        {
            Toast.makeText(getApplicationContext(), "Center", Toast.LENGTH_SHORT).show();
        }

        else if(xint > 330 && xint < 394 && yint > 24 && yint < 122)
        {
            Toast.makeText(getApplicationContext(), "Manual 68 Section", Toast.LENGTH_SHORT).show();
        }

        else if(xint > 330 && xint < 394 && yint > 154 && yint < 250)
        {
            Toast.makeText(getApplicationContext(), "Manual 79 Section", Toast.LENGTH_SHORT).show();
        }

        else if(xint > 330 && xint < 394 && yint > 314 && yint < 410)
        {
            Toast.makeText(getApplicationContext(), "Manual 17 Section", Toast.LENGTH_SHORT).show();
        }

        else if(xint > 330 && xint < 394 && yint > 458 && yint < 554)
        {
            Toast.makeText(getApplicationContext(), "Manual 45 Section", Toast.LENGTH_SHORT).show();
        }

        else if(xint > 330 && xint < 394 && yint > 602 && yint < 700)
        {
            Toast.makeText(getApplicationContext(), "Group Task Section", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        return false;
    }
}

Now its working fine when the image is fit to screen. But what I want is to add zoom feature to the image without loosing the main function of particular toast on particular sections of the image. Is there any way to do this.? I mean is there anything by which I can represent the ratio of the zoom with the change in x and y coordinates.? So that the image should still do the same action when touched in that particular section even after zoom.?

Upvotes: 2

Views: 154

Answers (2)

Jitesh Upadhyay
Jitesh Upadhyay

Reputation: 5260

ZoomableImageView img = (ZoomableImageView) findViewById(R.id.img);

If you are using ZoomableImageView in xml, then you must provide the full package name, because it is a custom view. Example:

<com.example.touch.ZoomableImageView
    android:id="@+id/img”
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

beside this you can go with have a look on project https://github.com/laurencedawson/ZoomableImageView

Upvotes: 1

Lijo
Lijo

Reputation: 6788

UPDATE I've just given TouchImageView a new update. It now includes Double Tap Zoom and Fling in addition to Panning and Pinch Zoom. The code below is very dated. You can check out the github project to get the latest code.

USAGE Place TouchImageView.java in your project. It can then be used the same as ImageView. Example:

TouchImageView img = (TouchImageView) findViewById(R.id.img);

If you are using TouchImageView in xml, then you must provide the full package name, because it is a custom view. Example:

<com.example.touch.TouchImageView
    android:id="@+id/img”
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Note: I've removed my prior answer, which included some very old code and now link straight to the most updated code on github.

Upvotes: 2

Related Questions