newb--Q: HOW to DISPLAY MODIFIED IMAGE

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

newb--Q: HOW to DISPLAY MODIFIED IMAGE

Postby doubleslash » Fri Feb 13, 2009 6:33 am

Hi,
I need help solving the following basic problem. I want to create an app that displays an image when loaded and a button, which, when pressed, will display the modified image with an 'X' drawn over it.

I took the following approach. I have the image as a png file stored in resources. In my layout, I have an ImageView to display the image. In my Activity class, when the button is pressed, I have the code as follows:
Code: Select all
Canvas canvas = new Canvas(mybmp); //mybmp iis the Bitmap obtained from applying BitmapFactory on my resource image
//drawing 'X' over the image
canvas.drawLine(0,0,width, height,null);
canvas.drawline(0,height,width,0,null);
mImageView.setImageBitmap(mybmp);//mImageView is the ImageView object used to display my image


I also tried calling setContentView after the image source has been modified, but the program crashed. I'm confused about when I can call setContentView, Canvas, Bitmap, and Drawable. If you could provide any info as to how those elements fit together when rendering an image on the screen, it would be extremely helpful.
Thanks for your help.
doubleslash
Developer
Developer
 
Posts: 30
Joined: Fri Feb 13, 2009 6:05 am

Top

Postby MrSnowflake » Fri Feb 13, 2009 10:05 am

You create a Canvas from you image, and modify that, so the X will appear on the canvas, not on mybmp. You should make a bmp from the canvas.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby doubleslash » Fri Feb 13, 2009 12:04 pm

Thanks for your reply. I think the main reason why my original code doesn't work is that it makes draw calls on an immutable bitmap. The following works
Code: Select all
public Bitmap DrawX(Bitmap src){
int height= src.getHeight();
int width = src.getWidth();
//preparing Paint
       Paint mybrush = new Paint();
       mybrush.setColor(Color.BLACK);
  //create mutable bitmap copy
        Bitmap mod_bmp = src.copy(src.getConfig(), true);
  //use canvas to draw on the mutable bitmap
           Canvas canvas = new Canvas(mod_bmp);
           if(mod_bmp.isMutable()){
              //draw 'X' on the image
               canvas.drawLine(0, 0, width, height, mybrush);
               canvas.drawLine(0, height, width, 0, mybrush);
           }
           //mod_bmp is the original with 'X' drawn over it
           return mod_bmp;
}


A new question now: how can I save the new image as a file?
doubleslash
Developer
Developer
 
Posts: 30
Joined: Fri Feb 13, 2009 6:05 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Exabot [Bot] and 10 guests