如何在不丢失 x 和 y 布局坐标的情况下添加缩放功能



我有一个图像,当触摸不同的部分时,它会显示不同的吐司。这是代码。

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;
    }
}

现在,当图像适合屏幕时,它可以正常工作。但我想要的是向图像添加缩放功能,而不会丢失图像特定部分的特定吐司的主要功能。有什么办法可以做到这一点。?我的意思是有什么东西可以表示缩放与 x 和 y 坐标变化的比率。?因此,即使在缩放后,图像在该特定部分中触摸时仍应执行相同的操作。?

更新我刚刚给了TouchImageView一个新的更新。它现在包括双击缩放和摆动以及平移和捏合缩放。下面的代码非常过时。您可以查看 github 项目以获取最新代码。

用法将 TouchImageView.java 放在您的项目中。然后可以像 ImageView 一样使用它。例:

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

如果您在 xml 中使用 TouchImageView,则必须提供完整的软件包名称,因为它是自定义视图。例:

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

注意:我已经删除了我之前的答案,其中包括一些非常旧的代码,现在直接链接到 github 上的最新代码。

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

如果在 xml 中使用 ZoomableImageView,则必须提供完整的包名称,因为它是自定义视图。例:

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

除此之外,您还可以查看项目https://github.com/laurencedawson/ZoomableImageView

最新更新