我有个问题。目标是在画布上绘制笔画并获取它,但是当我让 doubleTap 绘制的笔画采用我选择的颜色时,我不希望第一个笔画的颜色仍然与第二个笔画相同另一种颜色。
这是我的代码:
package com.example.guiao_2_1;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.v4.view.GestureDetectorCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class NewView extends View implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener{
private Paint paint = new Paint();
private Path path = new Path();
private DrawingPath newPath;
private GestureDetectorCompat mDetector;
private static final String DEBUG_TAG = "Gestures";
private DrawingPath oldPath;
private ArrayList<DrawingPath> olds = new ArrayList<DrawingPath>();
private ArrayList<DrawingPath> news = new ArrayList<DrawingPath>();
public NewView(Context context, AttributeSet attrs) {
super(context, attrs);
paint.setAntiAlias(true);
paint.setStrokeWidth(6f);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
mDetector = new GestureDetectorCompat(context, this);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
for (DrawingPath p : olds){
paint.setColor(p.color);
paint.setStrokeWidth(p.width);
canvas.drawPath(p, paint);
}
for (DrawingPath j : news){
paint.setColor(j.color);
paint.setStrokeWidth(j.width);
canvas.drawPath(j, paint);
}
//canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
this.mDetector.onTouchEvent(event);
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(eventX, eventY);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(eventX, eventY);
break;
case MotionEvent.ACTION_UP:
// nothing to do
break;
default:
return false;
}
// Schedules a repaint.
invalidate();
return true;
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
// TODO Auto-generated method stub
float width = paint.getStrokeWidth();
int color = paint.getColor();
oldPath = new DrawingPath(width, color);
olds.add(oldPath);
newPath = new DrawingPath(8, Color.GREEN);
news.add(newPath);
//int numColor = paint.getColor();
//String color = Integer.toHexString(numColor);
//Toast.makeText(getContext(), color, Toast.LENGTH_SHORT).show();
Log.d(DEBUG_TAG, "onDoubleTap: " + e.toString());
return true;
}
@Override
public boolean onDoubleTapEvent(MotionEvent event) {
// TODO Auto-generated method stub
Log.d(DEBUG_TAG, "onDoubleTap: " + event.toString());
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
private class DrawingPath extends Path {
public float width;
public int color;
public DrawingPath(float w, int c){
width = w;
color = c;
}
}
}
请告诉我我在做什么,请。