I am working on a drawing app and extending View
, and would like to implements OnTouchListener. Codes as follows:
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
}
onDraw:
@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();
}
onTouchEvent:
@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;
}
However, I really scratch my head and dont know why for
ERROR1: implements OnTouchListener reports red underlines says OnTouchListener cannot be resolved to a type
, and suggest import android.view.View
. But I tried click to import by it still unwilling to be imported, and I see the import list it is already there
ERROR2: The method setOnTouchListener(View.OnTouchListener) in the type View is not applicable for the arguments (DoodleView)
, should be because of ERROR1
ERROR3: The method onTouchEvent(View, MotionEvent) of type DoodleView must override or implement a supertype method
, should be also be because of ERROR1.
Are there anyone know what is happening? I got stuck here without move! Thanks a lot!
It seems like a simple import issue. Try to add
import android.view.View
if not already present in your class and change class declaration as follows:
public class DoodleView extends View implements View.OnTouchListener {
....
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.