可能是因为我度过了漫长的一天,而且已经很晚了,但我似乎无法弄清楚我做错了什么。
我只想能够在我的手指位置画线,每条线具有不同的宽度/颜色等。
每当 ACTION_DOWN 被触发时,我都会创建一个新的Path
并继续将其附加到 ACTION_MOVE。当 ACTION_UP 被解雇时,我将当前的Path
andPaint
放入 my 中HashMap
,从而保存 which Path
used which Paint
,对吗?
当我setRadius(float radius)
从这个类之外调用时,我调用paint.setStrokeWidth(radius)
,从而改变了当前Paint
的笔画宽度。
但是由于某种原因,每次我调用我的 Map.Entry 的setStrokeWidth(radius)
所有更改时?从而导致Every Paint
的笔画宽度成为“新”笔画宽度,并用新的笔画宽度重新绘制所有内容。
这可能很明显,但我似乎找不到错误。
这是我的 DrawView.java。
package com.example.paintandprint;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class DrawView extends View implements OnTouchListener {
Path path = new Path();
Paint paint = new Paint();
Map<Path, Paint> pathMap = new HashMap<Path, Paint>();
public DrawView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
paint.setColor(Color.BLACK);
paint.setAntiAlias(true);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
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:
pathMap.put(path, paint);
break;
default:
return false;
}
invalidate();
return true;
}
public float getRadius() {
return paint.getStrokeWidth();
}
public void setRadius(float radius) {
paint.setStrokeWidth(radius);
}
}
提前致谢!