import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
public class DrawingActivity extends Activity {
private ArrayList _graphics = new ArrayList();
private Paint mPaint;
Handler myHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
myview.invalidate();
};
};
Myview myview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(new DrawingPanel(this));
mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(0xFFFFFF00);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(3);
myview = new Myview(this);
myview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
setContentView(myview);
myHandler = new Handler();
}
class Myview extends View {
public Myview(Context context) {
super(context);
}
Path path;
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
path = new Path();
path.moveTo(event.getX(), event.getY());
path.lineTo(event.getX(), event.getY());
_graphics.add(path);
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
path.lineTo(event.getX(), event.getY());
} else if (event.getAction() == MotionEvent.ACTION_UP) {
path.lineTo(event.getX(), event.getY());
}
invalidate();
return true;
}
@Override
protected void onDraw(Canvas canvas) {
for (Path path :_graphics) {
// canvas.drawPoint(graphic.x, graphic.y, mPaint);
canvas.drawPath(path, mPaint);
}
}
}
}
Here the compiler shows the error in that line Type mismatch: cannot convert from element type Object to Path ———- for (Path path :_graphics) { what problem occurs here
Your ArrayList is untyped, so even though you might only ever put objects of those type in there, the compiler doesn’t know it. You either need to use generics (if Java on Android supports it) e.g.
Or rewrite the loop so you can cast the elements as you get them from the list.
Good luck.