有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在不丢失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;
    }
}

现在,当图像适合屏幕时,它可以正常工作。但我想要的是在图像的特定部分添加缩放功能,而不丢失特定toast的主要功能。有没有办法做到这一点。?我的意思是,是否有任何东西可以表示缩放比例随x和y坐标的变化。?这样,即使在变焦后,在特定区域触摸图像时,图像仍应执行相同的操作


共 (2) 个答案

  1. # 1 楼答案

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

    用法 放置触摸图像视图。在你的项目中使用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上最新的代码

  2. # 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