为什么我的canvas不在我的canvas方法中更新?(android java)

时间:2015-02-01 18:06:44

标签: java android canvas ondraw

所以我创建了一个绘画类,它有一个ontouchevent方法,我将矩形的xpos设置为触摸事件的xpos,但矩形不动!我怎么能纠正这个? 矩形类:

public class myRectangle{
public int xpos;
public int ypos;
public int size;
private Paint paint;
public myRectangle(){
    size = 40;
    paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.BLUE);
}
public void drawRectangle(Canvas c,int  x, int y){
    c.drawRect(x, y, size, size, paint);
}

}

和我的油漆观点:

public class Painting extends View {
myRectangle player;
float x;
float y;
public Painting(Context context) {
    super(context);
    player = new myRectangle();
    // TODO Auto-generated constructor stub
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    x = event.getX();
    y = event.getY();
    String s = Float.toString(event.getX());
    String c = Float.toString(event.getX());

    Log.d(c, s);
    return super.onTouchEvent(event);
}

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    player.drawRectangle(canvas, (int)x, (int)y);

}

} 如果需要,我也可以发布主要活动

1 个答案:

答案 0 :(得分:1)

唯一缺少的是invalidate()中的onTouchEvent来电。 invalidate()安排抽奖