views:

80

answers:

2

Is it possible to manipulate images using canvas? How do we get the image onto the canvas?

@Override 
protected void onDraw(Canvas canvas) {


   Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
   Bitmap mBitmap = bitmap.copy(bitmap.getConfig(), true);
   canvas = new Canvas(mBitmap);
   Matrix matrix = new Matrix();

   canvas.drawBitmap(mBitmap, matrix, mPaint);
}

I'm unable to see the image on the screen. canvas.drawBitmap() shouldn't be necessary since I'm using the constructor and passing mBitmap.

A: 

Yes you can, but you don't "get the image onto the Canvas," a Canvas is just an interface to draw onto a Bitmap. To do so, simply create a Canvas and give a reference to your mutable Bitmap:

Canvas c = new Canvas(myBitmap);

Very easy :)

Romain Guy
thanks. I want to manipulate video. So is it possible to record the whole scene in front of you but display only half of it to the user as preview?
Namratha
Please see above. I went ahead according to your suggestion but I'm unable to see the image.
Namratha
A: 

You shouldn't do this in your onDraw method. Try to create an ImageView that should display the image and then set the bitmap via setImageBitmap(bitmap).

In general you are doing somethings wrong in your code.

  1. Loading an image every time something should be drawn to the screen will slow down your application a lot. Try to load the image only once and then just edit and set it to the imageview.
  2. You are assigning a new Canvas to the parameter you got. But this won't change anything on the screen. Think of it like somebody puts a paper in front of you and says:"Please write your message on the paper". Now you take another blank paper and write on it. But the other person only knows about his paper and will use this paper to read your message.
Janusz