在自定义ImageView中绘制位图,并获取ImageView的坐标,而不考虑设备 [英] Draw Bitmap in Custom ImageView and get the Co-ordinates of the ImageView irrespective of the device

查看:340
本文介绍了在自定义ImageView中绘制位图,并获取ImageView的坐标,而不考虑设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得 ImageView 的坐标,不考虑设备大小。

I want to get the co-ordinates of the ImageView, Irrespective of device size.

任何可能的方式!!

我试图创建 ImageView 的特定大小,

I have tried to create specific size for the ImageView,Even specific size for the Parent View also ,but its not working.

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

I have tried the following possible ways.

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

我已经尝试过Matrix了,但不工作。

I have tried with Matrix too ,But not working.

Matrix m = imageview.getImageMatrix();

尝试下面的代码,但它也不工作。

Tried the below code, but it is also not working.!!

我需要为同一点(位置)的所有设备获得相同的{x,y}坐标。

I need to get the same {x,y} Co-ordinates for all devices for the same point (Location).

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;
    }

这里是draw方法:

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

if i set bitmap image in draw, it does not fit the screen for every devices. If i set image with Display width and height, i am getting different co-ordinates.

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

    }

任何想法或帮助都会有帮助。

Any Idea or help would be really helpful.

推荐答案

最后找到一些相对的解决方案,所有设备的相同坐标。使用Activity,没有自定义ImageView。

Finally found some relative solution,Same Co-ordinates for all devices.Using Activity,no Custom ImageView.

On ImageView OnTouch ..

On 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;
  }

使用画布绘制

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

这篇关于在自定义ImageView中绘制位图,并获取ImageView的坐标,而不考虑设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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