所以我在繪圖應用中面臨這個奇怪的問題。Android在另一條路徑上繪製一條路徑的頂部
我從上到下和從左到右畫出了從1到8的這些行。
雖然我畫的線,它顯示爲它繪製所有其他敵後。每當我放開屏幕,它就會彈出一個有時,這似乎是完全隨機的。
我在任何時候都可以俯視一切來繪製其他任何東西?
我DrawView.java:
public class DrawView extends View implements OnTouchListener {
private Path path = new Path();
private Paint paint = new Paint();
private Map<Path, Paint> pathMap = new HashMap<Path, Paint>();
private boolean isScreenCleared = false;
public DrawView(Context context) {
super(context);
this.setOnTouchListener(this);
paint.setColor(Color.BLACK);
paint.setAntiAlias(true);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
}
@Override
public void onDraw(Canvas canvas) {
if (isScreenCleared) {
pathMap.clear();
canvas.drawColor(Color.WHITE);
isScreenCleared = false;
} else {
//Current line
canvas.drawPath(path, paint);
//All other lines
for (Map.Entry<Path, Paint> p : pathMap.entrySet()) {
canvas.drawPath(p.getKey(), p.getValue());
}
}
}
public boolean onTouch(View view, MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path = new Path();
path.reset();
path.moveTo(eventX, eventY);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(eventX, eventY);
break;
case MotionEvent.ACTION_UP:
Paint newPaint = new Paint();
newPaint.set(paint);
pathMap.put(path, newPaint);
break;
default:
return false;
}
invalidate();
return true;
}
public float getRadius() {
return paint.getStrokeWidth();
}
public void setRadius(float radius) {
paint.setStrokeWidth(radius);
}
public void setColor(int color) {
paint.setColor(color);
System.out.println("Color set to: " + color);
}
public void clearScreen() {
isScreenCleared = true;
invalidate();
}
}
我實例drawView函數在我的MainActivity這樣的:
Relative layout = (RelativeLayout) findViewById(R.id.drawscreen);
DrawView dv = new DrawView(layout.getContext());
除非你正在做的事情,你還沒有顯示,使用LinkedHashMap不是最好的容器選擇。這是一個比Vector或List更重的容器或... – spartygw 2013-05-14 13:25:01