clayton33
clayton33

Reputation: 4216

trying to draw some simple lines in android

i am trying to draw some lines via code on top of a background image to make a sort of graph-like thing, but i can't seem to find a method to do this, can anyone help?

*edit here is what i have so far, but i get a force close on the line Canvas canvas

*edit2 sorry for ignorance on my part, i am new to this, i've not used logcat before. i opened logcat and it appears that it might be a "immutable bitmap passed to canvas constructor". after some googling, i think this is getting to be beyond the scope of what i am capable of, i didn't realize drawing was this involved. thanks for the help anyways all.

package com.surreall;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas; 
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;

public class drawline extends Activity {
/** Called when the activity is first created. */

// load picture and create a canvas to draw onto


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);  
// set drawing colour
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.felt2);
Canvas canvas = new Canvas(bitmap);
//Paint p = new Paint();
//p.setColor(Color.RED);

// draw a line onto the canvas
//canvas.drawLine(0, 0, 50, 50, p);
}
}

Upvotes: 1

Views: 5542

Answers (1)

Dave
Dave

Reputation: 6104

You'll want something along the lines of:

// load picture and create a canvas to draw onto
Bitmap bitmap = BitmapFactory.decodeFile("my_pretty_picture.png");
Canvas canvas = new Canvas(bitmap);

// set drawing colour
Paint p = new Paint();
p.setColor(Color.RED);

// draw a line onto the canvas
canvas.drawLine(0, 0, 50, 50, p);

The rest is for you to fill in :)

Upvotes: 3

Related Questions