Question

Im making an application for painting on a bitmap, and its been asked several times before on SO like here and here

I used them inside my app and none seemed to work , here's the code i have used:

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

    }

}

Now, as i draw the path by touching on the screen, it just draws itself elsewhere, also it only happens in the images with have been scaled by android itself, not on images which are small enough to fit without scaling , please look into it and help me out

EDIT: I've followed this answer and this is my code after implementing, but theres no path now....

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

    }

}

Thanks, Jeet

Was it helpful?

Solution

Okay , i have found the answer by tinkering by myself, and I hope its useful to others

This are the steps i followed

1) Calculate original width and scaled width 2)find the scale ratio 3)multiply the scaled ratio to the events' x and y coordinate, and you can get the desired co-ordinates...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top