4

Let me start off with I'm mostly new to writing Java, so I would love for a complete explanation. Not just a spew of code, but something that gives some in site to why and where it should be

I'm currently attempting to write an Application, how ever I'm have some trouble with the Canvas and drawing the bitmap I would like onto it. This is my code to draw the image:

Canvas canvas = null;
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.image);
canvas.drawBitmap(image, 10, 800, null);

This method (expectantly) throws a Null Pointer Exception. However, Defining "canvas" as null is the only option Eclipse gives me. When attempting to define it as

Canvas canvas = new Canvas();

Android simply refuses to draw the bitmap. I'm sure my image is located at res/drawable/image If it helps, the image is saved as "image.png"

How would I make Android display/display my image/bitmap at the specific location I have asked it to draw the image (10x800)?

DeadChex
  • 4,379
  • 1
  • 27
  • 34

1 Answers1

4

Writing nonsense that solves a compiler error doesn't change the fact that it's nonsense! :)

You have to get a reference to a Canvas object another way. You might want to be more specific about what you are exactly trying to do, so that we can suggest how you should go about it. (Ex. are you attempting to just display an image along with some other Views? Are you trying to create a custom View? You might just want to consider using an ImageView)

Edit:

You should read up about Android architecture at developer.android.com . If you are simply trying to show an image, there might not be a reason to use the canvas directly. Nonetheless, you can draw within a custom View by extending the View class

class myView extends View{

    Bitmap bm;

    loadBitmap()
    {
        bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);
    }

    @Override
    public void draw(Canvas c)
    {
       c.drawBitmap(bm, XCORD, YCORD, null);
    }
}

if you don't need a custom view, just use the ImageView class

class MyActivty extends Activity{
   @Override
   public void onCreate(Bundle b)
   {
       super.onCreate(b);
       ImageView iv = new ImageView(this);
       iv.setImageResource(R.drawable.pic);
       setContentView(iv);
   }
}

Warning: I wrote those method calls off of the top of my head, they might be slightly off.

you786
  • 3,659
  • 5
  • 48
  • 74
  • I'm just attempting to get Android to Draw a bitmap, in the current View, at the cords of 10X800... Code to do just that would be appreciated, or just giving me what canvas must be initialized as to prevent the NUllPointer while allowing Android to draw and Display the bitmap – DeadChex Jan 29 '12 at 15:31
  • The first soloutiton, both in my project and a new project cause Eclipse to error with "Implicit super constructor View() is undefined for default constructor. Must define an explicit constructor" on 'class CustomView extends View{' Specifically the "CustomView" Part of it... Adding 'public CustomView(Context context) { super(context); // TODO Auto-generated constructor stub }' As sugessted by Eclipse, solve the issue, how ever Android does not display the image (Yes I called c.drawBitmap) Please help, I will try the Second Solution soon, and post back – DeadChex Jan 29 '12 at 22:16
  • I'm sorry, but from what I can tell it seems like you haven't read the documentation at all for Android. (Not to mention, you probably need to learn a lot more about programming in general before you can even attempt to write an Android application). Read the introduction and follow all of the tutorials at developer.android.com. – you786 Jan 29 '12 at 22:28
  • Your Second soloution goes through successfully on a New Project, How ever Android doesn't display my image, That one makes no sense. The image is put into iv, and iv is set as the content view... Do I have to put the ImageView in my XML? (Please forgive me, I'm fairly new to Android, and the Dev site seems to make no sense and I get lost) And I have done all the Tutorials, none go over Image in a spefic spot, and nothing on the internet does give a tutorial that works... – DeadChex Jan 29 '12 at 22:29
  • http://developer.android.com/resources/tutorials/hello-world.html Follow this, and instead of a textview just use an ImageView like I suggested. If it still doesn't work, then try a different image file. Also, you may want to read about the difference between a View and an Activity – you786 Jan 29 '12 at 22:32
  • While that does display the image, it doesn't answer the question, I need the image at a CERTAIN SET OF COORDINATES. The resources for ImageView give no Methods for a specific spot... – DeadChex Jan 29 '12 at 22:44
  • Modification of the First Solution, put into its own Activity, and called from the main works, Thanks! – DeadChex Jan 30 '12 at 01:57
  • Don't reload the bitmap right before you draw, this will continue reload it every time the screen refreshes (around 30 / sec for normal views IIRC) – you786 Feb 09 '12 at 03:11