Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Draw on top of an image

b2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                setContentView(R.layout.new_main);

                String editTextStr = text.getText().toString();
                Toast msg = Toast.makeText(getBaseContext(),"/sdcard/Stored_Images/" + editTextStr + ".jpg", Toast.LENGTH_LONG);
                msg.show();

                Bitmap bmp = BitmapFactory.decodeFile("/sdcard/Stored_Images/" + editTextStr + ".jpg");
                ImageView img = (ImageView) findViewById(R.id.ImageView01);
                img.setImageBitmap(bmp);
            }
        });

The code above displays an image on the screen that is saved on the sd card.

Canvas c = holder.lockCanvas();
c.drawARGB(255,0,0,0);
onDraw(c);
holder.unlockCanvasAndPost(c);

This code creates a canvas to draw on (black screen).

I want to be able to combine the two to set/display an image as the canvas so that I can draw on it. So if i take a picture of someones face, I want to be able to display that image so that I can draw a mustache or something on it.

like image 731
homes Avatar asked Dec 06 '12 04:12

homes


People also ask

Can you draw on pictures on android?

Luckily, drawing on photos on your Android is super easy! Google Photos is a free default app that comes pre-installed on most Androids and has a few new features that will allow Android users to markup and draw on existing photos. If you have a Samsung phone, you also have a photo editor pre-installed on your Android!


2 Answers

You are probably better off creating the canvas, adding the bitmap image to it and then handling the user touch/drawing from there.

Bitmap bmp = BitmapFactory.decodeFile("/sdcard/Stored_Images/" + editTextStr + ".jpg");
mCanvas = new Canvas(bmp);

then for the drawing... sound like you have that figured out, but if not you can check out the fingerPaint samples from the api demos that demonstrate drawing on a canvas (which you would have your image on at that point.)

like image 169
Patrick Kafka Avatar answered Oct 21 '22 10:10

Patrick Kafka


You may customize the ImageView and made drawing on your image in onDraw(Canvas canvas)

For Example:

In your Activity:

1)create Bitmap from the image

2)set the Bitmap to customized ImageView

 a) create object for customized ImageView

MyImageView view =new MyImageView(this);

 b) set the bitmap


view.setImageBitmap(bmp);

MyImageView.java:

class MyImagView extends ImageView{
  //constructor
   public MyImageView(Context context){
}

//onDraw()

@Override
protected void onDraw(Canvas canvas)
{
             super.onDraw(canvas);
             //drawlines where ever you want using canvas.drawLine()
}

@Override
 public void setImageBitmap(Bitmap bm)
 { 
   super.setImageBitmap(bm);

}

}
like image 20
mini Avatar answered Oct 21 '22 08:10

mini