我想知道方法什么时候
protected void onDraw(final Canvas canvas) {}
将被调用。我问的是控制流。这个类的构造函数是从其他类调用的。当控件来到构造函数时,它会简单地调用这个类中的所有方法吗?
此外,我想在触摸和移动绘图图像时进行一些绘图。因为我使用了onTouchEvent(MotionEvent事件)。但我不知道如何在onTouch 中进行编码后调用onDraw。那我是否会更改一些坐标值如何调用onDraw来重绘图像?< / p>
有人可以帮忙吗?
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);
}
}
答案 0 :(得分: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。