My app is a paint app , where the user paints on the screen . I added the color selection option so that user can select colour of the paint brush. I added undo button , so I had to add arrays to store the paint paths and color array.
Everything works fine except that the color when I select does not add to the colorarray immediately, it applies the color to the path after one path completes.
To understand clearly, take a look at my code.
public class DrawingPanel extends View implements OnTouchListener {
private Canvas mCanvas;
private Path mPath;
private Paint mPaint, circlePaint, outercirclePaint;
public static Bitmap defaultbitmap;
List<Integer> colorarray = new ArrayList<Integer>();
public static ArrayList<Path> paths = new ArrayList<Path>();
public static int color=0xFFFF0000;
private int[] mColors;
private float xleft, xright, xtop, xbottom;
public DrawingPanel(Context context, AttributeSet attrs) {
super(context, attrs);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
circlePaint = new Paint();
mPaint = new Paint();
outercirclePaint = new Paint();
outercirclePaint.setAntiAlias(true);
circlePaint.setAntiAlias(true);
mPaint.setAntiAlias(true);
outercirclePaint.setColor(0x44FFF000);
circlePaint.setColor(0xAADD5522);
outercirclePaint.setStyle(Paint.Style.STROKE);
circlePaint.setStyle(Paint.Style.FILL);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(6);
outercirclePaint.setStrokeWidth(6);
mCanvas = new Canvas();
mPath = new Path();
colorarray.add(color);
}
public void colorChanged(int color) {
mPaint.setColor(color);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
int w = defaultbitmap.getWidth();
int h = defaultbitmap.getHeight();
Bitmap resultBmp = null;
int widthofBitMap = MainActivity.Width ;
int heightofBitMap = widthofBitMap*h/w;
resultBmp = Bitmap.createScaledBitmap(defaultbitmap, widthofBitMap, heightofBitMap, true);
canvas.drawBitmap(resultBmp, (MainActivity.Width-widthofBitMap)/2,(MainActivity.Height-heightofBitMap)/2, mPaint);
for( int i=0;i<paths.size();i++){
mPaint.setColor(colorarray.get(i));
System.out.println("colorarray size is"+colorarray.size());
System.out.println("paths size is"+paths.size());
canvas.drawPath(paths.get(i), mPaint);
}
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 0;
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
}
}
public void undo(){
if (paths.size() > 0) {
paths.remove(paths.size() - 1);
colorarray.remove(colorarray.size()-1);
invalidate();
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath = new Path();
paths.add(mPath);
}
public void colorshit(){
if(MainActivity.buttons==1)
{
color=0xFFFF0000;
}
else if(MainActivity.buttons==2)
{
color=0xFFFF00FF;
}
else if(MainActivity.buttons==3)
{
color=0xFF0000FF;
}
else if(MainActivity.buttons==4)
{
color=0xFF000000;
}
else if(MainActivity.buttons==5)
{
color=0xFF00FF00;
}
else if(MainActivity.buttons==6)
{
color=0xFFFFFF00;
}
colorarray.add(color);
}
@Override
public boolean onTouch(View arg0, MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
colorshit();
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
}
I am adding the color to color arrays in ONtouch action down, there might some other way to add color to colorarray other than in ontouch. Please suggest me some ideas.