這可能是因爲我有一個漫長的一天,已經很晚了,但我似乎無法弄清楚我做錯了什麼。繪製多條線並記住它們的厚度
我只是希望能夠吸引我的手指的位置行,每行有不同的寬度/顏色等
每當ACTION_DOWN,都會激發我創建了一個新的Path
,並保持在ACTION_MOVE其追加。當ACTION_UP被觸發時,我把當前的Path
和Paint
放在我的HashMap
中,這樣保存哪個Path
使用哪個Paint
,對不對?
當我打電話setRadius(float radius)
從這個類我打電話paint.setStrokeWidth(radius)
外部,從而改變了當前Paint
的筆劃寬度。
但由於某種原因我每次打電話setStrokeWidth(radius)
全部我的Map.Entry的變化??因此導致每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);
}
}
在此先感謝!
哦,上帝,當然。謝謝!這解決了這個問題。 – James 2013-05-09 09:06:14
非常感謝這一點 - 我花了很多年試圖找出如何改變Paint對象的寬度。 – 2014-10-29 11:39:04