0

I am creating a POC on how to resize an imageView in android. I want to put 4 squares in the corner of the ImageView so I can drag the squares to resize. What should I use to get this result?

enter image description here

MMakati
  • 693
  • 1
  • 15
  • 33

2 Answers2

0
  1. Get the coordinate of the corners
  2. Draw rectangles at those coordinates with method like Canvas.drawRect(float left, float top, float right, float bottom, Paint paint)
  3. Get the coordinates of the touch point from onTouch()
  4. Resize the imageview with the new size

Get Coordinates of Touch Point

The class (suppose it is an Activity) should implements OnTouchListener, and register the listener to itself

    public class MyActivity extends Activity implements OnTouchListener {
    ...
        setOnTouchListener(MyActivity.this);
    ...

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getActionMasked();

        switch(action) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                // Get the coordinates
                int x = (int)event.getRawX();
                int y = (int)event.getRawY();
                // Resize the imageView
                resize();
            break;
            case MotionEvent.ACTION_CANCEL:
            break;
        }
        return true;
    }
}

Resize the ImageView

You can use ImageView.setMaxHeight(), ImageView.setMaxWidth() to resize the imageview

By the way, does the imageView you want to resize is in fact an image? In that case, using drawable might be a better idea

chinglun
  • 637
  • 5
  • 18
  • Then how can I resize the image? – MMakati Nov 10 '13 at 09:12
  • Oh my bad, I missed that part. Hold on. – chinglun Nov 10 '13 at 09:15
  • What is the `Paint paint` all about? – MMakati Nov 10 '13 at 09:28
  • The class Paint enables you to set the color, transparency, etc of your drawing --http://developer.android.com/reference/android/graphics/Paint.html – chinglun Nov 10 '13 at 09:32
  • Do I need to use onDraw method to use the `canvas.drawRect()`? – MMakati Nov 10 '13 at 09:38
  • You can, it's not necessary – chinglun Nov 10 '13 at 09:39
  • but how? I am putting up that canvas.drawRect() inside the ACTION_UP. Sorry I am a newbie in android development. – MMakati Nov 10 '13 at 09:48
  • Create a SurfaceView, do things like draw the rectangles and image on it, then post it to your view These links might help: http://wptrafficanalyzer.in/blog/android-drawing-circle-at-the-touched-point-of-surfaceview/ http://stackoverflow.com/questions/12912102/how-to-draw-image-on-surfaceview-android http://stackoverflow.com/questions/14139177/android-surfaceview-drawing Of course, Developer's Guide is always your second best friend http://developer.android.com/guide/topics/graphics/2d-graphics.html – chinglun Nov 10 '13 at 10:02
0

Maybe an overlay is what you need. Create a 9-patch drawable with the squares and corners and put that layer on top of your imageview.

DKIT
  • 3,471
  • 2
  • 20
  • 24