在Custom ImageView中绘制位图并获取ImageView的坐标,而不管设备如何

Jan*_*joy 7 android canvas draw coordinates android-custom-view

ImageView无论设备大小如何,我都希望获得坐标.

有没有办法!!

我曾尝试为其创建特定尺寸ImageView,即使是特定尺寸Parent View,但它不起作用.

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

int[] posXY = new int[2];
imageview.getLocationOnScreen(posXY);
int MoveX = posXY[0];
int MoveY = posXY[1];
Run Code Online (Sandbox Code Playgroud)

我也尝试过Matrix,但没有工作.

Matrix m = imageview.getImageMatrix();
Run Code Online (Sandbox Code Playgroud)

尝试下面的代码,但它也无法正常工作.!!

我需要为同一点(位置)的所有设备获得相同的{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;
    }
Run Code Online (Sandbox Code Playgroud)

这是绘制方法:

如果我在绘图中设置位图图像,则它不适合每个设备的屏幕.如果我设置显示宽度和高度的图像,我会得到不同的坐标.

 @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);

    }
Run Code Online (Sandbox Code Playgroud)

任何想法或帮助都会非常有用.

Jan*_*joy 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;
  }
Run Code Online (Sandbox Code Playgroud)

使用画布绘制

        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);
Run Code Online (Sandbox Code Playgroud)