无法重写 onMeasure 方法

发布于 2024-10-15 07:19:51 字数 1521 浏览 4 评论 0原文

我需要重写 onMeasure 方法,但在尝试重写时出现错误: DrawLnClass.DrawLn 类型的方法 OnMeasure(int, int) 必须重写超类方法

这是我的代码:

public class DrawLnClass extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(new DrawLn(this));
    }
    public class DrawLn extends View {

        Paint _paint;
        int _height;
        int _width;
        Bitmap _bitmap;
        Canvas _canvas;
        Point[] xLine;
        Point[] yLine;

        public DrawLn(Context context) {
            super(context);
            _paint = new Paint();
            _paint.setColor(Color.CYAN);
            _paint.setStyle(Paint.Style.STROKE);
        }

        @Override
        protected void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            _width = View.MeasureSpec.getSize(widthMeasureSpec);
            _height = View.MeasureSpec.getSize(heightMeasureSpec);

            Toast.makeText(getContext(), "Width = " + _width, Toast.LENGTH_SHORT).show();
            Toast.makeText(getContext(), "Height = " + _height, Toast.LENGTH_SHORT).show();

            setMeasuredDimension(_width, _height);  

            //_bitmap = Bitmap.createBitmap(_width, _height, Bitmap.Config.ARGB_8888);
            //_canvas = new Canvas(_bitmap);

            //calcLinePlacements();
            //drawBoard();

        }

I need to override onMeasure method but when trying to override I get error:
The method OnMeasure(int, int) of type DrawLnClass.DrawLn must override a superclass method

Here is my code:

public class DrawLnClass extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(new DrawLn(this));
    }
    public class DrawLn extends View {

        Paint _paint;
        int _height;
        int _width;
        Bitmap _bitmap;
        Canvas _canvas;
        Point[] xLine;
        Point[] yLine;

        public DrawLn(Context context) {
            super(context);
            _paint = new Paint();
            _paint.setColor(Color.CYAN);
            _paint.setStyle(Paint.Style.STROKE);
        }

        @Override
        protected void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            _width = View.MeasureSpec.getSize(widthMeasureSpec);
            _height = View.MeasureSpec.getSize(heightMeasureSpec);

            Toast.makeText(getContext(), "Width = " + _width, Toast.LENGTH_SHORT).show();
            Toast.makeText(getContext(), "Height = " + _height, Toast.LENGTH_SHORT).show();

            setMeasuredDimension(_width, _height);  

            //_bitmap = Bitmap.createBitmap(_width, _height, Bitmap.Config.ARGB_8888);
            //_canvas = new Canvas(_bitmap);

            //calcLinePlacements();
            //drawBoard();

        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日裸衫吸 2024-10-22 07:19:51

您将第一个 O 大写,它应该是 onMeasure 而不是 OnMeasure。

我打赌你很高兴你使用了@Override,否则你不会意识到该方法没有正确覆盖。

You capitalised the first O, it should be onMeasure not OnMeasure.

I bet you are glad you used @Override though, otherwise you wouldn't have realised the method wasn't correctly overriding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文