Brejuro
Brejuro

Reputation: 3541

Add drawing/painting functionality to my activity/fragments

I'm confused on how to add drawing/painting functionality to my existing system. I have an Activity that controls 3 Fragments through a Tab Bar and I want to be able to add drawing/painting on the Fragment screens in my app. My Fragments already have their own views/layout with checkboxes, text views, etc.

Does anyone know how I could go about doing this?

Upvotes: 1

Views: 291

Answers (1)

Aritra Roy
Aritra Roy

Reputation: 15615

Yes, you can. And there are some libraries which can allow you to do that easily.

Please try these,

I am sure this will help you. You can either choose to use these libraries directly in your app or you can learn from their source code and implement your own.

But I am sure these resources will be enough to solve your problem.

EDIT

Taking specific example of the CanvasView,

Pu this in your fragment XML,

<com.android.graphics.CanvasView
    android:id="@+id/canvas"
    android:layout_width="280dp"
    android:layout_height="280dp"
    android:layout_gravity="center_horizontal" />

Reference the view in code like this,

public class DrawActivity extends Activity {
    private CanvasView canvas = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_draw);

        // Create the instance of CanvasView
        this.canvas = (CanvasView)this.findViewById(R.id.canvas);
    }

    // ....
}

There are various customization's you can try like,

this.canvas.setDrawer(CanvasView.Drawer.PEN);               // Use Pen Tool
this.canvas.setDrawer(CanvasView.Drawer.LINE);              // Draw Line
this.canvas.setDrawer(CanvasView.Drawer.RECTANGLE);         // Draw Rectangle
this.canvas.setDrawer(CanvasView.Drawer.CIRCLE);            // Draw Circle
this.canvas.setDrawer(CanvasView.Drawer.ELLIPSE);           // Draw Ellipse (Oval)
this.canvas.setDrawer(CanvasView.Drawer.QUADRATIC_BEZIER);  // Draw Quadratic Bezier

And these,

this.canvas.setPaintStyle(Paint.Style.STROKE);
this.canvas.setPaintStyle(Paint.Style.FILL);
this.canvas.setPaintStyle(Paint.Style.FILL_AND_STROKE);

There are lots of other cuztomizations you can try. Just check the resource I have provided you.

Hope it helps.

Upvotes: 3

Related Questions