在自定义ImageView中绘制位图并获取ImageView的坐标,而不考虑设备。

6

我想获取ImageView的坐标,不受设备大小的影响。

是否有可能!!

我尝试为ImageView创建特定的大小,甚至为Parent View创建特定的大小,但都不起作用。

我尝试了以下可能的方法。

int[] posXY = new int[2];
imageview.getLocationOnScreen(posXY);
int MoveX = posXY[0];
int MoveY = posXY[1];

我也尝试使用Matrix,但没有起作用。

Matrix m = imageview.getImageMatrix();

尝试了下面的代码,但它也没有起作用。!
我需要在同一位置(Location)上为所有设备获取相同的{x,y}坐标。
final float[] getPointerCoords(ImageView view, MotionEvent e)
    {
        final int index = e.getActionIndex();
        final float[] coords = new float[] { 
                e.getX(index), e.getY(index) 
                };
        Matrix matrix = new Matrix();
        view.getImageMatrix().invert(matrix);

        matrix.mapPoints(coords);

        return coords;
    }

这里是绘图方法:

如果我在绘图中设置位图图像,它不能适应每个设备的屏幕。如果我使用显示器宽度和高度来设置图像,则会得到不同的坐标。

 @Override
    public void draw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.draw(canvas);

        Resources res = getResources();
        Drawable drawable = res.getDrawable(R.drawable.subimage);
        mBitmap = ((BitmapDrawable) drawable).getBitmap();     
        canvas.drawBitmap(mBitmap, 0, 0, null);

    }

任何想法或帮助都将非常有用。

你在哪个方法中调用了 imageview.getLocationOnScreen(posXY); - aravindsagar
使用 getImageMatrix 并仔细阅读 Matrix API。 - pskink
@pskink您好,请帮忙查看我更新的问题。 - Janmejoy
@pskink,我还没有尝试过使用canvas onDraw方法,让我试试!! - Janmejoy
1
你看过我发的链接了吗?你看过Table 1了吗?如果是,你有没有阅读关于ALL *dpi 限定符的内容? - pskink
显示剩余57条评论
3个回答

1

最终找到了一些相关的解决方案,所有设备都使用相同的坐标。使用Activity而非自定义ImageView。

在ImageView OnTouch中...

 public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
      final int index = e.getActionIndex();
      final float[] coords = new float[] {
              e.getX(index), e.getY(index)
      };
      Matrix matrix = new Matrix();
      choosenImageView.getImageMatrix().invert(matrix);
      matrix.mapPoints(coords);
    return true;
  }

使用 Canvas 绘制
        Resources res = getResources();
        Drawable drawable = res.getDrawable(R.drawable.image);

        Bitmap bmp= ((BitmapDrawable) drawable).getBitmap();

        alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
        canvas = new Canvas(alteredBitmap);
        paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setStrokeWidth(5);
        matrix = new Matrix();
        canvas.drawBitmap(bmp, matrix, paint);

1

您需要首先计算您设备的屏幕比例,

deviceWidth / screenHeight = ratio

然后你需要将其乘以你想要的坐标:

ratio * xCoord = newXcoord; // and same for Y coord

通过这样做,您将为所有设备大小保留等效坐标。
此外,您可以声明相对于屏幕尺寸的百分比来设置大小和坐标,例如:
x = 0.2 * deviceWidth  // %20 of device width from left

0
你可以尝试这个。
imageview.post(new Runnable() {
            @Override
            public void run() {
                int[] posXY = new int[2];
                imageview.getLocationOnScreen(posXY);
                int MoveX = posXY[0];
                int MoveY = posXY[1];
Log.i("xy co-ordinate", "x "+MoveX, "y"+MoveY);

            }
        });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接