开发者

onDraw operation is not putting circle where I expect it

I am trying to do a simple GUI interaction - touch the screen and have a red ball move there from its previous position. Everything works great, except that my ball appears at an offset from where I intended. The offset is directly related to the where I drew the original ball. It's as if the ball view includes that offset as part of its size. For example, if I draw the original at X=100, Y=100 with radius 25, I later have to subtrace 100 from each of my getX and getY return values to get it placed in the proper place. I'm sure this isn't normal, but haven't been able to spot my problem. Can someone help? The code is

public LinearLayout my_canvas;
public Ball ball1;

public class Ball extends View {
    private final float x;
    private final float y;
    private final int r;
    private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public Ball(Context context, float x, float y, int r) {
        super(context);
        mPaint.setColor(0xFFFF0000);
        LinearLayout.LayoutParams tlp =
            new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT );
        this.setLayoutParams(tlp);
        this.x = x;
        this.y = y;
        this.r = r;
        Log.v("BookOne", "ball constructor at "+x+","+y);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(x, y, r, mPaint);
       开发者_Python百科 my_canvas.invalidate();
    }
}   

is within my main application. Followed by

    my_canvas = (LinearLayout) findViewById(R.id.my_canvas); 
    my_canvas.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent e) {
                Log.v("BookOne", "Entered onTouch for canvas");
                float x = e.getX();
                float y = e.getY();

                Log.v("BookOne", "set coordinates");
                ball1.setX(x);
                ball1.setY(y);
                ball1.invalidate();
                //my_canvas.invalidate();

                Log.v("BookOne", "get ranges");
                int r = v.getRight();
                int l = v.getLeft();
                int t = v.getTop();
                int b = v.getBottom();
                int w = v.getWidth();
                int h = v.getHeight();
                Log.v("BookOne", "Ball moving to "+x+","+y);
                log_view_dimensions("ball1 dimensions", ball1);
                log_view_dimensions("View v dimensions", v);
                log_view_dimensions("my_canvas dimensions", my_canvas);
                Log.v("BookOne", "about to return FALSE");
                return(false);
            }
    });

    ball1 = new Ball(my_canvas.getContext(), 100, 100, 25);
    my_canvas.addView(ball1);
    Log.v("BookOne", "ball1 added");

The X and Y values I am getting from getX and getY appear correct, but the ball is being redraw 100 pixels away.

FYI, here's the dump from LogCat:

07-26 20:54:43.830: VERBOSE/BookOne(5463): Entered onTouch for canvas 07-26 20:54:43.830: VERBOSE/BookOne(5463): set coordinates 07-26 20:54:43.830: VERBOSE/BookOne(5463): get ranges 07-26 20:54:43.830: VERBOSE/BookOne(5463): Ball moving to 337.0,262.5 07-26 20:54:43.830: VERBOSE/BookOne(5463): ball1 dimensions l=0; r=800; t=0; b=536 07-26 20:54:43.830: VERBOSE/BookOne(5463): ball1 dimensions w=800; h=536 07-26 20:54:43.830: VERBOSE/BookOne(5463): View v dimensions l=0; r=800; t=584; b=1120 07-26 20:54:43.830: VERBOSE/BookOne(5463): View v dimensions w=800; h=536 07-26 20:54:43.830: VERBOSE/BookOne(5463): my_canvas dimensions l=0; r=800; t=584; b=1120 07-26 20:54:43.830: VERBOSE/BookOne(5463): my_canvas dimensions w=800; h=536 07-26 20:54:43.830: VERBOSE/BookOne(5463): about to return FALSE

Notice the dimensions on ball1. What are the implications?

Many thanks in advance for any help.

Mike


The possible reason for the offset of your circle is that you have not overridden the onMeasure() method. If an implementation of onMeasure is not provided then by default Android will set the size to 100 X 100. That's the reason you end up subtracting 100 from x and y to get the ball at the correct location.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜