draggable box to crop image

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

draggable box to crop image

Postby aneuryzma » Sun Jan 17, 2010 8:38 pm

Hi,

I would like to have a draggable box to crop an image on android.
How can I draw it ?

thanks
aneuryzma
Developer
Developer
 
Posts: 28
Joined: Wed Jan 06, 2010 10:46 am

Top

Postby serkan.dogantekin » Mon Jan 18, 2010 10:51 am

You can use View's "setOnTouchListener" and "onDraw" methods.

First define a OnTouchListener and bind it to your view (view shows the image):
view.setOnTouchListener(new View.OnTouchListener() {
int startX;
int startY;
int currentX;
int currentY;
int endX;
int endY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = (int)event.getX();
startY = (int)event.getY();
return true;
case MotionEvent.ACTION_MOVE:
currentX = (int)event.getX();
currentY = (int)event.getY();
// set startX , startY, currentX , currentY fields on your view so that you can use it in drawing
view.invalidate(); // call invalidate or postInvalidate to force redraw on view
return true;
case MotionEvent.ACTION_UP:
endX = (int)event.getX();
endY = (int)event.getY();
// set startX , startY, currentX , currentY fields on your view so that you can use it in drawing
view.invalidate(); // call invalidate or postInvalidate to force redraw on view
return true;
}
return false;
}
});


Then on your view's onDraw method draw a rectange related to x,y values that you provided through ontouchListener:

public void onDraw(Canvas canvas) {
// if it is a custom view you should have something like canvas.drawBitmap to draw image
canvas.drawRect(......);
}
Last edited by serkan.dogantekin on Mon Jan 18, 2010 11:00 am, edited 1 time in total.
serkan.dogantekin
Developer
Developer
 
Posts: 27
Joined: Thu Jun 04, 2009 6:12 pm
Location: Istanbul/Turkey

Postby aneuryzma » Mon Jan 18, 2010 10:57 am

thanks
aneuryzma
Developer
Developer
 
Posts: 28
Joined: Wed Jan 06, 2010 10:46 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Majestic-12 [Bot] and 8 guests