我使用下面給出的代碼繪製了基於用戶手指移動的多邊形路徑。更改已繪製的多邊形路徑的顏色
paint = new Paint();
strokePaint = new Paint();
//paint.setColor(Color.RED);
paint.setARGB(125, 255, 0, 0);
paint.setStyle(Style.FILL);
paint.setPathEffect(new DashPathEffect(new float[] {10,20}, 0));
paint.setStrokeWidth(5);
paint.setAntiAlias(true);
wallpath = new Path();
和onDraw有我使用這個代碼來繪製圖
wallpath.lineTo(endPoint.x, endPoint.y);
canvas.drawPath(wallpath, paint);
上面的代碼工作正常。但雙擊我想改變填充顏色。爲此,我正在使用此代碼
paint.setARGB(125, 225, 0, 0);
paint.setStyle(Style.FILL);
paint.setPathEffect(new DashPathEffect(new float[] {10,20}, 0));
paint.setStrokeWidth(5);
paint.setAntiAlias(true);
invalidate();
但它似乎並沒有工作。我怎樣才能正確地做到這一點?參考
public class HotspotView extends View
{
private Paint paint,strokePaint;
private PointF startPoint, endPoint;
private boolean isDrawing,isFinished,isAnimating,isRecording,isRedrawing;
private Path wallpath;
private ArrayList<PointF> points;
private RectF rectF;
private CurlView curlView;
public int imageWidth;
private String fileName;
private AudioRecorder audioRecorder;
private GestureDetector gestureDetector;
public static int LONG_PRESS_TIME = 500; // Time in miliseconds
private AudioPlayer player;
final Handler _handler = new Handler();
Runnable _longPressed = new Runnable() {
public void run() {
Log.i("info","LongPress");
isRecording = true;
isDrawing = false;
isRedrawing = true;
///////////////////******************///////////////////////
//paint = new Paint();
//strokePaint = new Paint();
//paint.setColor(Color.RED);
paint.setARGB(125, 225, 0, 0);
paint.setStyle(Style.FILL);
paint.setPathEffect(new DashPathEffect(new float[] {10,20}, 0));
paint.setStrokeWidth(5);
paint.setAntiAlias(true);
invalidate();
//////////////////*****************////////////////////////
audioRecorder = new AudioRecorder(fileName);
setFileName();
audioRecorder.startRecording(fileName);
}
};
public HotspotView(Context context)
{
super(context);
init();
gestureDetector = new GestureDetector(context, new GestureListener());
}
public HotspotView(Context context, AttributeSet attrs)
{
super(context, attrs);
init();
gestureDetector = new GestureDetector(context, new GestureListener());
}
public HotspotView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
gestureDetector = new GestureDetector(context, new GestureListener());
}
private void init()
{
isRecording = false;
isRedrawing = false;
paint = new Paint();
strokePaint = new Paint();
//paint.setColor(Color.RED);
paint.setARGB(125, 255, 0, 0);
paint.setStyle(Style.FILL);
paint.setPathEffect(new DashPathEffect(new float[] {10,20}, 0));
paint.setStrokeWidth(5);
paint.setAntiAlias(true);
wallpath = new Path();
points = new ArrayList<PointF>();
rectF = new RectF();
rectF.set(-1.7883435f, 1.0f, 1.7883435f, -1.0f);
}
@Override
protected void onDraw(Canvas canvas)
{
if(isAnimating)
{
PointF point = this.translate(points.get(0));
if(wallpath == null)
{
wallpath = new Path();
}
wallpath.moveTo(point.x, point.y);
isDrawing = false;
isFinished = false;
for(int i=1;i<points.size();i++)
{
if(isRedrawing)
{
point = points.get(i);
}
else
{
point = this.translate(points.get(i));
}
wallpath.lineTo(point.x, point.y);
//Log.d("Points", "X = "+point.x);
//Log.d("Points", "Y = "+point.y);
canvas.drawPath(wallpath, paint);
}
if(isRedrawing)
{
point = points.get(0);
}
else
{
point = this.translate(points.get(0));
}
wallpath.lineTo(point.x, point.y);
canvas.drawPath(wallpath, paint);
isFinished = true;
}
else if(isDrawing)
{
//canvas.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y, paint);
wallpath.lineTo(endPoint.x, endPoint.y);
canvas.drawPath(wallpath, paint);
}
if(isFinished)
{
//wallpath.lineTo(endPoint.x, endPoint.y);
//canvas.drawPath(wallpath, strokePaint);
wallpath.close();
}
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
boolean result = gestureDetector.onTouchEvent(event);//return the double tap events
if(!isAnimating)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
isDrawing = true;
//wallpath.reset();
_handler.postDelayed(_longPressed, LONG_PRESS_TIME);
startPoint = new PointF(event.getX(), event.getY());
endPoint = new PointF();
endPoint.x = event.getX();
endPoint.y = event.getY();
wallpath.moveTo(endPoint.x,endPoint.y);
points.add(startPoint);
//invalidate();
break;
case MotionEvent.ACTION_MOVE:
PointF point = new PointF(event.getX(),event.getY());
endPoint.x = event.getX();
endPoint.y = event.getY();
double distance = Math.sqrt(Math.pow((endPoint.x - startPoint.x), 2)+ Math.pow(endPoint.y - startPoint.y,2));
if(distance >2)
{
_handler.removeCallbacks(_longPressed);
if(!isRecording)
{
if(isDrawing)
{
Log.d("Point", "X = "+(event.getX() - this.getLeft()));
Log.d("Point", "Y = "+(event.getY() - this.getTop()));
points.add(point);
invalidate();
}
}
}
break;
case MotionEvent.ACTION_UP:
_handler.removeCallbacks(_longPressed);
if(isRecording)
{
audioRecorder.stopRecording();
isRecording = false;
}
if(isDrawing)
{
endPoint.x = startPoint.x;//event.getX();
endPoint.y = startPoint.y;//event.getY();
strokePaint.setARGB(255, 255, 0, 0);
strokePaint.setStyle(Style.STROKE);
strokePaint.setPathEffect(new DashPathEffect(new float[] {5,10}, 0));
strokePaint.setStrokeWidth(5);
strokePaint.setAntiAlias(true);
isFinished = true;
invalidate();
//isDrawing = false;
}
break;
default:
break;
}
}
return result;
}
我已添加完整的代碼 – Zach
謝謝,這是與其中一個標誌的問題。我有新問題,你可以請檢查這個http://stackoverflow.com/questions/18512269/change-the-color-of-a-particular-closed-path-in-a-android-canvas – Zach