ImageView的getLocationtOnScreen机器人 [英] ImageView getLocationtOnScreen android

查看:92
本文介绍了ImageView的getLocationtOnScreen机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让屏幕上的图像的坐标。我现在有一个活动中的一个ImageView的。我明白,一旦布局已创建getLocationOnScreen()方法只能被调用,所以调用的OnCreate函数中此方法将返回[0,0]。但我不知道如何得到这个方法返回正确的值。我曾尝试各种overiding超类方法,如OnStart方法或onTouchEvent方法,它仍然会返回[0,0]给我。在code目前,我有如下:

I am trying to get the coordinates of the image on the screen. I currently have an ImageView within an activity. I understand that the getLocationOnScreen() method can only be called once the layout has been created, so calling this method within the oncreate function would return [0,0]. But I do not know how to get this method to return the correct values. I have tried overiding various superclass methods, like the onstart method or the onTouchEvent method and it still returns [0,0] to me. The code I currently have is as follows:

@Override
public void onCreate(Bundle savedInstanceState) 
{
    // Some code here after which ..
    image = (ImageView) findViewById(R.id.imageVfi);        
    image.setImageBitmap(imageData.entrySet().iterator().next().getValue());
}

然后我有我已经重写OnStart方法

Then I have the onStart method which I have overriden

@Override
public void onStart()
{
    super.onStart();        
    image.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            int[] dim = new int[2];
            image.getLocationOnScreen(dim);
            new AlertDialog.Builder(DisplayPicture.this)
            .setIcon(R.drawable.icon)
            .setTitle("Touch coordinates : " +
                    String.valueOf(dim[0]) + "x" + String.valueOf(dim[1]))
            .setPositiveButton("OK", 
                     new DialogInterface.OnClickListener() {   
                 public void onClick(DialogInterface dialog, int which) 
                 {
                     // TODO Auto-generated method stub
                 }
            }).show();
            // image calculations go here
            return true;

        }
    });
}   

这将返回0X0给我。任何帮助深表AP preciated。

This returns 0X0 to me. Any help is much appreciated.

推荐答案

ImageView的不提供一种方式来获得它的屏幕图像的位置。可以只查询的ImageView本身的位置。您可以尝试使用ImageView.getImageMatrix(),但我不知道我们填充它当图像被简单地居中(而不是缩放等)

ImageView does not offer a way to get the location of its image on screen. You can only query the location of the ImageView itself. You can try to use ImageView.getImageMatrix() but I'm not sure we populate it when the image is simply centered (as opposed to scaled, etc.)

这篇关于ImageView的getLocationtOnScreen机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆