【问题标题】:How to add zoom feature without loosing the x and y layout coordinates如何在不丢失 x 和 y 布局坐标的情况下添加缩放功能
【发布时间】:2014-01-09 11:17:48
【问题描述】:

我有一张图片,在不同部分触摸时会显示不同的祝酒词。这是代码。

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坐标变化的比率。?这样即使在缩放之后,在该特定部分中触摸图像时,图像仍应执行相同的操作。?

【问题讨论】:

标签: java android imageview zooming coordinates


【解决方案1】:

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

用法 将 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 上的最新代码。

【讨论】:

  • 我给你的答案投了 +1 票,因为你真的在 github 上更新了一篇很棒的作品
  • @404 触摸同一图像中不同的 x 和 y 坐标时的响应如何?它会起作用吗?
【解决方案2】:
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

【讨论】:

  • 触摸同一图像中不同的x和y坐标时的反应如何。?它会起作用吗?
  • 希望它会起作用。请查看源代码。我也会调查并通知你。请给我几分钟。
  • 好吧.. 谢谢.. 其实我不太了解 android,所以任何帮助都会非常棒。我会检查来源。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-19
  • 1970-01-01
  • 2016-01-01
  • 2011-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多