【问题标题】:Bizarre touch coordinates奇怪的触摸坐标
【发布时间】:2013-07-21 12:47:00
【问题描述】:

我正在申请在位图上绘画,之前在 SO 上被问过好几次,比如 herehere

我在我的应用程序中使用了它们,但似乎都不起作用,这是我使用的代码:

public class EditActivity extends Activity implements OnTouchListener,
        OnClickListener {
    GestureDetector gd;
    View.OnTouchListener gl;
    RelativeLayout parent;
    ImageView im;
    Bitmap bmp;
    Bitmap alteredBitmap;
    Canvas canvas;
    Paint paint;
    Matrix matrix;
    Rect imageBounds;
    Path mPath;
    int dw;
    int dh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);
        parent = (RelativeLayout) findViewById(R.id.rl);


        matrix = new Matrix();
        parent.setBackgroundColor(Color.BLACK);
        gd = new GestureDetector(this, new MyGestureDetector());
        gl = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {

                float x = event.getX();
                float y = event.getY();

                int action = event.getAction();

                    switch (action) {

                    case MotionEvent.ACTION_DOWN:

                        mPath.moveTo(x,y);
                        im.invalidate();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        mPath.lineTo(x,y);
                        im.invalidate();
                        break;
                    case MotionEvent.ACTION_UP:
                        // canvas.drawPath(mPath, paint);
                        mPath.reset();
                        im.invalidate();
                        break;
                    case MotionEvent.ACTION_CANCEL:
                        break;
                    default:
                        break;
                    }
                    return gd.onTouchEvent(event);
                } else {
                    return gd.onTouchEvent(event);
                }
            }
        };

        im = (ImageView) findViewById(R.id.ImageView);

        Uri imageFileUri = Uri.parse(getIntent().getStringExtra("uri"));
        Display currentDisplay = getWindowManager().getDefaultDisplay();

        dw = currentDisplay.getWidth();

        dh = currentDisplay.getHeight();

        try {
            // Load up the image's dimensions not the image itself
            BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();

            bmpFactoryOptions.inJustDecodeBounds = false;
            bmp = BitmapFactory.decodeStream(getContentResolver()
                    .openInputStream(imageFileUri), null, bmpFactoryOptions);

            alteredBitmap = Bitmap.createBitmap(bmp.getWidth(),
                    bmp.getHeight(), bmp.getConfig());

            mPath = new Path();
            canvas = new Canvas(alteredBitmap);
            paint = new Paint(Paint.SUBPIXEL_TEXT_FLAG);
            paint.setFlags(Paint.DEV_KERN_TEXT_FLAG);
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(Color.WHITE);
            paint.setFilterBitmap(true);
            paint.setDither(true);
            paint.setAntiAlias(true);
            paint.setTextSize(40f);

            paint.setTypeface(getTypeFaceFromFile("Roboto-Light"));

            canvas.drawBitmap(bmp, matrix, paint);
            canvas.save();
            im.setImageBitmap(alteredBitmap);
            im.setOnClickListener(this);
            im.setOnTouchListener(gl);

        } catch (FileNotFoundException e) {
            Log.v("ERROR", e.toString());
        }

    }

    class MyGestureDetector extends SimpleOnGestureListener {

        @Override
        public boolean onDown(MotionEvent event) {

            return super.onDown(event);
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {

            return super.onSingleTapConfirmed(e);
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {

            return super.onDoubleTap(e);
        }

        @Override
        public void onLongPress(MotionEvent e) {

            super.onLongPress(e);
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {

            return super.onDoubleTapEvent(e);
        }

    }

}

现在,当我通过触摸屏幕来绘制路径时,它只是在其他地方绘制自己,而且它只发生在已由 android 本身缩放的图像中,而不是小到足以适应不缩放的图像,请调查一下,帮帮我

编辑: 我已经关注了这个answer,这是我实现后的代码,但现在没有路径....

        gl = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {

                float x = event.getX();
                float y = event.getY();


                float scaledImageOffsetX = x - imageBounds.left;
                float scaledImageOffsetY = y - imageBounds.top;

                float originalImageOffsetX = scaledImageOffsetX * widthRatio;
                float originalImageOffsetY = scaledImageOffsetY * heightRatio;
                int action = event.getAction();

                    switch (action) {

                    case MotionEvent.ACTION_DOWN:

                        mPath.moveTo(originalImageOffsetX,originalImageOffsetY);
                        im.invalidate();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        mPath.lineTo(originalImageOffsetX,originalImageOffsetY);
                        im.invalidate();
                        break;
                    case MotionEvent.ACTION_UP:
                        // canvas.drawPath(mPath, paint);
                        mPath.reset();
                        im.invalidate();
                        break;

                    default:
                        break;
                    }
                    return true;
                } 

            }
        };

        im = (ImageView) findViewById(R.id.ImageView);
        Uri imageFileUri = Uri.parse(getIntent().getStringExtra("uri"));
        Display currentDisplay = getWindowManager().getDefaultDisplay();
        dw = currentDisplay.getWidth();
        dh = currentDisplay.getHeight();

        try {

            BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();

            bmpFactoryOptions.inJustDecodeBounds = false;
            bmp = BitmapFactory.decodeStream(getContentResolver()
                    .openInputStream(imageFileUri), null, bmpFactoryOptions);

            alteredBitmap = Bitmap.createBitmap(bmp.getWidth(),
                    bmp.getHeight(), bmp.getConfig());

            mPath = new Path();
            canvas = new Canvas(alteredBitmap);
            paint = new Paint(Paint.SUBPIXEL_TEXT_FLAG);
            paint.setFlags(Paint.DEV_KERN_TEXT_FLAG);
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(Color.WHITE);
            paint.setFilterBitmap(true);
            paint.setDither(true);
            paint.setAntiAlias(true);
            paint.setTextSize(40f);

            paint.setTypeface(getTypeFaceFromFile("Roboto-Light"));

            canvas.drawBitmap(bmp, matrix, paint);
            canvas.save();
            im.setImageBitmap(alteredBitmap);
            Drawable drawable = im.getDrawable();
            imageBounds = drawable.getBounds();

            float intrinsicHeight = drawable.getIntrinsicHeight();
            float intrinsicWidth = drawable.getIntrinsicWidth();;

            float scaledHeight = imageBounds.height();
            float scaledWidth = imageBounds.width();

            heightRatio = intrinsicHeight / scaledHeight;
            widthRatio = intrinsicWidth / scaledWidth;
            im.setOnClickListener(this);
            im.setOnTouchListener(gl);

        } catch (FileNotFoundException e) {
            Log.v("ERROR", e.toString());
        }

    }

}

谢谢, 吉特

【问题讨论】:

  • 你需要按照这个问题的第一个答案中的描述来缩放坐标:stackoverflow.com/questions/4933612/…
  • 我关注了它,我也将它链接到我的问题第一行,但它不适用于我的情况
  • 另外,在那个答案中,设置 originalImageOffsetX 和 originalImageOffsetY 触摸时不显示任何内容
  • 您实际尝试了什么?您的问题几乎肯定是由于需要转换或缩放 TouchEvent 给出的坐标以与图像上的坐标对齐。
  • 检查我的编辑,我已经包含了我实现它的方式

标签: android bitmap bitmapfactory


【解决方案1】:

好的,我自己摸索找到了答案,希望对大家有用

这是我遵循的步骤

1) 计算原始宽度和缩放宽度 2)找到比例 3)将缩放比例乘以事件的x和y坐标,就可以得到想要的坐标...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 2011-03-23
    相关资源
    最近更新 更多