@override与android中的onDraw导致错误

时间:2013-03-04 22:53:20

标签: java android override ondraw

我无法弄清楚为什么我得到一个类型为DrawStuffActivity.MyView的方法OnDraw(Canvas)必须覆盖或实现超类型方法“错误,任何帮助将不胜感激

我的项目设置为java 1.6,API设置为4.0.3(15)

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class DrawStuffActivity extends Activity implements OnTouchListener
{
    MyView myView;
    int numberOfFingers = 0; 
    float oldX[] = new float[2],
            oldY[] = new float[2];
    Rect rectangle = new Rect(0, 0, 100, 100);
    DisplayMetrics metrics = new DisplayMetrics();

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        myView = new MyView(this);
        setContentView(myView);
        myView.setOnTouchListener(this);

        getWindowManager().getDefaultDisplay().getMetrics(metrics);
    }


    @Override
    public boolean onTouch(View view, MotionEvent event) 
    {
        switch (event.getActionMasked() )
        {
        case MotionEvent.ACTION_DOWN:
            numberOfFingers = 1;
            oldX[0] = event.getX(0);
            oldY[0] = event.getY(0);
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            numberOfFingers = 2;
            oldX[1] = event.getX(1);
            oldY[1] = event.getY(1);
            break; 
        case MotionEvent.ACTION_MOVE:
            handleMove(event);
            break;
        case MotionEvent.ACTION_POINTER_UP:
        case MotionEvent.ACTION_UP:
            numberOfFingers--;
            break;
        }

        view.invalidate();
        return true;
    }


    float newX[] = new float[2], newY[] = new float[2];
    int xChange[] = new int[2], yChange[] = new int[2];

    int diffX, diffY;
    int newLeft = rectangle.left, 
            newTop = rectangle.top, 
            newRight = rectangle.right,
            newBottom = rectangle.bottom;




    void handleMove(MotionEvent event)
    {
        newX[0] = Math.round(event.getX(0) );
        newY[0] = Math.round(event.getY(0) );
        xChange[0] = Math.round(newX[0] - oldX[0] );
        yChange[0] = Math.round(newY[0] - oldY[0] );
        oldX[0] = newX[0];
        oldY[0] = newY[0];

        switch (numberOfFingers)
        {
        case 1:
            newLeft = rectangle.left + xChange[0];
            newTop = rectangle.top + yChange[0];
            newRight = rectangle.right + xChange[0];
            newBottom = rectangle.bottom + yChange[0];

            if (newLeft < 0 || newRight > metrics.widthPixels)
            {
                newLeft = rectangle.left;
                newRight = rectangle.right;
            }
            if (newTop > 0 || newTop > metrics.heightPixels)
            {
                newTop = rectangle.top;
                newBottom = rectangle.bottom;
            }

            rectangle = new Rect(newLeft, newTop, newRight, newBottom);

            break;

        case 2:
            newX[1] = Math.round(event.getX(1) );
            newY[1] = Math.round(event.getY(1) );

            diffX = Math.abs(Math.round(newX[1] - newX[0])) - Math.abs(Math.round(oldX[1] - oldX[0]));
            diffY = Math.abs(Math.round(newY[1] - newY[0])) - Math.abs(Math.round(oldY[1] - oldY[0]));

            oldX[1] = newX[1];
            oldY[1] = newY[1];

            newLeft = rectangle.left - diffX / 2;
            newTop = rectangle.top - diffY / 2;
            newRight = rectangle.right - diffX / 2;
            newBottom = rectangle.bottom - diffY / 2;
            rectangle = new Rect(newLeft, newTop, newRight, newBottom);

            break;
        }



    }

    class MyView extends View
    {
        Paint whitePaint = new Paint();

        MyView(Context context)
        {
            super(context);
            whitePaint.setColor(Color.WHITE);
        }

        @Override
        public void OnDraw(Canvas canvas) 
        {
            canvas.drawRect(rectangle, whitePaint);
        }
    }
}

1 个答案:

答案 0 :(得分:4)

onDraw()大写为OnDraw()。将其更改为onDraw()。方法名称应始终以小写字母开头。