我正在开发一个绘图应用程序并进行扩展View
,并希望实现 OnTouchListener。代码如下:
public class DoodleView extends View implements OnTouchListener //ERROR1
{
private Bitmap bitmap; // drawing area for display or saving
private Canvas bitmapCanvas; // used to draw on bitmap
private Paint paintScreen; // use to draw bitmap onto screen
private Paint paintLine; // used to draw lines onto bitmap
private Path mPath;
private Paint mPaint, circlePaint, outercirclePaint;
private ArrayList<Path> paths = new ArrayList<Path>();
private ArrayList<Path> undonePaths = new ArrayList<Path>();
private float xleft,xright,xtop,xbottom;
private float mX, mY;
// DoodleView constructor initializes the DoodleView
public DoodleView(Context context)
{
super(context); // pass context to View's constructor
this.context_new=context;
this.setOnTouchListener(this); //ERROR2
paintScreen = new Paint(); // used to display bitmap onto screen
// set the initial display settings for the painted line
paintLine = new Paint();
paintLine.setAntiAlias(true); // smooth edges of drawn line
paintLine.setColor(Color.BLACK); // default color is black
paintLine.setStyle(Paint.Style.STROKE); // solid line
paintLine.setStrokeWidth(5); // set the default line width
paintLine.setStrokeCap(Paint.Cap.ROUND); // rounded line ends
mPath = new Path();
paths.add(mPath);
} // end DoodleView constructor
OnSizeChanged:
@Override
public void onSizeChanged(int w, int h, int oldW, int oldH)
{
super.onSizeChanged(w, h, oldW, oldH);
DoodlzViewWidth = w;
DoodlzViewHeight = h;
bitmap = Bitmap.createBitmap(getWidth(), DoodlzViewHeight, Bitmap.Config.ARGB_8888);
bitmapCanvas = new Canvas(bitmap);
bitmap.eraseColor(Color.WHITE); // erase the BitMap with white
}
绘制:
@Override
protected void onDraw(Canvas canvas)
{
canvas.drawBitmap(bitmap, 0, 0, paintScreen); // draw the background screen
// for each path currently being drawn
for (Path p : paths){canvas.drawPath(p, paintLine);}
Toast.makeText(getContext(), "ondraw", Toast.LENGTH_SHORT).show();
}
触摸事件:
@Override
public boolean onTouchEvent(View arg0, MotionEvent event) //ERROR3
{
float x = event.getX();
float y = event.getY();
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
touchStarted(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touchMoved(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touchEnded();
invalidate();
break;
}
return true;
}
但是,我真的很挠头,不知道为什么
ERROR1:实现 OnTouchListener 报告红色下划线表示
OnTouchListener cannot be resolved to a type
,并建议导入android.view.View
。但是我尝试点击导入它仍然不愿意被导入,我看到导入列表已经在那里了ERROR2:
The method setOnTouchListener(View.OnTouchListener) in the type View is not applicable for the arguments (DoodleView)
, 应该是因为 ERROR1ERROR3:
The method onTouchEvent(View, MotionEvent) of type DoodleView must override or implement a supertype method
,应该也是因为ERROR1。
有谁知道发生了什么?我被卡在这里一动不动!非常感谢!