15

I want to learn how can I create layers (like in photoshop) in my android application. I want to achieve one basic thing: when I add an image in my canvas, which will be some figure for example, I want to be able to paint the canvas, but the painting must not effect the lines of figure. And then I need to save that image on my Sd card.

Any suggestions/advice/examples? What can I use to achieve this?

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
Android-Droid
  • 14,365
  • 41
  • 114
  • 185

4 Answers4

1

The order how you paint makes layers. Create some stack of objects what you paint and then modify individual objects (painting in layer) or move them in this stack (changing layers). Use the same order to create your image to save.

Alex
  • 4,457
  • 2
  • 20
  • 59
1

Layers in Canvas.

    Bitmap bitmap=Bitmap.createBitmap(500,500, Bitmap.Config.ARGB_8888);
    Canvas canvas=new Canvas(bitmap);
    Paint paint_outer=new Paint();
    paint_outer.setColor(Color.parseColor("#DEB887"));
    paint_outer.setStrokeWidth(140);
    paint_outer.setStyle(Paint.Style.STROKE);
    canvas.drawCircle(250,250,150,paint_outer);

    Paint paint_inner=new Paint();
    ComposePathEffect pathEffect=new ComposePathEffect(new CornerPathEffect(40),new DiscretePathEffect(60f,25f));
    paint_inner.setPathEffect(pathEffect);
    paint_inner.setColor(Color.parseColor("#8B4513"));
    paint_inner.setStrokeWidth(90);
    paint_inner.setStyle(Paint.Style.STROKE);
    canvas.drawCircle(250,250,150,paint_inner);
    iv.setImageBitmap(bitmap);
0

Have a look at the various Xfermodes that you can apply to a Paint. PorterDuff.Mode offers some options like you get in Photoshop, such as, screen and multiply, lighten and darken. The AvoidXferMode may suit your needs more however.

techi.services
  • 8,473
  • 4
  • 39
  • 42
0

You can compose your layers in a FrameLayout. You might need some trickery to ensure touch events get passed to the correct layer.

Once you have android composing your views how you like, you can mFrameLayout.onDraw(Canvas c) to draw the entire thing to a canvas.

This article has some info on how to turn your viewgroup into a jpeg Image on canvas to JPEG file

Community
  • 1
  • 1
HaMMeReD
  • 2,440
  • 22
  • 29