0

我想知道方法什么时候

protected void onDraw(final Canvas canvas) {}

将被调用。我在问这个类的控制流。这个类的构造函数是从其他类调用的。当控制权来到构造函数时,它会简单地调用这个类中的所有方法吗?

当绘制图像被触摸和移动时,我也想做一些绘图。为此,我使用了 onTouchEvent(MotionEvent 事件)。但是我在 onTouch 中进行了一些编码后,我不知道如何调用 onDraw。也就是说,我确实更改了一些坐标值,如何调用 onDraw 来重绘图像?

任何人都可以帮忙吗?

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

1 回答 1

1

/** * @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;
}



}

当您希望调用 onDraw 方法时调用 Invalidate。

于 2013-02-19T10:01:44.170 回答