gpt4 book ai didi

java - @override 在 android 中使用 onDraw 导致错误

转载 作者:行者123 更新时间:2023-12-01 18:52:49 26 4
gpt4 key购买 nike

我不明白为什么我收到“DrawStuffActivity.MyView 类型的方法 OnDraw(Canvas) 必须重写或实现父类(super class)型方法”错误,任何帮助将不胜感激

我的项目设置为 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);
}
}
}

最佳答案

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

关于java - @override 在 android 中使用 onDraw 导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15212724/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com