Reputation: 387
I would like to know when will the method
protected void onDraw(final Canvas canvas) {}
will be called. I am asking about the control flow.constructor of this class is called from other class.When control comes to the constructor will it simply call all methods in this class??
Also i want to do some drawing when the draw image is touched and moved. for that i used onTouchEvent(MotionEvent event).But i dont know how to invoke onDraw after i do some coding in onTouch.That is i do change some coordinate values how will call onDraw to redraw image?
Can anyone help?
public class DrawView extends View {
Paint paint = new Paint();
public DrawView(Context context) {
// TODO Auto-generated constructor stub
super(context);
}
@Override
protected void onDraw(final Canvas canvas) {
// TODO Auto-generated method stub
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
canvas.drawRect(30, 350, 50, 400, paint);
super.onDraw(canvas);
// some other drawings
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN://some code
break;
case MotionEvent.ACTION_MOVE://some code
break;
case MotionEvent.ACTION_UP://some code
break;
default: break;
}
return super.onTouchEvent(event);
}
}
Upvotes: 0
Views: 413
Reputation: 11359
/** * @author rajeshcp */
public class SimpleDrag extends View {
private Paint mPaint;
private Rect mRect;
/**
* @param context
* @return of type SimpleDrag
* Constructor function
* @since Feb 19, 2013
* @author rajeshcp
*/
public SimpleDrag(Context context) {
super(context);
init();
}
/**
* @param context
* @param attrs
* @return of type SimpleDrag
* Constructor function
* @since Feb 19, 2013
* @author rajeshcp
*/
public SimpleDrag(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
/**
* @param context
* @param attrs
* @param defStyle
* @return of type SimpleDrag
* Constructor function
* @since Feb 19, 2013
* @author rajeshcp
*/
public SimpleDrag(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
/* (non-Javadoc)
* @see android.view.View#onDraw(android.graphics.Canvas)
* @since Feb 19, 2013
* @author rajeshcp
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.BLUE, PorterDuff.Mode.CLEAR);
if( mRect != null )
{
mPaint.setColor(Color.RED);
canvas.drawRect(mRect, mPaint);
}
}
private void init()
{
mRect = new Rect(0, 0, 50, 50);
mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG | Paint.ANTI_ALIAS_FLAG);
}
private Point mTouchPoint;
/* (non-Javadoc)
* @see android.view.View#onTouchEvent(android.view.MotionEvent)
* @since Feb 19, 2013
* @author rajeshcp
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
final int action = event.getAction();
if( action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_DOWN)
{
mTouchPoint = new Point((int)event.getX(), (int)event.getY());
if( !mRect.contains(mTouchPoint.x, mTouchPoint.y) )
{
return false;
}
}
if( action == MotionEvent.ACTION_MOVE )
{
final Point curretPoint = new Point((int)event.getX(), (int)event.getY());
int xMoved = curretPoint.x - mTouchPoint.x;
int yMoved = curretPoint.y - mTouchPoint.y;
mRect.set(mRect.left + xMoved, mRect.top + yMoved, mRect.right + xMoved, mRect.bottom + yMoved);
mTouchPoint = curretPoint;
invalidate();
}
return true;
}
}
Call Invalidate when ever you want onDraw method to get called.
Upvotes: 1