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

标签 java android imageview zooming coordinates

我有一张图像,当触摸不同部分时显示不同的 toast 。这是代码。

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

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

最佳答案

更新 我刚刚给 TouchImageView 一个新的更新。除了平移和捏合缩放之外,它现在还包括双击缩放和滑动。下面的代码非常过时。您可以查看github project to get the latest code .

用途 将 TouchImageView.java 放入您的项目中。然后就可以像 ImageView 一样使用它了。示例:

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

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

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

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

关于java - 如何在不丢失 x 和 y 布局坐标的情况下添加缩放功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21018688/

相关文章:

java - Spring MVC - 在多部分 HTTP Post 中获取非多部分表单/数据

java - 如何构建适合java客户端的WCF服务

android - 允许用户仅访问我的内容提供者中的某些表

android - 将 ImageView 放在布局中的 ImageView 上

android - 如何同时在屏幕上绘制2个PNG图片

java - 使用java的TreeMap构建最小生成树

java - Jackson:如何将平面 json 转换为嵌套 json

android - SharedPreferences.getFloat() 有什么意义

android - 具有多列的 ORMLITE ORDER_BY

android - 缩放 ImageView 以适应 ToolBar 并重叠