onTouch事件坐标和imageview绘制坐标不匹配

时间:2015-01-14 14:48:13

标签: android events canvas imageview ontouchevent

我有:

                    imageView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            //Choose which motion action has been performed
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    //Get X, Y coordinates from the ImageView
                    int X = (int) event.getX();


                    int Y = (int) event.getY();

                        BitmapFactory.Options myOptions = new BitmapFactory.Options();
                    myOptions.inDither = true;
                    myOptions.inScaled = false;
                    myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
                    myOptions.inPurgeable = true;

                    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lithuania,myOptions);
                    Paint paint = new Paint();
                    paint.setAntiAlias(true);
                    paint.setColor(Color.BLUE);


                    Bitmap workingBitmap = Bitmap.createBitmap(bitmap);
                    Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);


                    Canvas canvas = new Canvas(mutableBitmap);
                    canvas.drawCircle(40, 160, 15, paint);


                    imageView.setAdjustViewBounds(true);
                    imageView.setImageBitmap(mutableBitmap);

任何想法,如何在这些坐标之间建立关系?因为现在我的onTouch坐标和绘图坐标必须不同才能正确地向用户表示。

在这里搜索,但似乎没有人有正确的答案。任何帮助将不胜感激:)

red one is my finger

修改

                    float xRatio = (float)bitmap.getWidth() / imageView.getWidth();
                    float xPos = event.getX() * xRatio;
                    float yPos = event.getY() * xRatio;

                    Canvas canvas = new Canvas(mutableBitmap);
                    canvas.drawCircle(xPos, yPos, 15, paint);

像魅力一样工作!

1 个答案:

答案 0 :(得分:2)

我测试了您的代码,问题是您的位图与您的imageView具有不同的尺寸。您需要确定尺寸的差异并相应地进行调整。例如,我使用的图像是1800x1800,而我的imageView是900x900,所以我必须将我的X和Y坐标乘以2以获得正确的位置

int xRatio = bitmap.getWidth() / imageView.getWidth();
int xPos = event.getX() * xRatio;

这是一个更强大的版本,它考虑了比目标视图小的位图:

            int xRatio, yRatio;
            int xPos, yPos;
            if (mutableBitmap.getWidth() >= ib.getWidth()) {
                xRatio = mutableBitmap.getWidth() / ib.getWidth();
                xPos = (int) Math.floor(xRatio * event.getX());
            } else {
                xRatio = ib.getWidth() / mutableBitmap.getWidth();
                xPos = (int) Math.floor(xRatio*event.getX()-((ib.getWidth() - mutableBitmap.getWidth())/2));
            }

            if (mutableBitmap.getHeight() >= ib.getHeight()) {
                yRatio = mutableBitmap.getHeight() / ib.getHeight();
                yPos = (int)Math.floor(event.getY()*yRatio);
            } else {
                yRatio = ib.getHeight() / mutableBitmap.getHeight();
                yPos = (int) Math.floor(yRatio*event.getY()-((ib.getHeight() - mutableBitmap.getHeight())/2));
            }